Resolving an Object by Type and Registration Name
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
The latest Unity Application Block information can be found at the Unity Application Block site. |
This scenario explores how you can retrieve concrete instances of registered object from a Unity container using the Resolve method when you have registered one or more mappings for a type and differentiated them by name.
Typical Goals
In this scenario, you retrieve a named instance of a registered object, a named reference to a singleton, or a named reference to an existing object managed by the container, using the object registration type and the mapping or registration name.
Solution
To retrieve object instances from the container based on a named registration, use the Resolve method and specify a value for the name property as well as the registered type. The Resolve method returns the object registered with the named mapping or type registration, or it raises an exception if there is no registration that matches the specified name. Registration names are simple strings that can contain spaces, but note that they are case-sensitive.
If you are not sure whether a named registration exists for an object, you can use the ResolveAll method to obtain a list of registrations and mappings, and then check for the object you need in the list returned by this method. However, this will cause the container to generate all the objects for all named registrations for the specified object type, which will affect performance. For information about using the ResolveAll method, see Resolving All Objects of a Particular Type.
Note
The API for the Unity container contains both generic and non-generic overloads of most of the methods, so that you can use it with languages that do not support the Generics syntax. The examples in this topic use the generic overloads. For more information about the non-generic overloads, see Unity Application Block Methods.
To retrieve an object from the container using a named mapping
Using a reference to the container, call the Resolve method and specify the required object type (the type specified when registering the object) and the registration name. If the registration was an interface of type IMyService, mapped to the concrete type DataService and registered with the name "Data" the following code will retrieve an instance of DataService.
IMyService result = myContainer.Resolve<IMyService>("Data");
'Usage Dim result As IMyService = myContainer.Resolve(Of IMyService)("Data")
Note
If the container does not contain a mapping for the specified combination of type and name, it will raise an exception. Also remember that names are case-sensitive.
If the registration was an object of type MyServiceBase, mapped to the concrete type CustomerService and registered with the name "Customers" the following code will retrieve an instance of CustomerService.
MyServiceBase result = myContainer.Resolve<MyServiceBase>("Customers");
'Usage Dim result As MyServiceBase = myContainer.Resolve(Of MyServiceBase)("Customers")
If you know what the actual returned type will be, you might decide to specify this as the return type, as shown in the following code. However, this is likely to cause an error if you later change the mappings in the container.
CustomerService result = (CustomerService)myContainer.Resolve<MyServiceBase>("Customers");
'Usage Dim result As CustomerService = myContainer.Resolve(Of MyServiceBase)("Customers")
Note
If the target class or object specifies any dependencies of its own, using constructor, property, or method call injection attributes, the instance returned will have these dependent object injected automatically. For information about using constructor, property, or method call injection techniques, see Annotating Objects for Constructor Injection, Annotating Objects for Property (Setter) Injection, and Annotating Objects for Method Call Injection.
More Information
For more information about the techniques discussed in this scenario, see the following topics: