c# - 统一处理数据库上下文的正确方法
问题描述
我正在使用统一和依赖注入,目前,我对连接的处理有点困惑。
我将举一个例子,希望我能正确解释:)
我有一个使用服务的控制器:
public class CompaniesController : IDatabaseController
{
private readonly ICompaniesService _companiesService;
public CompaniesController(ICompaniesService companiesService)
{
_companiesService = companiesService;
}
}
并且该服务注册到 UnityConfig 为:
container.RegisterType<ICompaniesService, CompaniesService>(new HierarchicalLifetimeManager());
*** 我读到如果我使用 IDisposable,那么 HierarchicalLifetimeManager 是强制性的。
实现接口的服务(我知道也可以注入数据库连接,但由于超出问题范围的原因请忽略它)是这样的:
public class CompaniesService : ICompaniesService
{
private readonly DatabaseContext _db = Helpers.GetDatabaseContextForRequest();
/// <summary>
/// Returns all employee of a company
/// </summary>
/// <param name="company_id">The id of the company</param>
/// <returns>A collection of EmployeeDAP</returns>
public IEnumerable<EmployeeDAP> GetCompanyEmployees(int company_id)
{
var employees = CompaniesRepository.GetCompanyEmployees(company_id);
return employees;
}
}
问题来了。我是否也应该实现服务的 IDisposable 接口并处理数据库连接,还是 GC 来清理混乱?
如果我必须手动处理连接,我应该使用 Dispose Pattern 还是
public void Dispose()
{
((IDisposable)_db).Dispose();
}
足够了?
提前致谢
更新:
辅助方法如下:
try
{
DatabaseContext db = (DatabaseContext)getRequestValue(name);
if (db == null || !db.Database.Exists())
{
db = new DatabaseContext();
setDatabaseContextForRequest(db, name);
}
return db;
}
catch (Exception)
{
return new DatabaseContext();
}
其中 new DatabaseContext 继承自 EF 的 DbContext。
解决方案
我认为让服务来处理DatabaseContext
是给服务过多的责任。
我会将DatabaseContext
配置从Helpers
类移动到 Unity,使用PerRequestLifetimeManager
.
使用 Unity 作为生命周期管理器,您将获得DatabaseContext
跨越 HTTP 请求的 a,将 HTTP 请求生命周期近似为DatabaseContext
.
这种方法将允许您避免让服务拥有DatabaseContext
生命周期的所有权,将所有清理逻辑和所有权保留在 Unity 容器中。此外,您将能够DatabaseContext
在单个请求中在服务之间安全地共享实例,因为DatabaseContext
它仅在请求结束时被释放。
最后,请记住,一旦您丢弃了某些东西,您就无法再次使用它,因此您必须改进Helpers.GetDatabaseContextForRequest()
以重新创建DatabaseContext
它是否已被丢弃。
推荐阅读
- python - 如何将额外(计算)列添加到熊猫数据透视表
- excel - 对另一个表中的一组行执行“if”的公式
- javascript - 无法获取 /index.html
- android - 地理编码器对于 searchview 来说太慢了
- process - 在Linux中,如何在进行系统调用时检索进程的nice值和当前进程所有者的进程数?
- c# - 如何将“2013:11:21 15:11:04+02:00”解析为 DateTime
- node.js - 在 mac 上安装 XPC 连接时如何解决错误,以便我可以破解我的 furby?
- openpyxl - 当 x 为 None 时,条件(x 不是 None)失败
- python - 如何在行尾加括号
- typeorm - 如何使用 typeorm 编写下面的 psql 查询