Custom providers
Providers are specialized factory classes that Ninject uses in order to instantiate resolved types. Whenever we bind a service type to a component, we are implicitly associating that service type to a provider that can generate instances of that component. This hidden provider is a generic factory, which can create instances of every given type, and is called
StandardProvider
. Although we can often rely on StandardProvider
without having to bother about what it does behind the scenes, Ninject also allows us to create and register our custom providers just in case we need to customize the activation process as follows:
Bind<IService>().ToProvider<MyService>(); public class MyServiceProvider : Provider<MyService> { protected override MyService CreateInstance(IContext context) { return new MyService(); } }
Although extending the
Provider<T>
class is the recommended way to create a custom provider, implementing the IProvider
interface...