Resolving an Object by Type

Unity provides a method named Resolve that you can use to resolve an object by type, and optionally by providing a registration name. Registrations that do not specify a name are referred to as default registrations. This topic describes how to use the Resolve method to resolve types and mappings registered as default registrations. For information about resolving named registrations, see Resolving an Object by Type and Registration Name.

The Resolve Method Overloads for Default Registrations

The following table describes the overloads of the Resolve method that return instances of objects based on the default registrations and mappings with the container. The API for the Unity container contains both generic and non-generic overloads of this method so that you can use it with languages that do not support the generics syntax.

Method

Description

Resolve<T>( )

Returns an instance of the default type registered with the container as the type T.

Resolve(Type t)

Returns an instance of the default type registered with the container as the type t.

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, or you want to intercept calls to the object to inject a policy.

If you register a default type or a default type mapping more than once using the RegisterType method (in other words, if you register more than one type or type mapping that specifies the same types and does not specify a registration name) only the last registration remains in the container and is applied when you execute the Resolve method.

The default registration is the same as a registration where the name is null or an empty string.

Using the Resolve Method with Default Registrations

The following examples show how you can use the Resolve method to create or obtain a reference to an object defined in the container configuration. Typically you will register a type mapping between an interface and a concrete type that implements it, or between a base class and a concrete type that inherits it. The examples use the run-time methods of the container to register the types it will resolve. For more information about how you can configure Unity with type registrations and mappings, see Configuring Unity.

Resolving Types Registered as Interfaces

The following code registers a mapping for an interface named IMyService and specifies that the container should return an instance of the CustomerService class (which implements the IMyService interface). In this case, the type IMyService identifies the registration type. Code that requests an instance of the type IMyService receives an instance of the CustomerService class. The following example uses the generic overloads of the container methods.

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<IMyService, CustomerService>();
IMyService myServiceInstance = myContainer.Resolve<IMyService>();
'Usage
Dim myContainer As IUnityContainer = New UnityContainer()
myContainer.RegisterType(Of IMyObject, CustomerService)()
Dim myServiceInstance As IMyService = myContainer.Resolve(Of IMyService)()

Alternatively, you can use the non-generic overloads of the methods. The following code achieves the same result.

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType(typeof(IMyService), typeof(CustomerService));
IMyService myServiceInstance = (IMyService)myContainer.Resolve(typeof(IMyService));
'Usage
Dim myContainer As IUnityContainer = New UnityContainer()
myContainer.RegisterType(GetType(IMyService), GetType(CustomerService))
Dim myServiceInstance As IMyService = myContainer.Resolve(GetType(IMyService))

Resolving Types Registered as Base Classes

When you need to register a mapping for a base class or other object type (instead of an interface), you use the overloads of the RegisterType and Resolve methods that accept object type names. The following examples show the use of the overloads of the RegisterType and Resolve methods that accept object type names as the registration identifier.

The following code registers a mapping for an object named MyBaseService and specifies that the container should return an instance of the CustomerService class (which inherits from the MyBaseService class). In this case, the type MyBaseService identifies the registration. Code that requests an instance of the type MyBaseService receives an instance of the CustomerService class.

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<MyBaseService, CustomerService>();
MyBaseService myServiceInstance = myContainer.Resolve<MyBaseService>();
'Usage
Dim myContainer As IUnityContainer = New UnityContainer()
myContainer.RegisterType(Of MyBaseService, CustomerService)()
Dim myServiceInstance As MyBaseService = myContainer.Resolve(Of MyBaseService)()

Alternatively, you can use the non-generic overloads of the methods. The following code achieves the same result.

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType(typeof(MyBaseService), typeof(CustomerService));
MyBaseService myServiceInstance = (MyBaseService)myContainer.Resolve(typeof(MyBaseService));
'Usage
Dim myContainer As IUnityContainer = New UnityContainer()
myContainer.RegisterType(GetType(MyBaseService), GetType(CustomerService))
Dim myServiceInstance As MyBaseService = myContainer.Resolve(GetType(MyBaseService))

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 objects injected automatically.

By default, the RegisterType method registers a type with a transient lifetime, which means that the container will not hold onto a reference to the objects it creates when you call the Resolve method. Each time you call one of these methods, the container generates a new instance of the specified or mapped type. However, you can use lifetime managers to control the creation, lifetime, and disposal of objects if required.