Resolving an Object by 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 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.

Typical Goals

In this scenario, you retrieve the default (un-named) instance of a registered object, a reference to a singleton, or a reference to an existing object managed by the container, using only the object registration type.

Solution

To retrieve object instances from the container, use the Resolve method. When you use this method without specifying a value for the optional name property, the method returns the object registered with the default mapping.

Note

If you call the Resolve method and specify the default instance of a type that is not registered, the container simply generates and returns an instance of that type. However, the only time that this is useful is when the object you are generating contains dependency attributes that the container will use to inject dependent objects into the requested object.

If you call the Resolve method and specify a name as well as the registration type, and there is no mapping registered for that type and name, the container will raise an exception.

To retrieve an object from the container using the default mapping

  1. Using a reference to the container, call the Resolve method and specify the required object type (the type specified when registering the object). If the registration was an interface of type IMyService, mapped to the concrete type EmailService, the following code will retrieve an instance of EmailService.

    IMyService result = myContainer.Resolve<IMyService>();
    
    'Usage
    Dim result As IMyService = myContainer.Resolve(Of IMyService)()
    

    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. For more information, see Unity Application Block Methods.

  2. If the registration was an object of type MyServiceBase, mapped to the concrete type CustomerService, the following code will retrieve an instance of CustomerService.

    MyServiceBase result = myContainer.Resolve<MyServiceBase>();
    
    'Usage
    Dim result As MyServiceBase = myContainer.Resolve(Of MyServiceBase)()
    
  3. 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>();
    
    'Usage
    Dim result As CustomerService = myContainer.Resolve(Of MyServiceBase)()
    

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: