Resolving All Objects of a Particular Type
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 Enterprise Library information can be found at the Enterprise Library site. |
This scenario explores how you can retrieve a list of concrete instances of registered objects from a Unity container using the ResolveAll method when you have registered mappings for a type and differentiated them by name.
Typical Goals
In this scenario, you retrieve an enumerable list of objects from the container. The list includes all objects registered with the specified type, and with a name that identifies the mapping or registration. You can then iterate through the list to locate an object or to display a list of the objects.
Solution
To retrieve a list of object instances from the container based on named registrations, use the ResolveAll method and specify a value for the object type (the registration type). The ResolveAll method returns an IEnumerable generic List of the non-default (named) registered types that you can iterate through in code to examine each object. Notice that it does not include in the list objects registered as the default (un-named) mapping for the specified type. If no registrations or mappings match the type you specify, the ResolveAll method returns null (in C#) or Nothing (in Visual Basic) Mapping names are simple strings, but are case-sensitive.
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 a list of registered objects for a specified registered type
- Using a reference to the container, call the ResolveAll method and specify the required object type (the type specified when registering the object). The following code assumes you have registered one or more named mappings for the type IMyObject.
- Iterate over the list performing any required tasks with each object in the list. The following code assumes that the registered non-default mappings for IMyObject return objects of type MyRealObject.
More Information
For more information about the techniques discussed in this scenario, see the following topics: