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

DIAB is now Unity

Published 14 January 08 05:19 PM

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

# MSDN Blog Postings » DIAB is now Unity said on January 14, 2008 9:23 PM:

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

# bradwils said on January 15, 2008 10:34 AM:

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

# gmelnik said on January 15, 2008 11:03 AM:

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

# TerryLee said on January 15, 2008 11:14 AM:
# derekgreer said on January 20, 2008 9:03 PM:

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

# gmelnik said on January 22, 2008 4:22 PM:

@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

# FrancoisTanguay said on January 23, 2008 4:00 PM:

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.

# FrancoisTanguay said on January 23, 2008 4:13 PM:

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");

# Software Mechanics said on January 26, 2008 2:18 AM:

Deconstructing ObjectBuilder - Introduction

# Being Scott Densmore said on January 30, 2008 12:34 AM:

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

# Noticias externas said on January 30, 2008 1:32 AM:

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

# Tom Hollander's blog said on January 30, 2008 4:25 AM:

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

# Noticias externas said on January 30, 2008 4:34 AM:

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

# nickp said on January 30, 2008 6:12 AM:

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.

# Tom Hollander's blog said on February 2, 2008 3:03 AM:

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

# Noticias externas said on February 2, 2008 3:32 AM:

If you&#39;ve been following Grigori Melnik&#39;s blog , you&#39;ll know a bit about Unity , the new

# Matthew Podwysocki's Blog said on February 18, 2008 7:48 PM:

As many people who read this would know, I'm a big fan of IoC containers, more in particular Castle Windsor

# Matthew Podwysocki's Blog said on February 18, 2008 7:50 PM:

As many people who read this would know, I'm a big fan of IoC containers, more in particular Castle Windsor

# nblog said on March 11, 2008 7:15 AM:

Od kilku miesięcy nic tu nie pisałem (oczywiście poza poprzednim nieplanowanym wpisem ). Jak łatwo się

Anonymous comments are disabled

This Blog

Syndication

Page view tracker