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.

This scenario explores how you can retrieve concrete instances of registered object from a Unity container using the Resolve method.

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 the object registration type.

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.


Ff649620.note(en-us,PandP.10).gifNote:
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.
  2. IMyService result = myContainer.Resolve<IMyService>();
    
    
    Ff649620.note(en-us,PandP.10).gifNote:
    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.

  3. If the registration was an object of type MyServiceBase, mapped to the concrete type CustomerService, the following code will retrieve an instance of CustomerService.
  4. MyServiceBase result = myContainer.Resolve<MyServiceBase>();
    
    
  5. 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.
  6. CustomerService result = (CustomerService)myContainer.Resolve<MyServiceBase>();
    
    

Ff649620.note(en-us,PandP.10).gifNote:
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.

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.

Show: