通过依赖注入进行对象处理

发布于 2024-10-09 18:42:12 字数 928 浏览 3 评论 0原文

我创建了一个存储库类,我想在代码隐藏页面中使用它。我在代码隐藏页面中使用构造函数注入来实例化存储库。

存储库类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2024-10-16 18:42:12

您应该采用Register Resolve Release 模式

具体来说,您应该记住始终发布您解决的问题。作曲家有责任跟踪是否应释放依赖项。这并非微不足道,因为它取决于不同的因素:

  • 依赖项是否实现 IDisposable?
  • 依赖项的生命周期是否表明它应该立即或稍后处置?

这是一项非常复杂的任务,您应该使用适当的 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:

  • Does the dependency implement IDisposable?
  • Does the dependency's lifetime indicate that it should be disposed now or later?

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.

小伙你站住 2024-10-16 18:42:12

嗯,一般的想法是,只要 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 implement Dispose() in the object being created, and explicitly calling Dispose() on the object being injected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文