what comes after LoadConfiguration?

Dec 8, 2010 at 9:19 PM

Please bare with me, I am new to Unity and it's concepts, but I think I have a decent grasp of it. 

I have an object called "Unit" in a Project called Library.  Unit contains some functions that retrieve Unit lists.  (Library contains all my Business Objects.)

I have a separate project that I want to inject the Library objects into called Manager. 

I am not referencing Library in my Manager anywhere.  I am using Design Time Configuration using the app.config to configure Unity.

In the Manager code I have a Load() routine that creates a container and loads it using LoadConfiguration.  I can step through my code and see Registrations in the container.  My code looks like this:

Private cnt As IUnityContainer = New UnityContainer().LoadConfiguration

Configuration looks like this:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="Library"/>
    <namespace name="Library.BusinessObjects.Aircraft"/>
    <namespace name="Library.BusinessObjects.Unit"/>
      <register type="IUnit" mapTo="Unit" name="Unit"/>
      <register type="IUnitInfo" mapTo="UnitInfo" name="UnitInfo"/>
      <register type="IUnitInfoList"  mapTo="UnitInfoList" name="UnitInfoList"/>
      <register type="IAircraft"  mapTo="Aircraft" name="Aircraft"/>
      <register type="IAircraftInfo" mapTo="AircraftInfo" name="AircraftInfo"/>
      <register type="IAircraftInfoList" mapTo="AircraftInfoList" name="AircraftInfoList"/>

My confusion is this:  Since I am using Design Time Configuration, how do I get my Unit object out of the container into a concrete form?  What do I instantiate my object(s) as?  What's comes after LoadConfiguration.  Do I still need to resolve my objects?  If so, how?


Dec 8, 2010 at 11:38 PM

"how do I get my Unit object out of the container into a concrete form?"

You mean how do you obtain an instance ofa Unit object?  You can get an instance of Unit object by resolving it from the container: 

IUnit unit = cnt.Resolve<IUnit>();

Use the interface type, not the actual implementation. 

Anyway, if you want to inject a dependency to your Manager class, you can make IUnit a parameter in its constructor and register the Manager class in the config as well with constructor injection.  Assuming the Manager is in a project called Business with the constructor below:

public Manager(IUnit unit)

the registration for the Manager class should look like: 

<register type="Business.Manager, Business" />
          <param name="unit" dependencyType="IUnit" />

and then when you resolve for the Manager class,

Manager manager = cnt.Resolve<Manager>();

it will automatically be created by Unity passing an instance of IUnit as defined in your IUnit registration.  Note that you can also opt not to make IUnit a parameter in the constructor; you can make it a public property as well and configure property injection for it.  This is included in the documentation so you can check it out.


Does this answer your question?


Global Technologies and Solutions
Avanade, Inc.

Dec 9, 2010 at 1:40 AM
Sarah, the problem I have is that there are no references in the Manager to IUnit so I cannot instantiate an object. I cannot IUnit unit = cnt.Resolve<IUnit>() because Manager knows nothing about Unit or IUnit other than what I'm trying to inject. That's where I'm getting messed up. Like I said, when I run the program, my container has the registrations in it. I've gotten that far. I just need to resolve them. Should I instantiate a generic object to resolve them? Object unit = cnt.Resolve...Resolve what? I was under the impression Unity allows me to loosely couple my objects. I'm trying to keep them as loose as possible. Am I wrong on this?
Dec 9, 2010 at 10:31 PM

What does the Manager type look like? In general, what you need is to have the interfaces (or base class) referenced in the "consumer" class. The implementations of those interfaces don't need to be referenced, and that's where the container kicks in.

If Manager doesn't have any references to IUnit, how do you intent it to use those objects? There still needs to be some common interface or base class or something; Unity isn't magic, and you still need shared types somewhere.

How would the code you want work if you weren't using the container (assuming hard references for now)? If you could post that it would be easier to figure out how to get the container to do it.