You know a lot about EF. The savechange () method of EF itself comes with the transaction function. That is, the operations on dbconext before the savechange () method will be processed as a transaction. In a framework system, savechange may not meet the requirements. Therefore, the existence of iunitofwork provides necessary conditions. Iunitofwork is just an interface that provides us with a specification. The specific implementation of various ORM is different. At that time, we only needed to follow this standard. :).
/* **************************************** ************* Author: egojit * Date: 2012-7-13 * description: transaction interface ************************************** ************* */ Namespace Egojitframework. Domain { /// <Summary> /// Unit work, to manage things /// </Summary> Public Interface Iunitofwork { /// <Summary> /// /// Returns a bool type used to identify whether a unit is submitted. /// </Summary> Bool Committed { Get ;} /// <Summary> /// Submit unit work /// </Summary> Void Commit (); /// <Summary> /// Rollback unit work /// </Summary> Void Rollback ();}}
You can see that this excuse is simple. A property is used to determine whether a transaction has been committed. The other two methods, as the name suggests, commit a transaction and roll back a transaction.
Needless to say, we should pay attention to how their inheritance classes and transactions are implemented. Open the source code provided by me and you can see that it is inherited by the repositorycontextmanager class.
# Region Iunitofwork Member /// <Summary> /// Determine whether a transaction is committed /// </Summary> Public Bool Committed { Get { Return Context. committed ;}} /// <Summary> /// Commit transactions /// </Summary> Public Void Commit () {context. Commit ();} /// <Summary> /// Roll back a transaction /// </Summary> Public Void Rollback () {context. rollback ();} # Endregion
From this sectionCodeYou can't see anything, but don't worry. I just want to analyze the implementation process with you. The context above is actually the irepositorycontext type. When you open this interface, it is easy to see that it also inherits iunitofwork. This will make everyone suddenly understand that all implementations are in the irepositorycontext implementation class. The repositorycontext class inherits the irepositorycontext interface, and then we find from the code that the method for iunitofwork interface is abstract in European style, so it must be implemented. It is not hard to understand that the repositorycontext storage environment is still general, rather than implemented for special Orm. Here we can easily think that the orm tool here is EF, so its implementation must be in the entityframeworkrepositorycontext class. By the way, we guess we are right. Entityframeworkrepositorycontext:
Public ClassEntityframeworkrepositorycontext: repositorycontext, ientityframeworkrepositorycontext
We can see that he inherits the repositorycontext class, so we are returning to this class:
Using System; Using System. Collections. Generic; Using Egojitframework. Infrastructure; /* **************************************** ************* Author: egojit * Date: 2012-7-13 * description: warehousing context basics ************************************* ************** */ Namespace Egojitframework. domain. repositories { /// <Summary> /// Warehousing context Basics /// </Summary> Public Abstract Class Repositorycontext: disposableobject, irepositorycontext { # Region Private Field Private Readonly Guid id = Guid. newguid (); [threadstatic] Private Readonly Dictionary <guid, Object > Newcollection = New Dictionary <guid, Object > (); [Threadstatic] Private Readonly Dictionary <guid, Object > Modifiedcollection = New Dictionary <guid, Object > (); [Threadstatic] Private Readonly Dictionary <guid, Object > Deletedcollection = New Dictionary <guid, Object > (); [Threadstatic] Private Bool Committed = True ; # Endregion
We found that there is another piece of code that is used to put, delete, modify, and add objects, using guid as the key. This is the local memory, first, place the changes to the object in these three sets. You can see
RepositorycontextClassIrepositorycontextThe implementation of the interface here I will paste a code
/// <Summary> /// Register a new object to the storage environment /// </Summary> /// <Typeparam name = "taggregateroot"> Warehouse root type. </Typeparam> /// <Param name = "OBJ"> The name of the registered object. </Param> Public Virtual Void Registernew <taggregateroot> (taggregateroot OBJ) Where Taggregateroot: Class , Iaggregateroot { If (Obj. Id. Equals (guid. Empty )) Throw New Argumentexception ( " The ID of the object is empty. " , " OBJ " ); If (Modifiedcollection. containskey (obj. ID )) Throw New Invalidoperationexception ( " The object cannot be registered as a new object since it was marked as modified. " ); If (Newcollection. containskey (obj. ID )) Throw New Invalidoperationexception ( " The object has already been registered as a new object. " ); Newcollection. Add (obj. ID, OBJ); committed = False ;}
In this way, they are put into the dictionary. AndCommitted=False;Return to the entityframeworkrepositorycontext class. Let's take a look at the implementation code of the transaction.
/* **************************************** ************* Author: egojit * Date: 2012-8-14 * description: role service ************************************** ************* */ Using System; Using System. Data. entity; Using System. Data. entity. validation; Using Egojitframework. domain. model; Namespace Egojitframework. domain. repositories { Public Class Entityframeworkrepositorycontext: repositorycontext, ientityframeworkrepositorycontext { Private Readonly Egojitframeworkcontext CTX = New Egojitframeworkcontext (); Private Readonly Object Sync = New Object (); Public Override Void Registerdeleted <taggregateroot> (Taggregateroot OBJ) {CTX. Entry <Taggregateroot> (OBJ). State = System. Data. entitystate. Deleted; committed = False ;} Public Override Void Registermodified <taggregateroot> (Taggregateroot OBJ) {CTX. Entry <Taggregateroot> (OBJ). State = System. Data. entitystate. Modified; committed = False ;} Public Override Void Registernew <taggregateroot> (Taggregateroot OBJ) {CTX. Entry <Taggregateroot> (OBJ). State = System. Data. entitystate. added; committed = False ;} Public Override Void Commit (){ If (! Committed ){ Lock (Sync ){ Try {CTX. savechanges (); committed = True ;} Catch (Dbentityvalidationexception e ){ Foreach ( VaR Eve In E. entityvalidationerrors) {console. writeline ( " Entity of Type \ "{0} \" in state \ "{1} \" has the following validation errors: " , Eve. Entry. entity. GetType (). Name, Eve. Entry. State ); Foreach ( VaR Ve In Eve. validationerrors) {console. writeline ( " -Property: \ "{0} \", error: \ "{1 }\" " , VE. propertyname, VE. errormessage );}} Throw ;}}}} Public Override Void Rollback () {committed =False ;}
So far, we have understood the entire transaction implementation process. Is the transaction implemented in this way more flexible. We do not need to frequently connect to the database, and all operations are stored in the local memory. Until we submit.
Copyright: This document is owned by the blog and egojit. For more information, see the source.