Tuesday, September 13, 2011

Howto create a simple IOC container

I'm using StructureMap in my current project and it works pretty well.
But how difficult could it be to make a simple IOC container. So I set out to create my own implementation and it proved to be quite easy to get the basics down.

How to setup the container.
var c = new Container();
c.For<IFoo>().Use<Bar>();

In case you need more controll over the creation of the class creation, I have added the posiblitity to use lamdas.
c.For<IFoo>().Use(() => new Bar1("someArgument"));
c.For<IFoo>().Use(ctx => new Bar2(ctx.Get<ISomeinterface>()));

How to resolve
var foo = c.Get<IFoo>();

The road ahead could be to handle scope/lifecycle.

The complete code can be found on GitHub

---
Share:

0 kommentarer:

Post a Comment