Kurt Claeys

DEVITECT.NET

  Home  |   Contact  |   Syndication    |   Login
  127 Posts | 43 Stories | 193 Comments | 13 Trackbacks

News

I'm a .NET architect / developer from Belgium and also trainer in .NET topics.

Kurt CLAEYS

 



view my trainer profile on TrainerExchange.com
 

Join Me at Tech·Ed EMEA Connect for Developers!

View Kurt CLAEYS's profile on LinkedIn


Being ...





 


Working ...


and



Reading ...



Riding ...

Tag Cloud


Article Categories

Archives

Post Categories

Links

The Unity Application Block Feb 2008 CTP (from now on called Unity) is released on www.codeplex.com/unity. It's a lightweight dependency injection container with support for constructor, property and method call injection.

To get people started with this, I created a small example. Actually I ported my Dependency Injection with Spring.NET example from some posts ago to this release of Unity.

This sample shows how a class can get an reference to another class through its constructor.

Hit this link to download this example (VS2008!)

Let assume that ClassA needs a reference to ClassB. But ... not always to ClassB. Sometimes, for testing purposes, you would like to have another Class (called ClassB2 in this example) being giving to the constructor. Yeah, once again .... the loose coupling pattern, very usefull from the point-of-view of unittests.

So ClassA Looks like this :

 

public class ClassA { IClassB _refToB; public ClassA(IClassB b) { Console.WriteLine("Class A constructed!"); _refToB = b; } public void Test() { _refToB.DoSomething(); } }

 

Of course ClassB2 would need to implement the same interface as ClassB does otherwise the compiler is not satisfied. Note that the contructor for ClassA gets a reference to a class which implements this interface called IClassB

IClassB (the Interface for ClassB and ClassB2) looks like this :

public interface IClassB { void DoSomething(); }

Concrete implementation of ClassB :

 

class ClassB : IClassB { public ClassB() { Console.WriteLine("Class B constructed!"); } public void DoSomething() { Console.WriteLine("Class B did something"); } }

 

Concrete implementation of ClassB2 :

 

class ClassB2 : IClassB { public ClassB2() { Console.WriteLine("Class B2 constructed!"); } public void DoSomething() { Console.WriteLine("Class B2 did something"); } }

 

Ok, these are normal classes, nothing special. If we want to use them, we instantiate them with the new operator. First instantiate ClassB (or ClassB2) than instantiate ClassA giving it the freshly created ClassB (or ClassB2) in the constructor. Not ! This is too tighly coupled !

With a depency injector container (like Unity) we do not explicitly program the instantiations with the new operator. The container will do this for us. Al we have to do is : ask the container which object we want. If we ask for ClassA, the container will see that the constructor must have an instantiation of a class that implements IClassB and such a class will be instantiated for us and been given to this constructor.

Of course the container must know what concrete class (ClassB or ClassB2) it must instantiate. This is done by first mapping the Interface type to the concrete type, either by code or by configuration.

By Code :

IUnityContainer container = new UnityContainer(); container.Register(typeof(IClassB), typeof(ClassB2));

This code results in the creating of the container. Thereafter the IClassB interface type is mapped to the concrete ClassB2. From now on, if we want the have ClassA instantiated we just ask the container to get a reference by specifying the type in the generic Get method of the container.

ClassA a1; a1 = container.Get<ClassA>(); a1.Test();

So this method will lookup (through reflection) if other instantiations are needed (= dependent) and detects that it needs a class of type IClassB in the constructor. Because we already mapped ClassB2 to this interface the container is able to know that he needs to instantiate ClassB2 and thereafter needs to instantiate ClassA with the reference to ClassB2.

Result :

Class B2 constructed!
Class A constructed!
Class B2 did something

By Configuration :

In the previous way the choice of wich implementation of the interface to use is still hardcoded in the program. To be really loose coupled we need to have this decision written down in the configuration. Unity also supports this.

 

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" /> </configSections> <unity> <containers> <container> <types> <type type="TestUnity.IClassB,TestUnity" mapTo="TestUnity.ClassB2,TestUnity" /> </types> </container> </containers> </unity> </configuration>

 

