I have some code in a NinjectModule that sets up Mock bindings for all interfaces in multiple assemblies. Ninject 2 allowed me to call the From()
methods multiple times inside the Scan
lambda:
Kernel.Scan(scanner =>
{
string first = "MyAssembly";
Assembly second = Assembly.Load("MyAssembly");
scanner.From(first);
scanner.From(second);
scanner.BindWith<MockBindingGenerator>();
});
The new way in Ninject 3 doesn't allow chained From()
calls, as far as I can tell. This is the best equivalent I could come up with:
string first = "MyAssembly";
Assembly second = Assembly.Load("MyAssembly");
Kernel.Bind(x => x
.From(first)
.SelectAllInterfaces()
.BindWith<MockBindingGenerator>());
Kernel.Bind(x => x
.From(second)
.SelectAllInterfaces()
.BindWith<MockBindingGenerator>());
My new code breaks* when a single assembly is loaded multiple times, as seen above. Note that the first
and second
variables are of different types, so I can't simply combine them into a single call. My real production code has the same issue, but of course I'm not simply hard-coding the same assembly name twice.
So, how can I rewrite the above so that I can combine multiple From()
calls and only call BindWith<>
once?
Edit
* The binding code itself executes just fine. The exception occurs when I try to get an instance of an interface that exists in an assembly that was bound twice.