Convert this Castle Windsor Installer to Ninject to register all repositories

蹲街弑〆低调 提交于 2019-12-13 17:24:26

问题


I have this in my MVC2 app but I think I might move to Ninject as its becoming more popular and Castle Windsor seems a tad over complicated.

How would I do something like this in Ninject however?

Maybe Castle Windsor is more developed and I should stick with it.

container.Register(AllTypes.FromThisAssembly()
               .Where(type => type.Name.EndsWith("Repository"))
               .WithService.DefaultInterface()
               .Configure(c => c.LifeStyle.PerWebRequest));                   

回答1:


What you want to do can be done with the Ninjec.Convention extension.

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/




回答2:


You need to import the namespace Ninject.Extensions.Conventions from https://github.com/ninject/ninject.extensions.conventions Then:

Kernel.Bind(x => x
            .FromThisAssembly()
            .SelectAllClasses().EndingWith("Repository")
            .BindDefaultInterface());


来源:https://stackoverflow.com/questions/4750163/convert-this-castle-windsor-installer-to-ninject-to-register-all-repositories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!