将Ninject 3.0用于给定代码

时间:2012-04-12 00:26:58

标签: c# .net dependency-injection ninject autofac

我正在尝试将此代码从DI autofac重写为Ninject 3.0.0.15。

我遇到方法问题:AsImplementedInterfaces(...)AsClosedTypesOf(...)

builder.RegisterAssemblyTypes(typeof(CategoryRepository).Assembly)
   .Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces()
   .InstancePerHttpRequest();

Assembly services = Assembly.Load("EFMVC.Domain");

builder.RegisterAssemblyTypes(services)
    .AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerHttpRequest();

builder.RegisterAssemblyTypes(services);

该代码是非常好的演示Web应用程序的一部分,用于演示ASP.NET MVC 4和EF 4.3 Code First,Windows Azure和架构实践:EFMVC

感谢您的回答!

1 个答案:

答案 0 :(得分:3)

使用Ninject.Extensions.Conventions V3.0.0

我认为这就是你要找的东西:

kernel.Bind(x => x
      .FromAssemblyContaining<CategoryRepository>()
      .SelectAllClasses().EndingWith("Repository")
      .BindAllInterfaces()
      .Configure(b => b.InRequestScope());

kernel.Bind(x => x
      .From("EFMVC.Domain")
      .SelectAllClasses().InheritedFromAny(typeof(ICommandHandler<>))
      .BindAllInterfaces()
      .Configure(b => b.InRequestScope());