-
Notifications
You must be signed in to change notification settings - Fork 29
Overview
bartelink edited this page Mar 26, 2012
·
9 revisions
First of all you have to add an assembly reference to Ninject.Extensions.Conventions and a namespace reference for Ninject.Extensions.Conventions.
Having referenced the Ninject Conventions Extension, you can begin to specify convention based registrations such as this:
kernel.Bind( x => x
.FromThisAssembly() // 1
.SelectAllClasses().InNamespaceOf<MyService>() // 2
.BindToAllInterfaces() // 3
.Configure(b => b.InSingletonScope())); // 4
Each registration consists of four elements:-
- Projecting Assemblies from within which the components to be bound are to be selected
-
Projecting Components from within those assemblies which are to supply services (the
To<X>()bit in explicit bindings terms) -
Projecting Services to Bind for each projected component (the
Bind<X>()bit in explicit bindings terms) -
Configuring Bindings for each projected service(the
.In...,.With... etc. bits in explicit bindings terms)
The first two portions can be repeated in case the criteria for the selection of the components differs across assembly groupings.
kernel.Bind( x => x
.FromThisAssembly() // 1a
.SelectAllClasses().InNamespaceOf<MyService>() // 2a
.Join.FromAssembliesInPath(".") // 1b
.SelectAllClasses().InheritedFrom<IService>() // 2b
.BindToAllInterfaces() // 3
.Configure(b => b.InSingletonScope())); // 4
Continue Reading: Projecting Assemblies
Back: What is configuration by convention?