dependency-injection - Ninject 3.0.0 基于约定的依赖注入(inject)

标签 dependency-injection ninject ninject-extensions

我的解决方案中有两个项目......一个域项目和 MVC3 Web 项目(例如 MyApp.Domain 和 MyApp.Web)。以前,当使用 Ninject.Extensions.Conventions 版本。 2,我能够在 NinjectMVC3.cs 文件中使用以下语句,并且整个解决方案(Web 和域)所需的依赖项都被正确注入(inject)(例如 IFoo 自动绑定(bind)到 Foo)。

kernel.Scan(x =>
{
  x.FromAssembliesMatching("*");
  x.BindWith<DefaultBindingGenerator>();
});

我刚刚升级到 Ninject 3.0.0(预发布)和 Ninject.Extensions.Conventions 3.0.0(另一个预发布),但是基于约定的绑定(bind)的语法已经改变。我发现我可以在新版本中使用以下语句,但它只会在 MyApp.Web 中自动绑定(bind)基于约定的接口(interface),而不是在 MyApp.Domain 中。以前的版本绑定(bind)了整个应用程序的接口(interface)。
kernel.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses()
    .BindToAllInterfaces());

知道如何使用新的 Ninject 版本配置基于约定的绑定(bind)吗?我认为它与指定程序集有关,但我尝试使用 FromAssembliesMatching("*")然后一切都失败了。

-- 编辑以在 RegisterServices 方法中显示我现有的代码: --
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();
}

最佳答案

等效的是:

kernel.Bind(x => x
    .FromAssembliesMatching("*")
    .SelectAllClasses()
    .BindDefaultInterface());

关于dependency-injection - Ninject 3.0.0 基于约定的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9824863/

相关文章:

python - 如何通过构造函数实现依赖注入(inject) - Python

dependency-injection - 是否存在依赖注入(inject)/控制反转的分层方法

c# - StructureMap 到 Ninject 的转换

c# - 使用 ninject.extensions.conventions 多次绑定(bind)服务

c# - 将具体实例注入(inject) Ninject 解析器

Ninject 使用 BindToFactory 按约定绑定(bind)到具有类 T 的工厂接口(interface)

symfony - 从服务获取 Symfony 基本 URL?

c# - 有状态服务的依赖注入(inject)

asp.net-mvc - 与 Entity Framework 一起使用的没有存储库模式的依赖注入(inject)

c# - 无法让 Ninject.Extensions.Conventions 工作