Microsoft   |   patterns & practices   |   Developer Network   |   Enterprise Library   |   Acceptance Testing Guide   |   Personal Site

DIAB is now Unity - Grigori Melnik: Thoughts on Agile Software Engineering and Beyond - Site Home - MSDN Blogs

DIAB is now Unity

DIAB is now Unity

  • Comments 19

First of all, we finally came up with a name for the DI container we are building. A drum roll, please… We’ve decided to call it “Unity” as a container that epitomizes a quality of one (system) being, comprising, or consisting of a number (of parts/components). It may not be as descriptive as other blocks but it fits with our vision. We also wanted to distinguish it from other application blocks.

A short description of Unity is a lightweight, extensible dependency injection container.

Unity will provide mechanisms for building instances of objects, which may contain other dependent object instances.

Unity will expose Register methods that support configuring the container with mappings and objects (including singleton instances), and Get methods to return instances of built objects that contain any dependent objects. Note we have purposefully designed the API to support the “fluent interface” pattern. Here’s a preview of the fundamental features of the Unity API, these are very tentative as we are still working on the semantics:

public class UnityContainer : IDisposable {

// getting objects

public T Get<T>(string key){}

public IEnumerable<T> GetAll<T>(){}

// type mapping

public UnityContainer Register<TFrom, TTo>(string key) where TTo : TFrom {}

// singleton support

public UnityContainer SetSingleton<T>(string key) {}

// support for registering existing objects

public UnityContainer RegisterInstance<T>(T existing, string key, bool overload) {}

// running existing objects through the container

public T BuildUp<T>(T existing, string key){}

}

There are also overloads for non-generics.

So, a typical usage scenario will be:

UnityContainer container = new UnityContainer()

                  .Register<ILogger, TraceLogger>()

                  .Register<ISomething, Something>()
                  .Register<ISomethingElse, SomethingElse>();

container.Get<ISomething>();

As mentioned in our previous postings, Unity will be released in two forms - standalone container and as a part of Enterprise Library 4, with the existing blocks refactored to take advantage of Unity.

Unity will support extensibility via the ContainerExtension mechanism to allow third parties to add support for other on-demand features at runtime, such as event broker.

 

Comments
  • PingBack from http://msdnrss.thecoderblogs.com/2008/01/14/diab-is-now-unity/

  • Your sample usage doesn't appear to be legal given your interface.

  • Brad, there are also non-parametrized overloads for those methods - for the sake of simplicity, I've only listed one method from each group.

  • Grigori, while I like the idea of the P&P group providing a container for Object Builder, I would rather an object factory pipeline container be delivered rather than one specifically focused on DI.   Object Builder is currently used for both Dependency Injection (give me service XYZ, generic dependency MyBlah, etc.) as well as for broader Inversion of Control usages (register my events, register my actions, register my commands, register my services, etc.) and can even be used as a general purpose factory (e.g. MyObjectBuilderContainer.Create<MyType>()).

    I understand the desire to provide a simple interface for accomplishing dependency injection, but with this approach it looks like many of us are still going to be left to build our own containers if we want to do stuff like write our own Strategies, Policies, etc (assuming this is still the underlying architecture).

    - Derek

  • @derekgreer

    Derek, ObjectBuilder is a pipeline. We envision that once Unity is released, you would not need to use ObjectBuilder directly. We'll provide an extensibility mechanism for Unity and, yes, documentation and examples on how to do it, which hopefully will be a good foundation for what you are trying to do. I'll do another blog posting on that this week.

    Grigori

  • Will Unity provide built-in strategies for interception?

    Also, in the past I needed to register type mappings and singleton instances on a more complex contract than simply a name (key). Complex contracts can include specifications such as: target database, security level required, thread safety, reliability, ...

    For that, I was happy with ObjectBuilder which uses an object as the key.

  • And I know I'll have time to write about it when it comes out, but naming wise, I don't really like TFrom, To... It doesn't tell much...

    (TInterface or TService) and (TClass or TConcrete) are better imho.

    And I'd make a distinction between Get<T> and Find<T>: Find returns default(T) if not found, Get calls Find and throws if default<T> is returned.

    The former(as posted) increases code that has to be written.

    ILogger logger = container.Get<ILogger>();

    if (logger == null) { throw new ServiceNotFoundException(typeof(ILogger)); }

    logger.Log("Customer Created");

    while the later:

    container.Get<ILogger>().Log("Customer Created");

  • Deconstructing ObjectBuilder - Introduction

  • Unity is our new Dependency Injection Container that will soon be available. Read more from Grigori .

  • Unity is our new Dependency Injection Container that will soon be available. Read more from Grigori

  • In case you missed it, the "Dependency Injection Application Block" promised for Enterprise Library 4.0

  • In case you missed it, the &quot;Dependency Injection Application Block&quot; promised for Enterprise

  • Instead of the SetSingleton<T> in the public API, it would be nice if you could provide an overload of Register<TFrom, TTo> that would accept a lifestyle of an entity other than singleton such as transient or pooled.

  • If you've been following Grigori Melnik's blog , you'll know a bit about Unity , the new Dependency Injection

Page 1 of 2 (19 items) 12
Leave a Comment
  • Please add 2 and 8 and type the answer here:
  • Post