I have a class that has dependencies that I’ve wired up with Ninject.
public interface IFoo {}
public class MyObject {
[Inject]
IFoo myfoo;
}
In the real implementation I’m using property injection, but just to quickly illustrate, I’ll inject on the field. As I understand, instead of newing instances of MyObject, in order to get the dependencies to be properly injected, I need to use
kernel.Get<MyObject>()
The thing I’m stumbling on however is that MyObject will only be used in the context of a class library. The intention is for end applications to create their own modules and pass it into a kernel instance to hydrate. Given that, what’s generally the most pragmatic way to approach surfacing a generic instance of a Ninject kernel to my class library so that instances of MyObject (and other similar cases) can be hydrated?
My first inclination is some sort of factory that internalizes a singleton kernel–which the applications themselves have to hydrate/initialize by loading up a module.
So in RandomService.cs
var myKernel = NinjaFactory.Unleash();
var myobj = myKernel.Get<MyObject>();
myobj.foo();
Before I go too far down this path though, I need to do a sanity check to make sure that the thinking is sound or that there isn’t some obvious other thing that I’m missing. I’m obviously new to IoC and feel like I grok the basics, but not necessarily the best real world ways to use it.
I’m not sure that I understand all the details in your question, but I as far as I understand, you are asking how you can externalize the dependency on Ninject.
It is possible to write DI-friendly libraries without ever referencing any particular DI Container (Ninject or anything else). This leaves the consumer of the library open to choose the DI Container of his or her liking (or not use a container at all). This approach is much preferred because it provides a greater degree of freedom.
However, you should really favor Constructor Injection over Property Injection. Although Property Injection seems deceptively simple to implement, it’s actually quite difficult to get right.
One of the great advantages of Constructor Injection is that you don’t need to put any attributes on the constructor because it structurally already carries all the information needed for a DI Container to correctly wire it up.