Reputation: 52942
I have a class library that internally I have structured to use dependency injection (ninject). It's used by a variety of projects; MVC, windows services, REST apis etc.
I simply have a class like so:
public class KittenInvestigator
{
[Inject]
public IKittenDetectorDevice KittenDetectorDevice { get; set; }
...
}
Googling the issue to death I have found it to be impossible to initialise ninject in a class library as there is no 'startup' method or hook.
I'm looking for a workaround. There are tons of posts about the issue but I can't find any solutions or workarounds. Any ideas?
Some things I have considered:
Put the bindings into the project referencing the library. This is super bad since the injection is used internally in the library such that external components should never know about it - they'd have to magically know to add these bindings or the library won't work.
Some kind of reflection, making my ninject code in the main project scan all assemblies for ninject modules and initialise them.
A static initialise method in the class library that must be called by the main project that will set up the ninject stuff (seems tacky).
Some other clever design.
Upvotes: 0
Views: 174
Reputation: 13233
You could use Fody.ModuleInit to bootstrap the library automatically when its assembly is loaded. But i would not recommend it and instead do either of:
Upvotes: 0
Reputation: 4895
You could have a startup method, create a static class called bootstrapper and do the composition of your DI container in the static constructor.
Upvotes: 1