Click here to Skip to main content
15,610,013 members
Articles / All Topics
Technical Blog
Posted 4 Mar 2014

Tagged as


1 bookmarked

Unity Messabout

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Mar 2014CPOL
Unity messabout

Untitled 1

In the past, I have used a few IOC containers such as:

  • Castle
  • MEF

Where I work now, they are using Unity which is not something I have looked at in earnest before, so I decided to have a quick look at it, and post a small demo project. So let's take a look at some sample code, shall we. So here are the demo classes that I will inject/inject into.

public class Foo
    public int Age { get; set; }

public interface IFooController
    void Start();

public class FooController : IFooController
    private readonly Foo foo;

    public FooController(Foo foo)
    { = foo;

    public void Start()
        Console.WriteLine(string.Format("Foo.Age={0}", foo.Age));

public class CompositeFoo
    public CompositeFoo(IEnumerable<IFoo> foos, Func<Foo> fooFactory, 
          Func<Foo, IFooController> fooControllerFactory)
        var foo = fooFactory();
        var fooController = fooControllerFactory(foo);

public class CompositeFooBase
    public CompositeFooBase(IEnumerable<FooBase> foos)


public class CompositeFooBaseArrayCtor
    public CompositeFooBaseArrayCtor(FooBase[] foos)


public interface IFoo

public abstract class FooBase

public class FooI1 : IFoo
public class FooI2 : IFoo

public class FooI3 : IFoo


public class FooB1 : FooBase

public class FooB2 : FooBase

public class FooB3 : FooBase


That should be enough to give us enough variety to show off a few of the Unity application blocks tricks, such as:

  1. How to use named instances
  2. How to resolve single instances
  3. How to resolve multiple instances
  4. How to resolve multiple instances using IEnumerable
  5. How to pick the constructor to inject
  6. How to inject a Func
  7. How to inject a Func and override its own constructor arguments

So those are the areas I have a code sample for, I think the best way is to just list the code, so without further ado, here is the code that demonstrates the points above:

UnityContainer container = new UnityContainer();
container.RegisterInstance<FooBase>("NonDefaultFooBaseInstanceB1",new FooB1());
container.RegisterInstance<FooBase>("NonDefaultFooBaseInstanceB2", new FooB2());
container.RegisterType<FooBase, FooB1>("NonDefaultFooB1");
container.RegisterType<FooBase, FooB2>("NonDefaultFooB2");
container.RegisterType<IFoo, FooI1>();
container.RegisterType<IFoo, FooI2>("NonDefaultIFoo2");
container.RegisterType<IFoo, FooI3>("NonDefaultIFoo3");

container.RegisterInstance<Foo>("NamedFoo", new Foo() { Age = 23});
container.RegisterType<IFooController, FooController>();

    new InjectionFactory(c => new Func<Foo>(()=> c.Resolve<Foo>("NamedFoo"))));

container.RegisterType<Func<Foo, IFooController>>(
    new InjectionFactory(c =>
        new Func<Foo, IFooController>(
            foo => c.Resolve<FooController>(new DependencyOverride<Foo>(foo)))));

//can just register this to get around the fact that unity 
//doesn't like IEnumerable in Ctor out of the box
container.RegisterType<IEnumerable<IFoo>, IFoo[]>();

var x = container.RegisterType<CompositeFooBase>
        (new InjectionConstructor(container.ResolveAll<FooBase>()));

var fooBases = container.ResolveAll<FooBase>().ToList();
var iFoos = container.ResolveAll<IFoo>().ToList();
var iFoo = container.Resolve<IFoo>();
var iFooNamed = container.Resolve<IFoo>("NonDefaultIFoo2");
var compositeFoo = container.Resolve<CompositeFoo>();
var compositeFooBase = container.Resolve<CompositeFooBase>();
var compositFooBaseArray = container.Resolve<CompositeFooBaseArrayCtor>();

As always, here is a small demo project so you can try this out for yourself:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.


I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

-- There are no messages in this forum --