Linq to SQL - 我应该如何管理数据库请求?
我对 DataContext 的生命周期进行了一些研究,试图找出最好的做事方式。
鉴于我想在 Web 应用程序中重复使用 DAL,我决定采用DataContext Per Business Object Request 方法。
我的想法是从 dbml 文件扩展我的 L2S 实体,以检索数据库的信息,为每个请求创建一个单独的上下文,例如,
public partial class AnEntity
{
public IEnumerable<RelatedEntity> GetRelatedEntities()
{
using (var dc = new MyDataContext())
{
return dc.RelatedEntities.Where(r => r.EntityID == this.ID);
}
}
}
在返回实体方面...我此时需要返回 POCO 还是可以简单地返回从查询返回的业务对象?我知道,如果我尝试访问返回实体的属性(在 DataContext 被释放之后),它将失败。然而,这就是我决定实现这些类型的方法的原因,例如
而不是:
AnEntity entity = null;
using (var repo = new EntityRepo())
{
entity = repo.GetEntity(12345);
}
var related = entity.RelatedEntities; // this would cause an exception
理论上我应该能够做到:
AnEntity entity = null;
using (var repo = new EntityRepo())
{
entity = repo.GetEntity(12345);
}
var related = entity.GetRelatedEntities();
鉴于我的特定应用程序的情况(需要在 Windows 服务和 Web 应用程序中工作),我会我想知道这是否是一种合理的方法,是否存在明显的缺陷,以及对于我正在尝试做的事情是否有更好的方法。
谢谢。
I have studied a bit into the lifespan of the DataContext trying to work out what is the best possible way of doing things.
Given I want to re-use my DAL in a web application I decided to go with the DataContext Per Business Object Request approach.
My idea was to extend my L2S entities from the dbml file to retrieve information the database creating a separate context per request e.g.
public partial class AnEntity
{
public IEnumerable<RelatedEntity> GetRelatedEntities()
{
using (var dc = new MyDataContext())
{
return dc.RelatedEntities.Where(r => r.EntityID == this.ID);
}
}
}
In terms of returning the Entities...do I need to return POCOs at this point or is it ok to simply return the business object returned from the query? I understand that if I was to try access properties of the returned entity (after the DataContext has been disposed) it would fail. However, this is the reason I have decided to implement these type of methods e.g.
Instead of:
AnEntity entity = null;
using (var repo = new EntityRepo())
{
entity = repo.GetEntity(12345);
}
var related = entity.RelatedEntities; // this would cause an exception
In theory I should be able to do:
AnEntity entity = null;
using (var repo = new EntityRepo())
{
entity = repo.GetEntity(12345);
}
var related = entity.GetRelatedEntities();
Given the circumstances of my particular app (needs to work in a windows service & web application) I would like to know if this seems a plausible approach, whether there are obvious flaws and if there are better approaches for what it is I am trying to do.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,只要您不使用多个线程调用单个 DataContext 对象,就应该没问题。换句话说,每个线程使用一个 DataContext 对象,并且不要在不同的 DataContext 对象之间共享数据或状态。
其余的多线程问题与数据库中的并发有关,不是线程操作。
除了这些警告之外,你的方法似乎是合理的。您可以使用分部类来实现业务方法,也可以在 Linq to SQL 类和存储库之间添加业务层。
Generally speaking, as long as you are not calling a single DataContext object using more than one thread, you should be OK. In other words, use one DataContext object per thread, and do not share data or state between different DataContext objects.
The remaining multi-threaded issues have to do with concurrency in the database, not threading operations.
Other than these caveats, your approach seems sound. You can either use partial classes to implement your business methods, or you can add a business layer between the Linq to SQL classes and your repository.
您可以摆脱这个:
请参阅此 StackOverflow 帖子解释为什么不处理你的上下文不会导致连接泄漏或类似的事情。
当存储库和上下文获取的实体超出范围时(构建网站时,在请求结束时),垃圾收集器将清理存储库和上下文。
编辑:MSDN文档连接是尽可能晚开门,尽快关门。因此跳过使用不会导致连接池问题。
You can get away with this:
See this StackOverflow post for an explanation why not disposing your context doesn't cause connection leaks or stuff like that.
The repository and context will get cleaned up by the garbage collector when the entities that were fetched by them fall out of scope (when building a website, at the end of the request).
Edit: MSDN documents that connections are opened as late as possible and closed as soon as possible. So skipping the using doesn't causes connection pool problems.