Resolving an Object by Type and Registration Name
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.
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.
|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.
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.
- 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.
|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.|