Configuring Unity with a closed generic constructor parmater

Posted by fearofawhackplanet on Stack Overflow See other posts from Stack Overflow or by fearofawhackplanet
Published on 2010-06-10T11:56:33Z Indexed on 2010/06/10 14:52 UTC
Read the original article Hit count: 569

I've been trying to read the article here but I still can't understand it.

I have a constructor resembling the following:

IOrderStore orders = new OrderStore(new Repository<Order>(new OrdersDataContext()));

The constructor for OrderStore:

public OrderStore(IRepository<Order> orderRepository)

Constructor for Repository<T>:

public Repository(DataContext dataContext)

How do I set this up in the Unity config file?

UPDATE: I've spent the last few hours banging my head against this, and although I'm not really any closer to getting it right I think at least I can be a little more specific about the problem.

I've got my IRespository<T> working ok:

  <typeAlias alias="IRepository"
                  type="MyAssembly.IRepository`1, 
                        MyAssembly" />

   <typeAlias alias="Repository"
                  type="MyAssembly.Repository`1, 
                        MyAssembly" />

   <typeAlias alias="OrdersDataContext"
                  type="MyAssembly.OrdersDataContext, 
                        MyAssembly" />


   <types>

        <type type="OrdersDataContext">
          <typeConfig>
            <constructor /> <!-- ensures paramaterless constructor used -->
          </typeConfig>
        </type>

        <type type="IRepository" mapTo="Repository">
          <typeConfig>
            <constructor>
              <param name="dataContext" parameterType="OrdersDataContext">
                <dependency />
              </param>
            </constructor>
          </typeConfig>
        </type>

   </types>

So now I can get an IRepository like so:

IRepository rep = _container.Resolve>();

and that all works fine.

The problem now is when trying to add the configuration for IOrderStore

  <type type="IOrderStore" mapTo="OrderStore">
        <typeConfig>
          <constructor>
            <param name="ordersRepository"  parameterType="IRepository">
              <dependency />
            </param>
          </constructor>
        </typeConfig>
    </type>

When I add this, Unity blows up when trying to load the config file.

The error message is OrderStore does not have a constructor that takes the parameters (IRepository`1).

What I think this is complaining about is because the OrderStore constructor takes a closed IRepository generic type, ie OrderStore(IRepository<Order>) and not OrderStore(IRepository<T>)

I don't have any idea how to resolve this.

© Stack Overflow or respective owner

Related posts about generics

Related posts about unity