Getting all instances of a particular type

162 views
Skip to first unread message

Romain Verdier

unread,
Apr 2, 2009, 8:47:49 AM4/2/09
to ninject
Hello,

Giving this model:

public interface IWorker { void DoWork(); }

public interface IWorkerA : IWorker {}
public interface IWorkerB : IWorker {}
public interface IWorkerC : IWorkerB {}

public class WorkerA : IWorkerA {}
public class WorkerB : IWorkerB {}
public class WorkerC : IWorkerC {}

And theses bindings:

Bind<IWorkerA>().To<WorkerA>();
Bind<IWorkerB>().To<WorkerB>();
Bind<IWorkerC>().To<WorkerC>();

Is it possible to to something like:

var workers = kernel.GetAll<IWorker>(); // I want to get 3 instances
foreach(var worker in workers)
worker.DoWork();

That is, to get all instances of a specified type, even if they were
not explicitly bound to this type. That doesn't seems to be the
purpose of the actual GetAll method.

Thank you,

Romain

Dave

unread,
Apr 2, 2009, 1:08:34 PM4/2/09
to ninject
> Is it possible to to something like:
>
> var workers = kernel.GetAll<IWorker>(); // I want to get 3 instances
> foreach(var worker in workers)
>    worker.DoWork();
>
Ninject 2 has Common Service Locator support. Nate was even kind
enough to write an implementation for the ServiceLocator from
Microsoft (Best Practices).
At Github (http://github.com/enkari/ninject/tree/master) you can find
the latest version of Ninject 2.

Site node: Don't forget to always add a 'using Ninject' statement as
the Get* methods are implemented as extension methods.
StandardModule has been renamed to NinjectModule. If you have an older
version (like the one Nate published on his blog you need Module)

Regards,
Dave

Romain Verdier

unread,
Apr 3, 2009, 2:48:15 PM4/3/09
to ninject
Hi Dave,

Thank you for the response. I'm aware of the GetAll extension method
that came in version 2.

However, if you use the bindings and types I described in my previous
mail:

Bind<IWorkerA>().To<WorkerA>();
Bind<IWorkerB>().To<WorkerB>();
Bind<IWorkerC>().To<WorkerC>();

Then:

kernel.GetAll<IWorker>()

won't return anything. That's my trouble : I would like to get 3
instances back : a WorkerA, a WorkerB, and a WorkerC :)

Thank you,

Romain

CandiQ

unread,
Apr 5, 2009, 4:22:42 AM4/5/09
to nin...@googlegroups.com
I have the same problem in such situation.
It seems like the kernel will return nothing if you didn't bind the certain type.
Is there any way to make it work?

Thanks


2009/4/4 Romain Verdier <romain....@gmail.com>

Nate Kohari

unread,
Apr 5, 2009, 9:58:29 AM4/5/09
to nin...@googlegroups.com
Multi-resolution (via GetAll) is currently not polymorphic. That means that it will only consider bindings from the exact interface you specify. If you do this:

kernel.Bind<IWorker>().To<WorkerA>();
kernel.Bind<IWorker>().To<WorkerB>();
kernel.Bind<IWorker>().To<WorkerC>();

And then:

kernel.GetAll<IWorker>();

It will return 3 items. However, even if IWorkerA, IWorkerB, and IWorkerC implement IWorker, Ninject will not look at bindings from IWorkerA to WorkerA when you ask for IWorker.


Thanks,
Nate

Miguel Madero

unread,
Apr 7, 2009, 10:22:59 AM4/7/09
to nin...@googlegroups.com
Makes a lot of sense and I think this is the right approach. Otherwise we might have unexpected results. We could simply create two bindings and it will work :)
Reply all
Reply to author
Forward
0 new messages