In this configuration the mapping between IClassB an ClassB2 (or ClassB) is clearly specified. To read this configuration we need to use the ConfigurationManager like this :

 

IUnityContainer container = new UnityContainer(); UnityConfigurationSection section; section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers.Default.GetConfigCommand().Configure(container);

 

The rest of the code remains the same.

Bottom line : The container shields the developer away from that awfull new operater and gives him/her the needed objects based on a simple configuration.

But ... there's more (lots of ...)

For example : You can even configure the container to use singletons when instantiating the classes by setting the lifetime attribute. By default the Unity container will not use singletons unless you specify this. Like this :

 

<type type="TestUnity.ClassA,TestUnity" mapTo="TestUnity.ClassA,TestUnity" lifetime="Singleton" />

 

Result : when calling this code ....

 

ClassA a1; a1 = container.Get<ClassA>(); a1.Test(); ClassA a2; a2 = container.Get<ClassA>(); a2.Test();

 

... the constructor of ClassA and the constructor of ClassB2 (or ClassB) will only be executed once and a1 and a2 will point to the same instantiation without the need to write our own singleton logic.

 

One advice about the CTP : clearly the documantation is not in syc with this release. Please follow the discussions about this on http://www.codeplex.com/unity/Thread/List.aspx

posted on Monday, February 18, 2008 7:40 AM

Feedback

# re: Unity Application Block Feb 2008 CTP 2/18/2008 9:26 PM Bill Seddon
So I can see this could be good for testing because ClassB2 can swapped and replaced by ClassB2Test just by using a different configuration file. I can see that it might be useful to allow an application to late bind access to an IDbConnection instance (we do this using reflection at the moment) so users can select the database type at run-time.

But these are a couple of simple examples. What real world examples are there that require dependency injection? I've written a lot of code in 25 years and I'm looking to build a mental image of the problem that dependency injection will make easier.

# re: Unity Application Block Feb 2008 CTP 2/19/2008 4:23 AM McGeeky
Bill. The problem it solves is one of tight coupling. Its all too easy for an application to become a mass of tightly coupled classes and method calls - so that when you come to test it or try to isolate a bug that occured in production, it can be very difficult to create a controlled environment in which to run the particular component in question.

Dependency injection ensures that a given component can be isolated from the rest of its environment and executed independently.

A similar design pattern can be achieved with messaging; components either side of a message queue can be isolated and tested without having any knowledge of the other.

Whether the IoC pattern is of any use to you depends upon whether tight coupling in code has given you grief before.

# re: Unity Application Block Feb 2008 CTP 2/21/2008 11:35 AM Henrik Gøttig
Thanks for writing this neat example.

But I thought the pattern & practices team already had a dependency injection framework called the ObjectBuilder?

I cannot see the differences between OB and the UnityContainer..maybe you can enlighten me?

I used the ObjectBuilder to build business services (WCF) much the same way with a config file telling how to map a specific interface to a concrete class and actual injection (or point of injection) is defined with attributes.

Thought I would share a real world example of where DI can be applied:

Apart for the "normal" use cases as logging, security, etc. I also used DI for business workflows.
The same WCF services are handling many different consumers in the same business segment (travel business). A consumer is considered a different travel agent. But some of these agents have slightly different business workflows for example in respect to price calculation.
I found DI a very neat way to plug-in a new price calculation based on who the consumer is.



# re: Unity Application Block Feb 2008 CTP 4/3/2008 10:20 PM Steve Degosserie
A nice article on the subject is this one:

http://msdn2.microsoft.com/en-us/library/aa973811.aspx

It's using Castle Project's Windsor container but the principles are the same (although not sure if Unity suports Generic Specialization ...).

Steve


# re: Unity Application Block Feb 2008 CTP 4/14/2008 8:23 PM PandaWood
I suggest not using names like A, A1, B, B2, A2, _refToB!
My head is spinning and I haven't got to the end yet!
Why not class Banana, Apricot and Jam or something that doesn't remind me of electrical circuitry.

# re: Unity Application Block Feb 2008 CTP 4/15/2008 9:57 PM Maor
Is there a way to use this classes without having them referenced in my solution (more like reflection).
i would like to know that because i have been asked to check which one is better, but i still don't know how to initate a class using Unity without a reference to that class

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: