问题
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