First of all, I struggled with this problem for a long time, see AUTOFAC powerful automatic registration function, I feel ninject weak explosion, but ninject with the property registration, feel very convenient, so still tangled ninject
The traditional way of binding
public class Bllmodule:ninjectmodule {public override void Load () { bind<itb_sys_modulebll> ( ). To<tb_sys_modulebll> (). Insingletonscope (); Bind<itb_sys_itembll> (). To<tb_sys_itembll> (). Insingletonscope (); Bind<itb_item_companybll> (). To<tb_item_companybll> (). Insingletonscope (); Bind<itb_item_departmentbll> (). To<tb_item_departmentbll> (). Insingletonscope (); Bind<itb_item_userbll> (). To<tb_item_userbll> (). Insingletonscope (); Bind<itb_item_rolebll> (). To<tb_item_rolebll> (). Insingletonscope (); } }
An interface corresponds to an entity class, too laborious.
Automatic Binding method:
/// <summary> ///Load your modules or register your services here! /// </summary> /// <param name= "kernel" >The kernel.</param> Private Static voidregisterservices (Ikernel kernel) {kernel. Bind (Scanner= = Scanner. From ("IBLL","BLL") . Select (Isservicetype). Binddefaultinterface (). Configure (Binding=binding. Insingletonscope ())); Kernel. Bind (Scanner= = Scanner. From ("Idao","Dao") . Select (Isservicetype). Binddefaultinterface (). Configure (Binding=binding. Insingletonscope ())); } Private Static BOOLisservicetype (Type type) {returnType. IsClass && type. Getinterfaces (). Any (Intface = Intface. Name = ="I"+type. Name); }
This will be OK, not the manual stick, all the interfaces are registered
Implementation method:
1. vs2010 NuGet Installation NINJECT.MVC3
2. vs2010 NuGet Installation Ninject.extensions
Well, that's it. It's too damn easy.
Ninject Automatic Registration