实体框架最佳实践:什么层应该调用调用SaveChanges()? [英] Entity Framework Best Practices: What layer should call SaveChanges()?

查看:163
本文介绍了实体框架最佳实践:什么层应该调用调用SaveChanges()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关干净的数据模型,我来回在这...

For a clean data model, I'm going back and forth on this...

使用审批工作流程作为一个例子,让我们在我的web应用程序说我有一个页面,让用户国旗的 MyEntityObject 批准。 MyEntityObject 有控制其审批流程的一些属性,所以我在那里叫 FlagForApproval(MyEntityObject EO)一个共同的实用方法

Using an approval workflow as an example, let's say in my web application I have a page that lets a user flag a MyEntityObject for approval. MyEntityObject has a few properties that control its approval workflow, so I have a common utility method out there called FlagForApproval(MyEntityObject eo).

应该在拨打 FlagForApproval()以只设置必要的属性,然后调用调用SaveChanges()当它准备好了,还是应该的 FlagForApproval()保存更改?

Should the page call FlagForApproval() to set necessary properties only and then call SaveChanges() when it's ready, or should FlagForApproval() save the changes?

具有实用方法保存更改恍如它做的多一点,比它的要求做(如果它是在一个系列的操作只需一个步骤?),但在同一时间,使得寻呼的SaveChanges()和提交数据到数据库似乎是它可以。被认为太接近数据层的职责

Having the utility method save changes seems like it's doing a little more than it's asked to do (what if it was just one step in a series of operations?), but at the same time, making the page call SaveChanges() and commit the data to the DB seems like it could be considered too close to data layer responsibilities.

思考

(更新:FWIW,到目前为止,我还一直有实用方法调用调用SaveChanges(),这样的页面只有一组异常处理,是否验证或数据。)

(Update: FWIW, so far I have been having utility methods call SaveChanges(), that way the page only has one set of exceptions to handle, whether validation or data.)

推荐答案

我在这个问题上目前的观点是始终有商业逻辑层调用保存验证数据之后再进行更改。当用户点击保存按钮,我通过任何实体需要验证下到BLL,并决定是否调用SaveChanges()。

My current opinion on this issue is to always have the business logic layer call save changes after validating data. When the user clicks Save button, i pass whatever entities need to be validated down to the BLL, and it decides whether to SaveChanges().

我一样好奇你虽然看到别人说什么,因为这个问题(和许多其他)一直困扰着我,因为我开始与EF

I'm as curious as you though, to see what others say because this issue (and many other) has been plaguing me since i started with EF.

这篇关于实体框架最佳实践:什么层应该调用调用SaveChanges()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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