Where does Unity resolve ctor parameters?

Oct 6, 2010 at 1:52 PM

Hi!

Could someone point me to the location where Unity decides how to satisfy a specific dependency in a constructor?

e.g. if I have

public MyClass(ISomeDependency dep)
{ ... }

Where does the container decide what value it should use as the "dep" parameter?

Thanks!

Oct 6, 2010 at 2:51 PM
Edited Oct 6, 2010 at 2:52 PM

Through type registrations along with constructor injection.  If you didn't specify the constructor injection, it just always use the default registration for each dependency.  In the case of your MyClass, it will be able to create an instance of ISomeDependency out of the container if you configured a type registration for ISomeDepency like this:

<container>
      <register type="ISomeDependency" mapTo="SomeDependency" />
</container>
  

Default registrations are those without the name attribute.   If for example you have another registration for the type ISomeDependency like this:

<container>
      <register type="ISomeDependency" mapTo="SomeDependency" />
      <register type="ISomeDependency" mapTo="SomeDependency2"  name="dependency2"/>
</container>

And you would want to be able to resolve MyClass using SomeDependency2, you would have to configure the registration of type MyClass like this:

<register type="MyClass">
       <constructor>
               <param name="dep" dependencyName="dependency2" />
       </constructor>
</register>

If you want to explicitly tell the container which constructor to use and its parameter values, configure constructor injection.  It can be done either through configuration file via the <constructor> element as illustrated above or using the InjectionConstructor API.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Oct 6, 2010 at 3:29 PM

Hi Sarah,

thanks for your reply.

I think I need to clarify my problem: I know about the theory behind constructor injection and default registrations. What I need are the "mechanics". Where is the location in the source code that says "ok, there is no InjectionConstructor specified so I use the default registrations for all ctor parameters"?

Oct 6, 2010 at 6:01 PM

When creating the build plan (the generated factory method that actually creates objects) the container uses strategy objects, called policies, to decide what specifically to do. Policy objects are stored in a data structure called a PolicyList (which is really a dictionary). And no, I didn't name any of these data structures. :-)

There's three policy types, IConstructorSelectorPolicy, IPropertySelectorPolicy, and IMethodSelectorPolicy. Policies are looked up by build key - the (type, name) pair that you're resolving. If a policy is not registered for a particular build key, then there's a default for each of these. The default implementation is what specifies "look for these attributes, otherwise look for the longest constructor).

In the source tree, these default implementations are in the Unity\Unity\Src\ObjectBuilderCustomization directory. The defaults are actually set in the PolicyList in the UnityDefaultStrategiesExtension.cs file.

Hope this helps you find what you're looking for.

 

Oct 7, 2010 at 6:32 AM

Thanks Chris, that was exactly the information I was looking for :-)

As for the slightly confusing naming of the participating objects: is there any official documentation/overview that explains these objects' responsibilities and how they interact?