Ddd ~ Infrastructure layer ~ Continued

Source: Internet
Author: User

Back to directory

The previously written DDD ~ The infrastructure layer article mentioned unitofwork, which has some methods, but after project proof, there should be no save or isexplicitsubmit, however, this work unit only plays a unified role in data context. For example, objects A and B need to work in the same context. In this case, we can reference the concept of work unit, the SAVE and submit operations should still be completed in the local method.

In order not to trigger MSDTC, I will encapsulate a special transaction to implement this operation. For sql2008, it can be directly used. net's own transactionscope implementation. For the same database, it will not be upgraded to a distributed transaction. For versions earlier than sql2008, the occupied transaction needs to be used. The Code is as follows:

/// <Summary> /// Author: Zhang. zhanling // synchronous article: http://www.cnblogs.com/lori/p/3455393.html // to transactionscope, so that it does not generate the MSDTC service for the same database. // </Summary> public class transactionscopenomsdtc {// <summary> // generate a package transaction // support the submission of the LINQ table, and return the submission result to the object // </Summary> /// <Param name = "DB"> data context, the context used by multiple methods must be the same </param> // <Param name = "isoutermost"> whether it is the outermost layer, the default value is false </param> /// <Param name = "action"> process code block </param> Public static void usingnomsdtc (dbcontext dB, bool isoutermost, Action action) {var objectcontext = (system. data. entity. infrastructure. iobjectcontextadapter) dB ). objectcontext; try {If (objectcontext. connection. state = system. data. connectionstate. closed) objectcontext. connection. open (); Using (transactionscope trans = new transactionscope () {try {Action (); Trans. complete ();} catch (exception) {Throw;} finally {TRANS. dispose () ;}} catch (exception) {Throw;} finally {If (isoutermost) // if the transaction is the outermost layer, close the connection {objectcontext. connection. close () ;}}/// <summary> // generates a package transaction, which is not the outermost layer, if it is the outermost layer, you need to call other heavy loads // </Summary> /// <Param name = "DB"> data context, the context used by multiple methods must be the same </param> // <Param name = "action"> processing code block </param> Public static void usingnomsdtc (dbcontext dB, action action) {usingnomsdtc (dB, false, Action );}}

The latest iunitofwork interface becomes an ID interface. The Code is as follows:

/// <Summary> /// data context identification interface, which should be public to the business layer // it does not care about the method for implementing the context. It can be linq2sql, EF, ado.net, nhib.pdf, memory, nosql, etc // </Summary> Public interface iunitofwork {}

We can see that the main function of iunitofwork is to mark each warehouse in the same context, or, in a work unit, the following code is used in the project. For details, refer

/// <Summary> /// point_info warehouse /// </Summary> /// <remarks> Create: Cyr _ (Ben) _ 20131128 </remarks> public class point_inforepository: Role <point_info> {# region constructors public point_inforepository (): This (null) {} public point_inforepository (iunitofwork dB): Base (db) {}# endregion}

We can see that the above warehouse provides two constructor implementation methods. The default value is null, and one can input an iunitofwork unit for it, this warehouse also supports IOC constructor injection.

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.