什么是EF的工作模式的单位? [英] What is the unit of work pattern in EF?

查看:169
本文介绍了什么是EF的工作模式的单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习EF和见过很多例子,我的学习过程中我才知道如何使用工作模式库和单位。我为什么要使用存储库,但我没有单位工作的认识确实是。

I am learning EF and have seen many examples, and during my learning I came to know about using repository and unit of work patterns. I got why to use repository but I do not have understanding of unit of work really is.

由于没有理解正在DAL理解困难。请指引我。

Having no understanding is making DAL understanding difficult. Kindly guide me.

感谢

推荐答案

在DataContext或ObjectContext的是工作的单位。

The DataContext or ObjectContext is the Unit of Work.

所以,你的DAL将保存,删除和检索对象和你的DataContext / ObjectContext的会跟踪你的对象,管理事务并应用更改。

So, your DAL will save, delete and retrieve objects and your DataContext/ObjectContext will keep track of your objects, manage transactions and apply changes.

这是一个例子只是为了说明该解决方案的想法

using(var context = new ObjectContext()) { // Unit of Work
    var repo = new ProductRepository(context);
    var product = repo.GetXXXXXXX(...);
    ...

    // Do whatever tracking you want to do with the object context. For instance:
    // if( error == false) { 
    //     context.DetectChanges();
    //     context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
    // }
}

和你的仓库将是这样的:

And your repository will look like:

public abstract class Repository {

    public Respository(ObjectContext context){
        CurrentContext = context;
    }

    protected ObjectContext CurrentContext { get; private set; } 
}

public class ProductRespository : Repository {
    public ProductRespository(ObjectContext context) : base(context){
    }

    public Product GetXXXXXX(...){
        return CurrentContext... ; //Do something with the context
    }
}    

另一种方式是把工作(目标上下文)的全球单位:

Another way is to put the unit of work (Object context) globally:

您需要定义什么将是你的工作范围单元。对于这个例子,这将是一个Web请求。在现实世界中实现,我会使用依赖注入的。

You need to define what will be your unit of work scope. For this example, it will be a web request. In a real world implementation, I'd use dependency injection for that.

public static class ContextProvider {

    public static ObjectContext CurrentContext {
        get { return HttpContext.Items["CurrentObjectContext"];
    }

    public static void OpenNew(){
        var context = new ObjectContext();
        HttpContext.Items["CurrentObjectContext"] = context; 
    }

    public static void CloseCurrent(){
        var context = CurrentContext;
        HttpContext.Items["CurrentObjectContext"] = null;
        // Do whatever tracking you want to do with the object context. For instance:
        // if( error == false) { 
        //     context.DetectChanges();
        //     context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
        // }
        context.Dispose();
    }
}

在这个例子中,是ObjectContext的工作单元,它将生活在当前请求。在您的全球ASAX您可以添加:

In this example, ObjectContext is the unit of work and it will live in the current request. In your global asax you could add:

protected void Application_BeginRequest(object sender, EventArgs e){
    ContextProvider.OpenNew();
}

protected void Application_EndRequest(object sender, EventArgs e){
    ContextProvider.CloseCurrent();
}

在你的资料库,您只需要调用 ContextProvider.CurrentContext

In your Repositories, you just call ContextProvider.CurrentContext

这篇关于什么是EF的工作模式的单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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