gpt4 book ai didi

c# - 在多个方法调用之间共享相同的 DataServiceContext - 好还是坏?

转载 作者:行者123 更新时间:2023-11-30 16:12:09 26 4
gpt4 key购买 nike

我想知道应该如何使用 DataServiceContext 实例的标准做法是什么。如果我有一个类,其中有多个方法需要对上下文执行操作,我应该在每个方法中创建一个新的上下文实例,还是让上下文成为所有方法共享的类的单个成员?

我问这个是因为我最近看到另一位开发人员在我正在处理的一些代码中发表的评论提到在添加一些对象之后和对其执行更多操作之前需要重新创建上下文。

即我应该这样做吗:

public class ServiceHelper
{
public void DoSomething()
{
DataContext ctx = new DataContext(Uri);

//do something with the context and call SaveChanges()
}

public int GetSometing()
{
DataContext ctx = new DataContext(Uri);

return ctx.GetSomething();
}
}

或者这个:

public class ServiceHelper
{
private readonly DataContext _ctx = new DataContext(Uri);

public void DoSomething()
{
//do something with _ctx and call SaveChanges()
}

public int GetSomething()
{
return _ctx.GetSomething();
}
}

?

我还应该补充一点,这是在 ASP.NET MVC 应用程序的上下文中,因此这里的 ServiceHelper 类将在每个页面请求时重建。

编辑

好吧,根据 Msft 的说法,这两种方法在技术上都是有效的,因为这些都是短期类,但我仍然想知道两者是否同样“安全”且等效。即,如果我添加/更新一些实体并调用 SaveChanges,那么可能会有一个单独的应用程序对相同的实体进行更新,然后我使用相同的上下文实例再次检索这些实体,一切都会表现得像我创建了一个第二次手术前的新环境?

结论

我刚找到 this我认为这有助于解释差异:

By default, the client only materializes an entry in the response feed into an object for entities that are not already being tracked by the DataServiceContext. This means that changes to objects already in the cache are not overwritten. This behavior is controlled by specifying a MergeOption value for queries and load operations. This option is specified by setting the MergeOption property on the DataServiceContext. The default merge option value is AppendOnly. This only materializes objects for entities that are not already being tracked, which means that existing objects are not overwritten. Another way to prevent changes to objects on the client from being overwritten by updates from the data service is to specify PreserveChanges. When you specify OverwriteChanges, values of objects on the client are replaced by the latest values from the entries in the response feed, even if changes have already been made to these objects.

所以看起来如果我更新一些实体,然后一个单独的应用程序进行进一步的更改,然后我使用相同的 DataServiceContext 实例再次检索这些实体,那么这取决于 MergeOption 设置为我将使实体处于它们在数据库中的状态,或者只是处于我上次在本地拥有它们的状态。

最佳答案

MSDN says it

In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

当然前提是您使用 LINQ to SQL。但即使在其他技术/库中,上下文通常也是一个非常轻量级的对象。在大多数情况下,物理连接不会被拆除(但逻辑上是)。我猜你关心的是连接池,所以只要你使用一个(你应该)然后就遵循 msdn 的建议。所以

public void DoSomething()
{
using(DataContext ctx = new DataContext(Uri))
{
//do something with the context and call SaveChanges()
}
}

关于c# - 在多个方法调用之间共享相同的 DataServiceContext - 好还是坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23431509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com