Export (0) Print
Expand All

How to: Map Types for Dependency 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.

By using the Composite Web Application Block, you can use type mapping to associate one type (type “A”) with another type (type “B”) for dependency injection purposes. When your code uses the dependency injection attributes for type A, ObjectBuilder will instantiate and inject an object of type B. With type mapping, a class can request a new object by its interface or base class, as illustrated in the following code.

[CreateNew]
public ITypeA PropertyA
{
  get { return _a; }
}

This topic describes how to map types for dependency injection purposes.

Ff709904.note(en-us,PandP.10).gifNote:
Note: You do not need use type mapping for services. Services can be mapped to other types when they are added to the Services collection of a composition container. To do this, you use the overloads of the Add and AddNew methods that take an additional type parameter.

Prerequisites

This topic assumes you have an existing Web client solution with a business module or a foundational module.

For information about how to create a Web client solution, see How to: Create a Web Client Solution.

For information about how to create a business module, see How to: Create a Business Module.

For information about how to create a foundational module, see How to: Create a Foundational Module.

Steps

To map types for dependency injection purposes, you have to register type mappings with a composition container. To register a type mapping, you invoke the RegisterTypeMapping method on a composition container instance. The RegisterTypeMapping method has two overloads that receive two types as parameters:

  • Type requested. This is the type that developers will use to request for a dependency.
  • Type returned. This is the type of the object that ObjectBuilder will inject instead of the type requested.

The following code shows the RegisterTypeMapping method overloads’ signatures.

public void RegisterTypeMapping<TRequested, TReturned>() where TReturned : TRequested;
public void RegisterTypeMapping(Type requested, Type returned);

You can register module-level type mappings, which apply to a particular module, or global-level type mappings that apply to the entire application. Module-level type mappings override global-level type mappings. This means that if a global-level type mapping for a particular type exists, and a module-level type mapping for the same type exists in a module, ObjectBuilder will use the type mapping defined at the module level when injecting dependencies in objects within that module.

The following procedure describes how to register module-level type mappings.

To register a module-level type mapping

  1. In the Load method of the module initializer class of your business module, call the RegisterTypeMapping method on the moduleContainer instance, as shown in the following code.
    public override void Load(CompositionContainer moduleContainer)
    {
        base.Load(moduleContainer);
    moduleContainer.RegisterTypeMapping<IContactListModel, ContactListModel>();
    }
    

    In the preceding code, the type IContactListModel is mapped to the type ContactListModel. This means that when ObjectBuilder injects a dependency of type IContactListModel, it injects an instance of type ContactListModel.

Ff709904.note(en-us,PandP.10).gifNote:
Note: The previous procedure describes how to register type mappings in a module initializer class. Typically, you register type mappings in a module initializer class. However, you can register type mappings in other classes of your application.

The following procedure describes how to register global-level type mappings.

To register a global-level type mapping

  1. Invoke the RegisterTypeMapping method on the root composition container of your application, as shown in the following code.
    rootContainer.RegisterTypeMapping<ITypeA, TypeA>();
    

    You can obtain a reference to the root composition container by using the RootContainer property on any CompositionContainer instance. If you register a type mapping within a foundational module, it will be global because foundational modules do not have a particular composition container associated to the module; instead, they use the root composition container.

Show:
© 2014 Microsoft