您好我正在使用Ninject.MVC Nuget包与我的MVC3应用程序,我有一些构造函数注入的当前绑定设置。Ninject.MVC构造函数注入其中注入对象的构造函数采用参数
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUnitOfWork>().To<ERSUnitOfWork>();
kernel.Bind<IRepository<Recipe>>().To<GenericRepository<Recipe>>();
}
我控制器例子如下:
public class RecipesController : Controller
{
private readonly IUnitOfWork unitOfWork;
private readonly ERSDbContext context;
private readonly IRepository<Recipe> recipeRepository;
public RecipesController(IUnitOfWork unitOfWork, IRepository<Recipe> recipeRepository)
{
this.context = new ERSDbContext();
this.unitOfWork = unitOfWork;
this.recipeRepository = recipeRepository;
}
}
我想从控制器中删除私有的DbContext财产和通过新的ERSDbContext()来ERSUnitOfWork和GenericRepository的构造为部分Ninject正在执行的构造函数注入,但最好在控制器内部保持ERSDbContext类的初始化?
任何帮助你如何做到这一点将不胜感激。谢谢
我有点希望它不需要我的NinjectWebCommon类必须创建DbContext,我想要在控制器中初始化。
DbContexts创建起来非常便宜。没有“初始化”。该对象已创建,但未打开任何连接或构建复杂结构。当第一次调用DbContext时,DbContext使用延迟初始化。 –
相关:http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why。 – Steven
我以前见过这个相关的帖子,我看到了像我的一个简单项目的方法注入的例子。我的问题是知道如何将其实现到我当前的示例中。 – Pricey