我在我的解決方案中有兩個項目...一個域項目和MVC3 web項目(例如MyApp.Domain和MyApp.Web)。以前,當使用Ninject.Extensions.Conventions版本。 2,我可以在NinjectMVC3.cs文件中使用以下語句,並且在我的解決方案(web和域)中都需要依賴項被正確注入(例如IFoo自動綁定到Foo)。基於約定的依賴注入與Ninject 3.0.0
kernel.Scan(x =>
{
x.FromAssembliesMatching("*");
x.BindWith<DefaultBindingGenerator>();
});
我剛剛升級到3.0.0 Ninject(發行前)和Ninject.Extensions.Conventions 3.0.0(另一個預發行版),但基於約束力的公約,語法發生了變化。我發現我可以在新版本中使用以下語句,但它只會自動綁定MyApp.Web中的基於約定的接口,而不是MyApp.Domain。以前的版本在整個應用程序中綁定接口。
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses()
.BindToAllInterfaces());
任何線索如何配置新的Ninject版本的基於約定的綁定?我認爲它與指定程序集有關,但我嘗試過使用FromAssembliesMatching("*")
,然後它就會失敗。
- 編輯,以顯示RegisterServices方法我exisiting代碼: -
private static void RegisterServices(IKernel kernel)
{
// This code used to work with v.2 of Ninject.Extensions.Conventions
// kernel.Scan(x =>
// {
// x.FromAssembliesMatching("*");
// x.BindWith<DefaultBindingGenerator>();
// });
// This is the new v3 code that automatically injects dependencies but only for interfaces in MyApp.Web, not MyApp.Domain
kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().BindToAllInterfaces());
// I tried this code, but it throws "Error activating IDependencyResolver" at "bootstrapper.Initialize(CreateKernel)"
// kernel.Bind(x => x.FromAssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath).SelectAllClasses().BindToAllInterfaces());
// These are dependencies in MyApp.Web that ARE being bound properly by the current configuration
// kernel.Bind<IMemberQueries>().To<MemberQueries>();
// kernel.Bind<IGrantApplicationQueries>().To<GrantApplicationQueries>();
// kernel.Bind<IMailController>().To<MailController>();
// These are dependencies in MyApp.Domain that ARE NOT being bound properly by the current configuration, so I have to declare them manually
// They used to be injected automatically with version 2 of the conventions extention
kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
kernel.Bind<IMemberServices>().To<MemberServices>();
kernel.Bind<IGrantApplicationServices>().To<GrantApplicationServices>();
// These are dependencies that SHOULD NOT be bound by convention as they require a different scope or have unique naming
kernel.Bind(typeof(EfDbContext)).ToSelf().InRequestScope();
kernel.Bind<IConfigurationProvider>().To<WebConfigConfigurationProvider>().InSingletonScope();
kernel.Bind<IAuthorizationProvider>().To<MyAppAuthorizationProvider>();
kernel.Bind<IPrincipal>().ToMethod(ctx => HttpContext.Current.User).InRequestScope();
kernel.Bind<IGrantApplicationDocumentServices>().To<MySpecialNameGrantAplicationDocumentServices>().InRequestScope();
}
你看着[維基](https://開頭的github的.com/ninject/ninject.extensions.conventions /維基)。如果不是的話,你能否介紹一下哪一點對你沒有意義? – 2012-03-22 20:38:12
魯本,我看過維基。沒有道理的是,用舊的代碼'x.FromAssembliesMatching(「*」)'工作並綁定MyApp.Web和MyApp.Domain中的所有接口。但是,對於新的3.0.0,該語法不起作用。我找到的最接近的是'x.FromThisAssembly()',但它只綁定MyApp.Web中的接口(因爲這是注入發生的地方)。它不會自動綁定MyApp.Domain中的接口。 – bigmac 2012-03-22 22:11:41
查看代碼,你總是可以自己獲取程序集(通過AppDomain的程序集列表或一些opther機制),並明確提供列表。除此之外,我建議閱讀來源 - 它只是不大。 https://github.com/ninject/ninject.extensions.conventions/blob/master/src/Ninject.Extensions.Conventions/BindingBuilder/AssemblyFinder.cs – 2012-03-23 10:15:53