【发布时间】:2016-12-11 12:13:18
【问题描述】:
我将 Ninject 用于 DI,它为每个请求创建 DbContext(所有服务单个),并且我通常为每个请求调用几个服务方法(因此在调用第一个服务方法后我无法处理 DbContext)。
问题是,我应该让 WallService 或 WallManager(以及其他服务和管理器)IDisposable 以及创建什么 Dispose 逻辑?
我的业务逻辑层
namespace MySite.BLL.Services
{
public class WallService
{
WallManager wallManager;
public WallService(MybContext db)
{
wallManager = new WallManager(db);
}
}
}
我的数据访问层
namespace MySite.DAL.Repositories
{
public class WallManager
{
MyDbContext db;
public WallManager(MyDbContext db)
{
this.db = db;
}
}
}
NinjectWebCommon.cs
kernel.Bind<MyDbContext>().ToSelf().InRequestScope().WithConstructorArgument<string>("MyMsSqlString");
kernel.Bind<WallService>().ToSelf().InRequestScope();
MyBaseController.cs
public class MyBaseController : Controller
{
[Inject]
public WallService WallService { get; set; }
// Other Services ..
}
【问题讨论】:
标签: c# asp.net-mvc entity-framework dependency-injection ninject