Tigraine

Daniel Hoelbling-Inzko talks about programming

Named dependency lookup – Pandora

Posted by Daniel Hölbling on June 3, 2009

One rather essential feature for a DI container is to be able to lookup components by some key. For example retrieving a specific IController class based on the controller name specified by the request.

So the usual use case for the above looks like this:

var store = new ComponentStore();
store.Add<IRepository, MemoryRepository>("memory.repository");
store.Add<IRepository, SqlRepository>("db.repository");
var container = new PandoraContainer(store);

var service = container.Resolve<IRepository>("db.repository");

Now, there is another thing too: What if I want one controller to use one special repository out of the registered ones:

var store = new ComponentStore();
store.Add<IRepository, SqlRepository>("db.repository");
store.Add<IRepository, MemoryRepository>("memory.repository");
store.Add<Controller, Controller>()
    .Parameters("repository").Eq("memory.repository");
var container = new PandoraContainer(store);

var controller = container.Resolve<Controller>();

Well, yes Pandora can do those kinds of things too.

If you know Windsor’s Fluent interface .Parameters().Eq() may sound familiar to you. That’s intentionally. I like the Windsor Fluent syntax.

What I really underestimated was how to do the fluent interface. It’s not terribly hard, but it’s takes some tinkering ;).

You can find the source to Pandora at the project website on Bitbucket.

comments powered by Disqus

My Photography business

Projects

dynamic css for .NET

Archives

more