To perform injection of dependent classes into objects you create through the Unity container, you can use the following two techniques:
-
Single-constructor automatic injection. With this technique, you allow the Unity container to satisfy any constructor dependencies defined in parameters of the constructor automatically. You use this technique when there is a single constructor in the target class.
-
Multiple-constructor injection using an attribute. With this technique, you apply attributes to the class constructor(s) that specify the dependencies. You use this technique when there is more than one constructor in the target class.
Constructor injection is a form of mandatory injection of dependent objects, provided developers use the Unity container to generate the target object. The dependent object instance is generated when the Unity container creates an instance of the target class using the constructor.
For more information, see Notes on Using Constructor Injection.
Single-Constructor Automatic Injection
For automatic constructor injection, you simply specify as parameters of the constructor the dependent object types. You can specify the concrete type, or specify an interface or base class for which the Unity container contains a registered mapping.
To use automatic single-constructor injection to create dependent objects
- Define a constructor in the target class that takes as a parameter the concrete type of the dependent class. For example, the following code shows a target class named MyObject containing a constructor that has a dependency on a class named MyDependentClass.
public class MyObject
{
public MyObject(MyDependentClass myInstance)
{
// work with the dependent instance
myInstance.SomeProperty = "SomeValue";
// or assign it to a class-level variable
}
}
Public Class MyObject
Public Sub New(myInstance As MyDependentClass)
' work with the dependent instance
myInstance.SomeProperty = "SomeValue"
' or assign it to a class-level variable
End Sub
End Class
- In your run-time code, use the Resolve method of the container to create an instance of the target class. The Unity container will instantiate the dependent concrete class and inject it into the target class. For example, the following code shows how you can instantiate the example target class named MyObject containing a constructor that has a dependency on a class named MyDependentClass.
IUnityContainer uContainer = new UnityContainer();
MyObject myInstance = uContainer.Resolve<MyObject>();
Dim uContainer As IUnityContainer = New UnityContainer()
Dim myInstance As MyObject = uContainer.Resolve(Of MyObject)()
- Alternatively, you can define a target class that contains more than one dependency defined in constructor parameters. The Unity container will instantiate and inject an instance of each one. For example, the following code shows a target class named MyObject containing a constructor that has dependencies on two classes named DependentClassA and DependentClassB.
public class MyObject
{
public MyObject(DependentClassA depA, DependentClassB depB)
{
// work with the dependent instances
depA.SomeClassAProperty = "SomeValue";
depB.SomeClassBProperty = "AnotherValue";
// or assign them to class-level variables
}
}
Public Class MyObject
Public Sub New(depA As DependentClassA, depB As DependentClassB)
' work with the dependent instance
depA.SomeClassAProperty = "SomeValue"
depB.SomeClassBProperty = "AnotherValue"
' or assign them to class-level variables
End Sub
End Class
- In your run-time code, use the Resolve method of the container to create an instance of the target class. The Unity container will create an instance of each of the dependent concrete classes and inject them into the target class. For example, the following code shows how you can instantiate the example target class named MyObject containing a constructor that has constructor dependencies.
IUnityContainer uContainer = new UnityContainer();
MyObject myInstance = uContainer.Resolve<MyObject>();
Dim uContainer As IUnityContainer = New UnityContainer()
Dim myInstance As MyObject = uContainer.Resolve(Of MyObject)()
- In addition to using concrete types as parameters of the target object constructor, you can use interfaces or base class types and then register mappings in the Unity container to translate these types into the correct concrete types. Define a constructor in the target class that takes as parameters the interface or base types of the dependent class. For example, the following code shows a target class named MyObject containing a constructor that has a dependency on a class that implements the interface named IMyInterface and a class that inherits from MyBaseClass.
public class MyObject
{
public MyObject(IMyInterface interfaceObj, MyBaseClass baseObj)
{
// work with the concrete dependent instances
// or assign them to class-level variables
}
}
Public Class MyObject
Public Sub New(interfaceObj As IMyInterface, baseObj As MyBaseClass)
' work with the dependent instance
' or assign them to class-level variables
End Sub
End Class
- In your run-time code, register the mappings you require for the interface and base class types, and then use the Resolve method of the container to create an instance of the target class. The Unity container will instantiate an instance of each of the mapped concrete types for the dependent classes and inject them into the target class. For example, the following code shows how you can instantiate the example target class named MyObject containing a constructor that has a dependency on the two objects of type IMyInterface and MyBaseClass.
IUnityContainer uContainer = new UnityContainer()
.RegisterType<IMyInterface, FirstObject>()
.RegisterType<MyBaseClass, SecondObject>();
MyObject myInstance = uContainer.Resolve<MyObject>();
Dim uContainer As IUnityContainer = New UnityContainer() _
.RegisterType(Of IMyInterface, FirstObject)() _
.RegisterType(Of MyBaseClass, SecondObject)()
Dim myInstance As MyObject = uContainer.Resolve(Of MyObject)()
Multiple-Constructor Injection Using an Attribute
When a target class contains more than one constructor with the same number of parameters, you must apply the InjectionConstructor attribute to the constructor that the Unity container will use to indicate which constructor the container should use. As with automatic constructor injection, you can specify the constructor parameters as a concrete type, or you can specify an interface or base class for which the Unity container contains a registered mapping.
To use attributed constructor injection when there is more than one constructor
- Apply the InjectionConstructor attribute to the constructor in the target class that you want the container to use. In the simplest case, the target constructor takes as a parameter the concrete type of the dependent class. For example, the following code shows a target class named MyObject containing two constructors, one of which has a dependency on a class named MyDependentClass and has the InjectionConstructor attribute applied.
public class MyObject
{
public MyObject(SomeOtherClass myObjA)
{
...
}
[InjectionConstructor]
public MyObject(MyDependentClass myObjB)
{
...
}
}
Public Class MyObject
Public Sub New(myObjA As SomeOtherClass)
...
End Sub
<InjectionConstructor()> _
Public Sub New(myObjB As MyDependentClass)
...
End Sub
End Class
- In your run-time code, use the Resolve method of the container to create an instance of the target class. The Unity container will instantiate the dependent concrete class defined in the attributed constructor and inject it into the target class. For example, the following code shows how you can instantiate the example target class named MyObject containing an attributed constructor that has a dependency on a class named MyDependentClass.
IUnityContainer uContainer = new UnityContainer();
MyObject myInstance = uContainer.Resolve<MyObject>();
Dim uContainer As IUnityContainer = New UnityContainer()
Dim myInstance As MyObject = uContainer.Resolve(Of MyObject)()
- Alternatively, you can define a multiple-constructor target class that contains more than one dependency defined in the target constructor parameters. The Unity container will instantiate and inject an instance of each one. For example, the following code shows a target class named MyObject containing an attributed constructor that has dependencies on two classes named DependentClassA and DependentClassB.
public class MyObject
{
public MyObject(SomeClassA objA, SomeClassB objB)
{
...
}
[InjectionConstructor]
public MyObject(DependentClassA depA, DependentClassB depB)
{
...
}
}
Public Class MyObject
Public Sub New(objA As SomeClassA, objB As SomeClassB)
...
End Sub
<InjectionConstructor()> _
Public Sub New(depA As DependentClassA, depB As DependentClassB)
...
End Sub
End Class
- In your run-time code, use the Resolve method of the container to create an instance of the target class. The Unity container will create an instance of each of the dependent concrete classes defined in the attributed constructor and inject them into the target class. For example, the following code shows how you can instantiate the example target class named MyObject containing a constructor that has constructor dependencies
IUnityContainer uContainer = new UnityContainer();
MyObject myInstance = uContainer.Resolve<MyObject>();
Dim uContainer As IUnityContainer = New UnityContainer()
Dim myInstance As MyObject = uContainer.Resolve(Of MyObject)()
- In addition to using concrete types as parameters of the target object constructor, you can use interfaces or base class types, and then register mappings in the Unity container to translate these types into the correct concrete types. For details, see steps 5 and 6 of the procedure Single-Constructor Automatic Injection.