This project is read-only.

Ninject to SimpleInjector

Jan 27, 2015 at 10:19 PM
What is the equivalent of the following Ninject IOC statements using SimpleInjector;

var kernel = new StandardKernel();
kernel.Bind(x => x.FromAssembliesMatching("*").SelectAllClasses().InheritedFrom(typeof(ICommandHandler<>)).BindAllInterfaces());


Jan 28, 2015 at 6:20 AM
Edited Jan 28, 2015 at 4:37 PM
you can use the RegisterManyForOpenGeneric for that. You can read more about this here.
Jan 29, 2015 at 1:54 PM
Edited Jan 29, 2015 at 3:00 PM

Thanks for responding to my question.

I still have an issue which I cannot seem to figure out. I am using the statement below to register all implementations of a interface;
container.RegisterManyForOpenGeneric(typeof(ICommandHandler<>),    AppDomain.CurrentDomain.GetAssemblies());
When attempting to extract the correct implementation from the container using the statement;
dynamic commandHandler = container.GetAllInstances(constructedCommandType);
(constructedCommandType is = to <ICommandHandler<SaveOriginalMessageCommand>)
The collection returned contains 0 items.

However, when I use the statement below to register each implementation with the container.
container.Register<ICommandHandler<SaveOriginalMessageCommand>, SaveOriginalMessageCommandHandler>();
I am able extract the correct implementation based on the command passed to the implementation.

Note that I am using the statement below to extract the correct type from the container.
dynamic commandHandler = container.GetInstance(constructedCommandType);
(constructedCommandType is = to <ICommandHandler<SaveOriginalMessageCommand>)
Should the statement
container.RegisterManyForOpenGeneric(typeof(ICommandHandler<>),    AppDomain.CurrentDomain.GetAssemblies());
register all implementations of an interface?


Jan 29, 2015 at 2:24 PM
Hi William,

Do you mind updating your question and format the code? The code is currently very hard to read and this makes the question not valuable to future readers that might have the same problem. You can do that by pre- and postfixing the code with three back ticks (Or by pressing CTRL + K).

Jan 29, 2015 at 2:40 PM

Simple Injector strictly separates between registration of collections and registration of single instances. Although you have many ICommandHandler<T> interfaces, you will only have one implementation for each closed version, e.g. You have exactly one implementation for SaveOriginalMessageCommand.

The default RegisterManyForOpenGeneric overload is just a fancy way of doing all those container.Register<ICommandHandler<... registrations yourself; one by one. So RegisterManyForOpenGeneric uses Register and not RegisterAll and therefore it maps to GetInstance and not to GetAllInstances.

So the following should work:
container.RegisterManyForOpenGeneric(typeof(ICommandHandler<>), AppDomain.CurrentDomain.GetAssemblies()); 
dynamic handler = container.GetInstance(constructedCommandHandlerType);