Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify constructor for the Unity IoC container to use

I'm using the Unity IoC container for resolving my objects. However, I've run into an issue. When I have more than one constructor - how does Unity know which one to use? It seems to use the one with parameters when I have one with and one without. Can I explicitly tell it which constructor to use?

Specifically I had a case similar to the following Person class with two constructors. In this case I want the IoC container to use the default constructor - without parameters - but it chooses the one with parameters.

public class SomeValueObject {     public SomeValueObject(string name)     {         Name = name;      }     public string Name { get; set; } }  public class Person {     private string _name;       public Person()     {         _name = string.Empty;     }      public Person(SomeValueObject obj)     {         _name = obj.Name;     } } 

This obviously fails as it can't create the SomeValueObject - not knowing what to inject to its string parameter. The error it gives is:

Resolution of the dependency failed, type = "MyApp.Person", name = "". Exception message is: The current build operation (build key Build Key[MyApp.Person, null]) failed: The parameter obj could not be resolved when attempting to call constructor MyApp.Person(MyApp.SomeValueObject obj). (Strategy type BuildPlanStrategy, index 3)

The container registration:

Container.RegisterType<Person, Person>(new Microsoft.Practices.Unity.ContainerControlledLifetimeManager()); 

And the resolving:

var person = Container.Resolve<Person>(); 
like image 365
stiank81 Avatar asked Jan 29 '10 13:01

stiank81


People also ask

Is Unity IoC container?

Unity is an IoC container released by Microsoft and is very simple, flexible and easy to use.

What is the use of unity container in C#?

The Unity Container (Unity) is a full featured, extensible dependency injection container. It facilitates building loosely coupled applications and provides developers with the following advantages: Simplified object creation, especially for hierarchical object structures and dependencies.

How do I register a type with unity container?

Before Unity resolves the dependencies, we need to register the type-mapping with the container, so that it can create the correct object for the given type. Use the RegisterType() method to register a type mapping. Basically, it configures which class to instantiate for which interface or base class.


2 Answers

Register it like this instead:

container.RegisterType<Person>(new InjectionConstructor()); 

You can add the LifetimeManager as well using an overload of the RegisterType method.

That said, when modeling for DI, your life will be much easier if you have unambiguous contructors (i.e. no overloaded constructors).

like image 51
Mark Seemann Avatar answered Sep 20 '22 11:09

Mark Seemann


By default, Unity chooses a constructor with maximum number of arguments. To override this, decorate required constructor with InjectionConstructorAttribute.

like image 37
Anton Gogolev Avatar answered Sep 23 '22 11:09

Anton Gogolev