Skip to content

Adds ability to automatically discover injectable classes that are marked with simple attribute

License

Notifications You must be signed in to change notification settings

EvilVir/Ninject.Extensions.AutoBinding

Repository files navigation

Ninject.Extensions.AutoBinding NuGet Version

Adds ability to automatically discover injectable classes that are marked with simple attribute

Basic usage

Marking classes for autoinjection

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
{
}

Injecting

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)
	{
	}
}

Making this all running

When configuring your container (IKernel) just call AutoBinding() extension method like so:

using Ninject.Extensions.AutoBinding;

IKernel container = new StandardKernel().AutoBinding();

Scopes

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
{
}

Profiles

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

Ninject.Extensions.AutoBinding.Web NuGet Version

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; :)

About

Adds ability to automatically discover injectable classes that are marked with simple attribute

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages