This project is read-only.

Using a Dependency Resolver

A dependency resolver is an object that implements the IDependencyResolver interface. This interface is used to resolve object instances. It is similar to other dependency injection frameworks and the Common Service Locator. This is intentional. The interface supports resolving types using generic and non-generic
method variants.

Here is the IDependencyResolver interface:

    public interface IDependencyResolver : IDisposable
    {
        object GetInstance(Type targetType);
        object GetInstance(Type targetType, string name);
        IEnumerable GetAllInstances(Type targetType);
        TTargetType GetInstance<TTargetType>();
        TTargetType GetInstance<TTargetType>(string name);
        IEnumerable<TTargetType> GetAllInstances<TTargetType>();
    }

IDependencyResolver defines 3 non-generic and 3 generic methods used to resolve object instances.

The IDependencyResolver non-generic methods are:
  • object GetInstance(Type targetType);
  • object GetInstance(Type targetType, string name);
  • IEnumerable GetAllInstances(Type targetType);

The IDependencyResolver generic methods are:
  • TTargetType GetInstance<TTargetType>();
  • TTargetType GetInstance<TTargetType>(string name);
  • IEnumerable<TTargetType> GetAllInstances<TTargetType>();

It is useful to read the code doc XML on the IDependencyResolver interface source code for more details as well as the Lesson abstract base class in the Drill.Samples.Sample01ConsoleApplication sample application in the Drill source code for example usages that are well documented.

Here are some example usage snippets, one for each of the methods shown above:

    IColor color = (IColor)dependencyResolver.GetInstance(typeof(IColor));

    IColor color = (IColor)dependencyResolver.GetInstance(typeof(IColor), "Green");

    IEnumerable allColors = 
        dependencyResolver.GetAllInstances(typeof(IColor));

    IColor color = dependencyResolver.GetInstance<IColor>();

    IColor color = dependencyResolver.GetInstance<IColor>("Green");

    IEnumerable<IColor> allColors = dependencyResolver.GetAllInstances<IColor>();



Last edited Nov 24, 2012 at 8:09 PM by wreynolds, version 4

Comments

No comments yet.