Adds ability to automatically discover injectable classes that are marked with simple attribute
Binding to self:
using Ninject.Extensions.AutoBinding;
[Injectable]
public class MyService : IService
{
}
Binding to interface:
using Ninject.Extensions.AutoBinding;
[Injectable(Interface = typeof(IService)]
public class MyService : IService
{
}
Binding to multiple interfaces:
using Ninject.Extensions.AutoBinding;
[Injectable(Interface = typeof(IService)]
[Injectable(Interface = typeof(IAnotherService)]
public class MyService : IService, IAnotherService
{
}
Just use standard [Inject]
attribute from Ninject:
using Ninject;
public class MyController
{
[Inject] // Property injection
public IService InjectedService { get; set; }
[Inject] // Constructor injection
public MyController(IAnotherService anotherInjectedService)
{
}
}
When configuring your container (IKernel
) just call AutoBinding()
extension method like so:
using Ninject.Extensions.AutoBinding;
IKernel container = new StandardKernel().AutoBinding();
You can controll in which scope your injectable will be bound (check this link for more info about Ninject scopes). Just use Scope parameter and provide one of InjectionScope
enum values. Default scope is Transient
.
using Ninject.Extensions.AutoBinding;
[Injectable(Interface = typeof(IService), Scope = InjectionScope.Singleton]
public class MyService : IService
{
}
Sometimes you might want to use different implementation depending on execution environment conditions. For example you'll use sandboxed WebService API during development and production one on production server. With this extension you can easly configure which implementation of your classes will be injected. Just use Profiles
property for that and then provide list of profiles to AutoBinding(params string[])
extension method.
using Ninject.Extensions.AutoBinding;
[Injectable(Profiles = new string[]{ "DEV_MODE" }, Interface = typeof(IWebServiceApi))]
public class DevModeSandboxedService : IWebServiceApi
{
}
[Injectable(Profiles = new string[]{ "PRODUCTION_MODE" }, Interface = typeof(IWebServiceApi))]
public class ProductionModeService : IWebServiceApi
{
}
IKernel container = new StandardKernel().AutoBinding("DEV_MODE");
IWebServiceApi apiClient = container.Get<IWebServiceApi>(); // Will return DevModeSandboxedService implementation
You can also exclude classes from certain profiles. Let's change previous example a little bit:
using Ninject.Extensions.AutoBinding;
[Injectable(Profiles = new string[]{ "DEV_MODE" }, Interface = typeof(IWebServiceApi))]
public class DevModeSandboxedService : IWebServiceApi
{
}
[Injectable(ExcludeInProfiles = new string[]{ "DEV_MODE" }, Interface = typeof(IWebServiceApi))] // Here instead of providing explicit profile name you can exclude that injectable from DEV_MODE profile.
public class ProductionModeService : IWebServiceApi
{
}
IKernel container = new StandardKernel().AutoBinding("DEV_MODE");
IWebServiceApi apiClient = container.Get<IWebServiceApi>(); // Will still return DevModeSandboxedService implementation
If you're working on Web Application project use this nuget, instead of main one, to be able to use Ninject.Web.Common's RequestScope when configuring your injectables.
using Ninject.Extensions.AutoBinding.Web;
[Injectable(Interface = typeof(IService), Scope = InjectionScope.Request]
public class MyService : IService
{
}
Everything else remains the same as documented above as Web project extends base one - just remember to use using Ninject.Extensions.AutoBinding.Web;
instead of using Ninject.Extensions.AutoBinding;
:)