通过依赖注入进行对象处理
我创建了一个存储库类,我想在代码隐藏页面中使用它。我在代码隐藏页面中使用构造函数注入来实例化存储库。
存储库类:
BritanniaPremierEntities PBEntities = new BritanniaPremierEntities();
public IQueryable<TradeRoutes> GetRoutes()
{
var routes = PBEntities.TradeRoutes.OrderBy(c => c.ConsignmentDate);
return routes;
}
public IQueryable<TradeRoutes> GetExpiredRoutes()
{
var routes = PBEntities.TradeRoutes.Where(
c => c.ConsignmentDate <= System.DateTime.Now);
return routes;
}
页面后面的代码
private IRepository repos;
public Admin_TradeRoutesAdmin()
: this(new Repository())
{
}
public Admin_TradeRoutesAdmin(IRepository repos)
{
this.repos = repos;
}
public IQueryable GetTradeRoutes()
{
// call repository method
return repos.GetRoutes();
}
这是我有点困惑的地方。我应该如何确保存储库被正确处置?例如,我无法将存储库调用包装在代码隐藏页面中的 using 语句中,从而利用存储库中的 dispose 方法。
I have created a repository class that I want to use in a code behind page. I'm using constructor injection in the code behind page to instantiate the repository.
Repository class:
BritanniaPremierEntities PBEntities = new BritanniaPremierEntities();
public IQueryable<TradeRoutes> GetRoutes()
{
var routes = PBEntities.TradeRoutes.OrderBy(c => c.ConsignmentDate);
return routes;
}
public IQueryable<TradeRoutes> GetExpiredRoutes()
{
var routes = PBEntities.TradeRoutes.Where(
c => c.ConsignmentDate <= System.DateTime.Now);
return routes;
}
Code behind page
private IRepository repos;
public Admin_TradeRoutesAdmin()
: this(new Repository())
{
}
public Admin_TradeRoutesAdmin(IRepository repos)
{
this.repos = repos;
}
public IQueryable GetTradeRoutes()
{
// call repository method
return repos.GetRoutes();
}
Here is where I get a little confused. How should I ensure the repository is disposed correctly? For instance, I'm unable to wrap the repository calls in using statements in the code behind page, thus making use of the dispose method in the repository.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该采用Register Resolve Release 模式。
具体来说,您应该记住始终发布您解决的问题。作曲家有责任跟踪是否应释放依赖项。这并非微不足道,因为它取决于不同的因素:
这是一项非常复杂的任务,您应该使用适当的 DI 容器来完成这项工作。
但是,请记住,这最终取决于您的 DI 容器是否支持停用。例如,Castle Windsor 可以,而 StructureMap 则不能。
You should employ the Register Resolve Release pattern.
Specifically you should remember to always Release what you Resolve. It's the responsibility of the Composer to keep track of whether or not the dependency should be disposed. This is not trivial as it depends on different factors:
This is such a complex task that you should use a proper DI Container for the job.
However, keep in mind that this ultimately depends on whether or not your DI Container supports decommissioning. For example, Castle Windsor does while StructureMap doesn't.
嗯,一般的想法是,只要 DI 容器创建的任何内容都是
IDisposable
,那么它都会被释放。唯一的问题是何时发生。我怀疑不同容器之间可能存在差异,但我对此的看法是在正在创建的对象中实现 Dispose(),并在对象上显式调用Dispose()
被注入的对象。Well, the idea generally is that whatever gets created by a DI container gets disposed, provided it is an
IDisposable
. The only issue is when that happens. I suspect there might be differences between different containers, but my take on this would be to implementDispose()
in the object being created, and explicitly callingDispose()
on the object being injected.