使用实体框架使用存储库和单元工作模式进行正确处理? [英] Correct disposing using Repository and Unit Work patterns with Entity Framework?

查看:185
本文介绍了使用实体框架使用存储库和单元工作模式进行正确处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

干杯!我对使用工作单位与存储库有一些疑问。特别是实体框架中的子语境的作用。
我搜索了很多关于这个主题的信息,但是我发现只是不同类型的使用模式,我感到困惑,我不明白主意。



1.我应该意识到处置和储存?
- 它是否正确实现了DbContext的继承类中的Disposable?
之后,在存储库和工作单元中或在Uni工作中实现?



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



我的存储库将是Generic
我的代码在建筑师风格和其他详细信息中是否正确?请告诉我的想法是否错误。

  interface IRepository:IDisposable 
{
void Create();
void Delete();
void Update();
void Get();
T getSomeByExpression()
...另一个costum操作
...我应该记得关于保存在这里吗?
}

class Repository:IRepository
{
SomeContext context = new SomeContext();
...在函数中使用using(context = new SomeContext()){}
...
....处理?
}

接口IUnitOfWork:IDisposable
{
...我应该实现哪些方法?
提交()
保存()
...需要一些其他方法,如回滚,Attach()Add()或Dispose等等?
}
class UnitOfWork
{
...存储库的集合

}

在逻辑层面上使用工作单元后?
请帮助我理解这个主题。



我想知道,如何正确地使用工作单位和存储库模式,特别是包括DBContext.Also我想知道其中使用一些操作,如Dispose。
哪些操作应该在UnitOfWork中常用,保存等
如何在存储库中处理上下文?

解决方案

这是一个伟大的文章使用MVC实现工作单元。



我通常处理一旦商业交易完成,该单位。例如,如果操作是创建一个Parent,一些孩子并附加它们,我们立即处理完毕。



添加了更多与上述相关的详细信息:



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



这是一个更好的文章相关的MSDN ,但我会为您总结。


根据Martin Fowler,工作单位模式维护一个列表
的受商业交易影响的对象,并协调
写入更改和解决并发问题。


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


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





  • 我应该意识到处置和保存?



我不知道我完全明白你的问题,但我想你在问什么应该管理单位的生命周期的工作?



这是另一个 SO post < a>与此相关,但总结是什么拥有当前的工作单位,它与您如何设置您的工作单位的范围有关。例如,它可以是业务命令或MVC操作。




  • 是否正确实现DbContext的继承类中的Disposable?After在知识库和工作单位中实现,或仅在工作中实现?



你的意思是,的DbContext?我相信它应该是在工作的单位。如果您在单个工作单位中创建/处理多个上下文,也许您应该将它们分成两个不同的单位。




  • 哪些放置方法保存在工作单位或存储库?



您的工作单位处理上下文,交易,应包含防止重复更新的逻辑,因为您的保存功能应由您的工作单位控制。


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?

-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.

    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?

解决方案

Here's a great article on implementing unit of work using 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 MSDN related to that, but I will summarize for you.

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."

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 post related 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.

  • "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?"

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.

  • 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.

这篇关于使用实体框架使用存储库和单元工作模式进行正确处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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