Tigraine
Daniel Hoelbling talks about programming

Named dependency lookup – Pandora

June 3rd, 2009 . by Daniel Hölbling

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.


  • http://www.tigraine.at/2009/06/04/common-service-locator-adapter-for-pandora/ Common Service Locator adapter for Pandora | Tigraine

    [...] CSLAdapter class has been there for quite some time now. But since Pandora only recently got the named lookups feature I never finished [...]