Defining what concrete classes implement what interface

Nov 13, 2012 at 9:57 AM

There is an interface called IFoo and a class called Bar that can contain 0 to many instances of IFoo.

The class that implements of IFoo depends on what customer is using our product.

How can class Bar create 1 to many instances of the desired implementation of IFoo on demand?

I haven't been able to find cases like this in the documentation or the example projects.

Nov 13, 2012 at 2:54 PM

I believe you're asking how can you store many instances of a specific interface in the IoC DI container, correct?  This is what the Items collection is for.

// create two items that derive from the same interface
IFoo a = new FooA();
IFoo b = new FooB();

// put them in the container. 
// use a unique name in case we want to get one by name later
// you can omit the name and IoC will give it one if you wish
RootWorkItem.Items.Add(a, "uidA");
RootWorkItem.Items.Add(b, "uidB");

// this will get just one item
var testA = RootWorkItem.Items.Get("uidA");

// this will get all items of a specific type
var foos = RootWorkItem.Items.Get();