By using dependency injection frameworks and inversion of control mechanisms, developers can generate and assemble instances of custom classes and objects that can contain dependent object instances and settings. The Unity Application Block supports this functionality, allowing developers to use techniques such as container-configured injection, constructor injection, property injection, and method call injection to generate and assemble instances of objects complete with all dependent objects and settings.
The Unity Application Block exposes two methods for registering types and mappings with the container:
- RegisterType. This method registers a type with the container. At the appropriate time, the container will build an instance of the type you specify. This could be in response to dependency injection initiated through class attributes or when you call the Resolve method. The lifetime of the object it builds will correspond to the lifetime you specify in the parameters of the method. If you do not specify a value for the lifetime, the type is registered for a transient lifetime, which means that the container will create a new instance on each call to Resolve.
- RegisterInstance. This method registers with the container an existing instance of a type that you specify, with the lifetime that you specify. The container will return the exiting instance during that lifetime. If you do not specify a value for the lifetime, the instance will have a container-controlled lifetime.
Container-Configured Registration of Types
As an example of using the overloads of the RegisterType and Resolve methods, the following code registers a mapping for an interface named IMyService, and specifies that the container should return an instance of the CustomerService class (which implements the IMyService interface).
IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<IMyService, CustomerService>();
IMyService myServiceInstance = myContainer.Resolve<IMyService>();
Dim myContainer As IUnityContainer = New UnityContainer()
myContainer.RegisterType(Of IMyService, CustomerService)()
Dim myServiceInstance As IMyService = myContainer.Resolve(Of IMyService)()
Container-Configured Registration of Existing Object Instances
As an example of using the overloads of the RegisterInstance and Resolve methods, the following code registers an existing instance of a class named LoggingService that implements the interface IMyService and then retrieves that instance.
IUnityContainer myContainer = new UnityContainer();
LoggingService myExistingObject = new LoggingService();
myContainer.RegisterInstance<IMyService>(myExistingObject);
IMyService myServiceInstance = myContainer.Resolve<IMyService>();
Dim myContainer As IUnityContainer = New UnityContainer()
Dim myExistingObject As New LoggingService()
myContainer.RegisterInstance(Of IMyService)(myExistingObject)
Dim myServiceInstance As IMyService = myContainer.Resolve(Of IMyService)()
Constructor Injection
As an example of constructor injection, if a class that developers instantiate using the Resolve method of the Unity container has a constructor that defines one or more dependencies on other classes, the Unity container will automatically create the dependent object instance specified in parameters of the constructor. For example, the following code shows a class named CustomerService that has a dependency on a class named LoggingService.
public class CustomerService
{
public CustomerService(LoggingService myServiceInstance)
{
// work with the dependent instance
myServiceInstance.WriteToLog("SomeValue");
}
}
Public Class CustomerService
Public Sub New(myServiceInstance As LoggingService)
' work with the dependent instance
myServiceInstance.WriteToLog("SomeValue")
End Sub
End Class
At run time, developers create an instance of the CustomerService class using the Resolve method of the container, which causes the instance generation framework to inject an instance of the concrete class LoggingService within the scope of the CustomerService class.
IUnityContainer uContainer = new UnityContainer();
CustomerService myInstance = uContainer.Resolve<CustomerService>();
Dim uContainer As IUnityContainer = New UnityContainer()
Dim myInstance As CustomerService = uContainer.Resolve(Of CustomerService)()
Property (Setter) Injection
In addition to constructor injection, described earlier, the Unity Application Block supports property and method call injection. The following code demonstrates property injection. A class named ProductService exposes as a property a reference to an instance of another class named SupplierData (not defined in the following code). To force dependency injection of the dependent object, developers must apply the Dependency attribute to the property declaration, as shown in the following code.
public class ProductService
{
private SupplierData supplier;
[Dependency]
public SupplierData SupplierDetails
{
get { return supplier; }
set { supplier = value; }
}
}
Public Class ProductService
Private supplier As SupplierData
<Dependency()> _
Public Property SupplierDetails() As SupplierData
Get
Return supplier
End Get
Set (ByVal value As SupplierData)
supplier = value
End Set
End Property
End Class
Now, creating an instance of the ProductService class using the Unity Application Block will automatically generate an instance of the SupplierData class and set it as the value of the SupplierDetails property of the ProductService class.
Note: |
| For more details of how you can use these and other features of the Unity Application Block in your applications, see Key Scenarios. |