在 LINQ to Entities 中实例化上下文 [英] Instantiating a context in LINQ to Entities

查看:12
本文介绍了在 LINQ to Entities 中实例化上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过程序员在其代码中创建实体上下文时采用的两种不同方式.

I've seen two different manners that programmers approach when creating an entity context in their code.

第一个是这样的,你可以在 MSDN 代码示例中找到它:

The first is like such, and you can find it all over the MSDN code examples:

public void DoSomething() {
     using (TaxableEducationEntities context = new TaxableEducationEntities()) {
          // business logic and whatever else
     }
}

第二种是在封装业务逻辑的某个类中将上下文创建为私有属性.所以你会有类似的东西:

The second is to create the context as a private attribute in some class that encapsulates your business logic. So you would have something like:

public class Education_LINQ {

        private TaxableEducationEntities context = new TaxableEducationEntities();

        public void DoSomething() {
            var result = from a in context.luAction
                         select a;

            // business logic and whatever else
        }
}

哪种方式更有效率?

假设您有两种方法,一种称为 DoSomething1(),另一种称为 DoSomething2(),并且这两种方法都包含 using 语句以打开上下文并对其执行任何操作.您是否要一个接一个地调用一个方法,是否会产生任何多余的开销,因为本质上这两种方法都创建了上下文,然后在完成后将其清理干净?与在实例化类对象时仅创建一个私有属性相反,然后在对象超出范围时又将其清除?

Assume that you have two methods, one called DoSomething1() and another called DoSomething2(), and both methods incorporate the using statement to open the context and do whatever with it. Were you to call one method after the other, would there be any superfluous overhead going on, since essentially both methods create the context and then clean it up when they're done? As opposed to having just one private attribute that is created when a class object is instantiated, and then in turn cleaned up when the object goes out of scope?

推荐答案

每次创建一个新的 ObjectContext 确实涉及一些"开销.基本上涉及的开销是将元数据从全局缓存复制到与特定 ObjectContext 关联的元数据中.

Creating a new ObjectContext each time does involve 'some' overhead. Essentially the overhead involved is copying metadata from a global cache into metadata associated with the specific ObjectContext.

这种开销相对较小,因此通常不值得担心,尤其是当您考虑到 using 模式固有的额外安全性时.

This overhead is relatively minor, so often it is not worth worrying about, especially when you consider the extra safety inherent in the using pattern.

对我来说,您选择哪个选项取决于以下几点:

For me which option you choose depends upon things like:

  1. 你的寿命有多长包装类可能是?如果它活了一个很长一段时间 ObjectContext 可能成长为拥有很多实体随着时间的推移放缓.所以一个新ObjectContext 每次都可能是一个好主意.
  2. 是否调用了包装类上的方法同步?对象上下文类本身不是线程安全的,所以如果你使用你需要的第二种模式确保您的包装类/存储库是线程安全的,如果你期望多个线程调用它.
  3. 方法本质上是无关?如果是这样,如果它们在方法之间共享一个上下文,您可能会得到意想不到的副作用.

总的来说,我的建议是,如果方法是无状态的,即触发并忘记每个方法的新上下文可能是个好主意.

In general my recommendation is that if the methods are stateless, i.e. fire and forget a new context for each method is probably a good idea.

但是,如果您有一个相对较短的有状态表单或其他东西,那么共享上下文可能是一个更好的主意.

If however you have a relatively short lived stateful form or something then maybe a shared context is a better idea.

更新:我花时间整理了更完整的答案

这篇关于在 LINQ to Entities 中实例化上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