Click here to Skip to main content
11,488,877 members (76,311 online)
Click here to Skip to main content

Tagged as

Unity Messabout

, 4 Mar 2014 CPOL 7.5K 1
Rate this:
Please Sign up or sign in to vote.
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 lets have a […]

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 lets have a look at some samples 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)
    {
        this.foo = foo;
    }

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


public class CompositeFoo
{
    [InjectionConstructor]
    public CompositeFoo(IEnumerable<IFoo> foos, Func<Foo> fooFactory, 
          Func<Foo, IFooController> fooControllerFactory)
    {
        var foo = fooFactory();
        var fooController = fooControllerFactory(foo);
        fooController.Start();
    }
}


public class CompositeFooBase
{
    [InjectionConstructor]
    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 of 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 mutiple 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 it 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>();

container.RegisterType<Func<Foo>>(
    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[]>();
container.RegisterType<CompositeFoo>();
container.RegisterType<CompositeFooBase>();
container.RegisterType<CompositeFooBaseArrayCtor>();

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 alawys here is a small demo project so you can try this out for yourself

https://dl.dropboxusercontent.com/u/2600965/Blogposts/2013/UnityTest.zip


License

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

Share

About the Author

Sacha Barber
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.

Award(s)

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

  • 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 --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 5 Mar 2014
Article Copyright 2014 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid