Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Best Practices: What layer should call SaveChanges()?

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

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

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

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.

Thoughts?

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

like image 549
Brandon Avatar asked Jan 16 '10 00:01

Brandon


People also ask

How to use savechanges in Entity Framework?

Entity Framework SaveChanges 1 You can add, modify, and remove data using your context and entity classes. 2 SaveChanges method automatically call DetectChanges method to discover any changes to entity instances before saving to... More ...

How to detect changes in context in Entity Framework?

In Entity Framework, the DbContext.SaveChanges method saves all changes made in the context of the database. You can add, modify, and remove data using your context and entity classes. SaveChanges method automatically call DetectChanges method to discover any changes to entity instances before saving to the underlying database.

How to detect changes to entity before saving to underlying database?

SaveChanges method automatically call DetectChanges method to discover any changes to entity instances before saving to the underlying database. To add data to the database, you can use the DbSet.Add method to add new instances of your entity classes, and then call SaveChanges.

Does business logic call EF Core’s savechanges method directly?

The business logic does not call EF Core’s SaveChanges method directly. I have a class in the Service Layer whose job it is to run the business logic – this is a case of the Service Layer implementing the command pattern. and, if there are no errors, it calls SaveChanges.


2 Answers

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

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.

like image 97
echo Avatar answered Sep 19 '22 14:09

echo


The key is to separate Database and Service language. If the utility method needs to save changes then it does, if not make it clear that it does not and additional steps are needed. The utility should not have a method called SaveChanges, it should have process related methods, like StartProcess or LoadToBatch.

View the utility as more of a service and do not think database. The "FlagForApproval" sounds like a database operation, try thinking of the method as something like "StartApprovalProcess" or something else process related. StartApprovalProcess would do all work and commits.

If there are multiple steps, make each step indicate indirectly that there may be more steps. Only the last step commits. Allthough all the last step may do is save changes, make is read like a process such as move or start.

Ex:

  1. LoadToBatchApproval(MyEntityObject eo)

  2. ValidateApprovalBatch()...

  3. MoveBatchToProcessing()...

like image 26
longday Avatar answered Sep 19 '22 14:09

longday