我有这个在我的MVC2应用程序,但我认为其越来越受欢迎我可能会移动到Ninject和温莎城堡似乎一点点过于复杂。
我怎么会做这样的事情在Ninject不过?
也许温莎城堡是比较发达的,我应该坚持下去。
container.Register(AllTypes.FromThisAssembly()
.Where(type => type.Name.EndsWith("Repository"))
.WithService.DefaultInterface()
.Configure(c => c.LifeStyle.PerWebRequest));
你想要做什么可以与Ninjec.Convention扩展来完成。
https://github.com/ninject/ninject.extensions.conventions
http://innovatian.com/2009/09/conventions-based-binding-with-ninject-2-0-2/
http://innovatian.com/2010/02/ninject-extensions-conventions-preview/
你需要导入来自命名空间Ninject.Extensions.Conventions https://github.com/ninject/ninject.extensions.conventions然后:
Kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses().EndingWith("Repository")
.BindDefaultInterface());
文章来源: Convert this Castle Windsor Installer to Ninject to register all repositories