C# 使用实体框架正确处理存储库和单元工作模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11809979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:20:59  来源:igfitidea点击:

Correct disposing using Repository and Unit Work patterns with Entity Framework?

c#entity-framework-4repositorydisposeunit-of-work

提问by TuffGong

Cheers!I have some doubts about using Unit of Work with Repository. Specially a role of child context from Entity Framework. I have searched a lot of information about this theme, but all that I found just different types of using patterns, I'm confused and I can't understand main think.

干杯!我对将工作单元与存储库一起使用有一些疑问。特别是来自实体框架的子上下文的角色。我搜索了很多关于这个主题的信息,但所有我发现的只是不同类型的使用模式,我很困惑,我无法理解主要的想法。

1.Where I should realize disposing and saving? -Is it correctly realize Disposable in Inheritance class of DbContext? After that realize in Repository and Unit of Work or just in Uni fo Work?

1.我应该在哪里实现处置和保存?- 在 DbContext 的继承类中是否正确实现了 Disposable?之后在 Repository 和 Unit of Work 中实现还是仅在 Uni fo Work 中实现?

-Where put method Save in Unit of Work or Repository?

- 将方法保存在工作单元或存储库中的位置?

My repository will be Generic Is my code is correct in architect style and other details?Please tell if my thinks are wrong.

我的存储库将是 Generic 我的代码在架构风格和其他细节上是否正确?如果我的想法是错误的,请告诉我。

    interface IRepository : IDisposable
    {
        void Create();
        void Delete();
        void Update();
        void Get();
        T getSomeByExpression()
        ...Some another costum operations
        ...should I remember about Save here? 
    }

    class Repository : IRepository
    {
        SomeContext context = new SomeContext();
        ...Using using(context = new SomeContext()){} in functions??
        ... 
        ....Disposing?
    }

    interface IUnitOfWork : IDisposable
    {
     ...Which methods I should realize?
    Commit()
    Save()
    ...Need some another methods like rollback, Attach() Add() or Dispose or something else?
    }
    class UnitOfWork
    {
     ...Collection of Repository

    }

Use after Unit of Work on Logic level? Please help me to understand this theme.

在逻辑级别的工作单元之后使用?请帮助我理解这个主题。

I want know, how correctly use Unit Of Work and Repository patterns together, especially include DBContext.Also I want know where use some operations like Dispose. Which operations should be in UnitOfWork commonly, Save etc. How disposing context in repository?

我想知道,如何正确地一起使用工作单元和存储库模式,尤其是包括 DBContext。我还想知道在哪里使用一些操作,如 Dispose。UnitOfWork中一般应该有哪些操作,Save等。如何在repository中配置context?

采纳答案by Mark Oreta

Here's a great articleon implementing unit of work using MVC.

这是一篇关于使用 MVC 实现工作单元的很棒的文章

I normally dispose of the unit once the business transaction is complete. For example, if the action was to create a Parent, some children and attached them I dispose immediately once that is finished.

我通常在业务交易完成后处置该单位。例如,如果操作是创建一个父级、一些子级并附加它们,我会在完成后立即处理。

Added more detail relating to above:

添加了与上述相关的更多细节:

In rereading your question, it sounds like you want more information about the theory of a unit of work rather than actual implementation, my apologies.

在重新阅读你的问题时,听起来你想要更多关于工作单元理论而不是实际实现的信息,我很抱歉。

Here's a better article on MSDNrelated to that, but I will summarize for you.

这是MSDN 上与此相关的更好的文章,但我会为您总结。

According to Martin Fowler, the Unit of Work pattern "maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems."

根据 Martin Fowler 的说法,工作单元模式“维护受业务事务影响的对象列表,并协调更改的写入和并发问题的解决。”

Generally, I use the unit of work pattern to bring together all of the related repositories, to solve concurrency issues while still keeping the repositories separate.

通常,我使用工作单元模式将所有相关的存储库放在一起,以解决并发问题,同时仍然保持存储库分离。

One of the best ways to use the Unit of Work pattern is to allow disparate classes and services to take part in a single logical transaction. The key point here is that you want the disparate classes and services to remain ignorant of each other while being able to enlist in a single transaction.

使用工作单元模式的最佳方法之一是允许不同的类和服务参与单个逻辑事务。这里的关键点是您希望不同的类和服务在能够参与单个事务的同时保持彼此不知情。

  • "Where I should realize disposing and saving?"
  • “我应该在哪里实现处置和储蓄?”

I'm not sure I fully understand your question, but I think you're asking what should be managing the lifecycle of the unit of work?

我不确定我是否完全理解您的问题,但我认为您在问应该如何管理工作单元的生命周期?

Here's another SO postrelated to this, but the summary is whatever owns the unit of work for the moment, and it's related to how you setup the scope of your unit of work. For example, It can be the business command, or an MVC action.

这是与此相关的另一篇SO 帖子,但摘要是目前拥有工作单元的任何内容,并且与您如何设置工作单元的范围有关。例如,它可以是业务命令,也可以是 MVC 操作。

  • "Is it correctly realize Disposable in Inheritance class of DbContext? After that realize in Repository and Unit of Work or just in Uni fo Work?"
  • “它是否在 DbContext 的继承类中正确实现了 Disposable?之后在 Repository 和 Unit of Work 中实现还是仅在 Uni fo Work 中实现?”

Do you mean, where should you be disposing of the DbContext? I believe that it should be in the unit of work. If you're creating/disposing of multiple contexts in a single unit of work, maybe you should separate them out into 2 different units.

你的意思是,你应该在哪里处理 DbContext?我认为它应该在工作单元中。如果您在单个工作单元中创建/处理多个上下文,也许您应该将它们分成 2 个不同的单元。

  • Where put method Save in Unit of Work or Repository?
  • 将方法保存在工作单元或存储库中的位置?

Your unit of work is handle the context, and the transactions, and should contain the logic to prevent duplicate updates, because of this your save function should be controlled by your unit of work.

您的工作单元是处理上下文和事务,并且应该包含防止重复更新的逻辑,因此您的保存功能应该由您的工作单元控制。