Iunitofwork ~ Method integrity and unified submission do not conflict

Source: Internet
Author: User

Back to directory

First, I want to know iunitofwork. Why does the iunitofwork interface appear? 2. What happened to submtchanges () in LINQ to SQL and entityframeworks Third, there is no conflict between method integrity and unified submission. Fourth, dbcontext object creation should be made public to the BLL Layer Fifth, my iunitofwork + repository Architecture

In a method, it generally does one thing. Such a method has a clear function and a single responsibility (here, the single method is derogatory ), what it does Will be done from start to end, and will not achieve half of the functions. This is an incomplete function, which is not recommended by us.

ProjectCode:

Complete Submission method:

 
Protected Virtual VoidSavechanges (){If(!Iunitwork. isnotsubmit) iunitwork. Save ();}

Complete insertion method:

Public Virtual VoidAdd (tentity item) {_ dB. Entry<Tentity>(Item); _ dB. Set<Tentity>(). Add (item );This. Savechanges ();}

The above code is ef-implemented insertion, Which is perfect. It adds the implementation to the entity set and submits it to the database using savechanges (). A complete data insertion process is complete, but a problem arises.

How can I implement business operations not only insert a table but also update another table?

Public Virtual VoidModify (tentity item) {_ dB. Set<Tentity>(). Attach (item); _ dB. Entry (item). State=Entitystate. modified;This. Savechanges ();}

The complete update action above is the code above. Now there is a hypothesis:

The userrepository class has the add method and the productrepository class has the modify method. In this case, the two methods are organized and the code may be like this:

... Code

Userrepository. Add (User );

Userrepository. Modify (product );

... Code

In fact, the process of executing the above Code is: First insert the user table, submit it to the SQL database, then update the product table, and then submit it to the SQL database,At this time, the SQL end will generate two connection pools because of the two commits. If the two methods use the transactionscope transaction block, and the SQL server and the WWW server are on different computers, it will trigger more distributed transactions (this can be avoided), and we know that Windows's MSDTC (Distributed Transaction) service is the most unreliable.

How can this problem be solved? Shouldn't the solution be complete?

There is a solution to everything, and the integrity of the method is no problem in the system design,But sometimes, when there are multiple methods in a work unit, we need to upgrade the integrity of these methods to a whole, that is, the integrity of multiple methods.The key to solving this problem is whether your data context is one and whether your submitchanges method is one.

Iunitwork new interface specifications

     ///   <Summary>      ///  Work Unit  ///  Provides a storage method, which can be made public to the call layer. To reduce the number of database connections  ///  </Summary>      Public   Interface  Iunitofwork {  ///   <Summary>          ///  Submit the operation to the database,  ///   </Summary>          Void  Save ();  ///   <Summary>          /// Whether or not to submit data to the database. This is only used in the savechanges method in the specific repository class.  ///  The default value is false. That is, it is submitted to the database by default.  ///   </Summary>          ///   <Returns> </returns>          Bool Isnotsubmit { Get ; Set  ;}}  ///   <Summary>      /// Work Unit  ///  Support for generic types  ///   </Summary>      ///   <Typeparam name = "T"> </typeparam>      Public   Interface Iunitwork <t>: iunitofwork Where T: Class {}

After reading the interface above, I don't need to say it. Everyone knows the meaning. Save () is the data context submission, and isnotsubmit indicates whether to submit the data to the database, we all know the default bool type object

The value is not false. By default, the commit actions of ADD and modify methods are true, that is, they are submitted to the database.

The above add and modify methods are optimized as follows:

Domain. Core. iunitofwork _ iunitwork =NewBackgroundentities (); _ iunitwork. isnotsubmit=True; Userrepository (User); productrepository (product); _ iunitwork. Save ();

OK. The result of the above Code is to send two SQL statements to the SQL end and use an SQL connection pool without the MSDTC Service.

Back to directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.