可重复使用的ObjectContext或每个组操作的新的ObjectContext? [英] Reuseable ObjectContext or new ObjectContext for each set of operations?

查看:113
本文介绍了可重复使用的ObjectContext或每个组操作的新的ObjectContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的实体框架,和我刚开始在我的空闲时间玩它。其中一个我的主要问题是关于如何处理ObjectContexts



这是一般首选/推荐这些:



 公共类的DataAccess {

MyDbContext m_Context;

公共数据访问(){
m_Context =新MyDbContext();
}

公开的IEnumerable< SomeItem> GetSomeItems(){
返回m_Context.SomeItems;
}

公共无效DeleteSomeItem(SomeItem项目){
m_Context.DeleteObject(项目);
m_Context.SaveChanges();
}
}

或此?

 公共类的DataAccess {

公共数据访问(){}

公IEnumerable的< SomeItem> GetSomeItems(){
MyDbContext背景=新的DbContext();
返回context.SomeItems;
}

公共无效DeleteSomeItem(SomeItem项目){
MyDbContext背景=新的DbContext();
context.DeleteObject(项目);
context.SaveChanges();
}
}


解决方案

的。ObjectContext的,就是要在工作单位



从本质上讲这是什么意思是,每个操作(如:每个网页的请求)应该有新ObjectContext的实例。在该操作中,相同的ObjectContext应重新使用。



这是有道理的,当你想想看,作为交易和变更提交都绑在ObjectContext的实例。



如果你不写一个web应用程序,并且,而不是写一个WPF或Windows窗体应用程序,它变得更复杂一点,因为你没有。紧请求的范围,一个网页加载给你,但你的想法



PS:在任何一种你的例子,ObjectContext中的寿命要么是全球性的,或短暂。在这两种情况下,它不应该生活在数据访问类的内部 - 它应该是传递作为依赖


I'm new to the Entities Framework, and am just starting to play around with it in my free time. One of the major questions I have is regarding how to handle ObjectContexts.

Which is generally preferred/recommended of these:

This

public class DataAccess{

    MyDbContext m_Context;

    public DataAccess(){
        m_Context = new MyDbContext();        
    }

    public IEnumerable<SomeItem> GetSomeItems(){
        return m_Context.SomeItems;
    }

    public void DeleteSomeItem(SomeItem item){
        m_Context.DeleteObject(item);
        m_Context.SaveChanges();
    }
}

Or this?

public class DataAccess{

    public DataAccess(){ }

    public IEnumerable<SomeItem> GetSomeItems(){
        MyDbContext context = new DbContext();
        return context.SomeItems;
    }

    public void DeleteSomeItem(SomeItem item){
        MyDbContext context = new DbContext();
        context.DeleteObject(item);
        context.SaveChanges();
    }
}

解决方案

The ObjectContext is meant to be the "Unit of Work".

Essentially what this means is that for each "Operation" (eg: each web-page request) there should be a new ObjectContext instance. Within that operation, the same ObjectContext should be re-used.

This makes sense when you think about it, as transactions and change submission are all tied to the ObjectContext instance.

If you're not writing a web-app, and are instead writing a WPF or windows forms application, it gets a bit more complex, as you don't have the tight "request" scope that a web-page-load gives you, but you get the idea.

PS: In either of your examples, the lifetime of the ObjectContext will either be global, or transient. In both situations, it should NOT live inside the DataAccess class - it should be passed in as a dependency

这篇关于可重复使用的ObjectContext或每个组操作的新的ObjectContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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