views:

46

answers:

1

Hi All,

I'm using my app.config to tell Unity my interface to type mappings...

<unity>
<containers>
  <container>
    <types>
      <type type="UnityDAL.Interfaces.IDataContextFactory, UnityDAL"
         mapTo="UnityDAL.UnityDemoDataContextFactory, UnityDAL" />
      <type type="UnityDAL.Interfaces.IProductRepository, UnityDAL"
         mapTo="UnityDAL.ProductRepository, UnityDAL" />
      <type name="productRepo" 
         type="UnityDAL.Interfaces.IProductRepository, UnityDAL"
         mapTo="UnityDAL.ProductRepository, UnityDAL" />

   and so on...

using this code

var wrapper = UnityWrapper.Create();
var productRepository = 
    wrapper.Container.Resolve<IProductRepository>("productRepo");
var productsBO = new ProductBO(productRepository);
var products = productsBO.GetAllProducts();

Here is the constructor for the wrapper object...

public UnityWrapper()
{
    _container = new UnityContainer();
    var section = 
        (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
    section.Containers.Default.Configure(_container);
}

but I get an exception that says...

{"Resolution of the dependency failed, type = \"IProductRepository\", name = \"productRepo\". Exception message is: The current build operation (build key Build Key[UnityDAL.ProductRepository, productRepo]) failed: The parameter dataContextFactory could not be resolved when attempting to call constructor UnityDAL.ProductRepository(UnityDAL.Interfaces. IDataContextFactory dataContextFactory). (Strategy type Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy, index 2)"}

I thought this node was wiring that up

<type type="UnityDAL.Interfaces.IDataContextFactory, UnityDAL"
   mapTo="UnityDAL.UnityDemoDataContextFactory, UnityDAL" />

The idea here was originally to create a nice dependency chain. Any idea what I'm doing wrong? If you have any advice or tips on how I can correct the problem, I would like to hear them. Thanks for any help.

Cheers,

~ck in San Diego

+1  A: 

This is a mapping problem.

Trying to resolve the UnityDAL.Interfaces.IProductRepository you need to first resolve UnityDAL.Interfaces.IDataContextFactory. Next trying to resolve UnityDAL.UnityDemoDataContextFactory you miss some mapping. Probably the ctor of the UnityDAL.UnityDemoDataContextFactory requires something that has being not registered.

By the way: what you do here is using a service location. I avoid this practice if possible but if you absolutely need this then try to expose the common service locator. This dll ships with Unity and provides a simple service locator ONLY interface.

bakopanos costas