Unit of work Design

Source: Internet
Author: User

In the DDD development process, a good UOW design is essential, in my mind the UOW design should have the following points:

1, has the good abstraction, has the aptly named;

2, can cope with different components, such as your system may exist efunitofwork, redisunitofwork;

3, easy to use, without the developer to display the call. UOW can be wrapped automatically outside the business logic at the beginning of a user request to the end;

After reading the source code of ABP I feel that the UOW in ABP exactly meet my requirements, but its implementation is slightly more complex, such as adding dynamicfilters,efunitofwork support DbContext in Efunitofwork. These complexities cause the Efunitofwork class to become a bit bloated. So I removed the design from the instance code.

First, Business abstraction

A typical code snippet that uses UOW is as follows:

            using (var UOW = Uowmanager.begin ())            {                //....logic                uow.commit ();            }

From this piece of code we can basically analyze the following UOW abstraction.

1, Iunitofworkmanager

As you can see from the code snippet above, this manager should have a begin () method that returns the IDisposable type.

2, Iunitofworkcompletehandle

The IDisposable type returned by the Iunitofworkmanager begin () method should have the ability to commit (), which is used to commit and release resources to Iunitofwork.

3, Iunitofwork

From this snippet we don't see the abstraction of Iunitofwork because Iunitofworkmanager hides the specific iunitofwork,iunitofworkmanager.begin () The implementation is actually a specific iunitofwork.begin () call.

4, Icurrentunitofworkprovider

The user's request in one context has a unique iunitofwork, so the iunitofwork of the current context can be read at any time through Icurrentunitofworkprovider.

Through the interface naming description can clarify the whole UOW design idea:

Second, efunitofwork

The begin of efunitofwork is embodied in the invocation of TransactionScope and commit in the invocation of Dbcontext.savechanges ().

 public void Begin (Unitofworkoptions options) {_transactionscope = new TransactionScope ( Options. Transactionscopeoption.getvalueordefault (_defaultunitofworkoptions.transactionscopeoption), new Transaction Options () {IsolationLevel = options. Isolationlevel.getvalueordefault (_defaultunitofworkoptions.isolationlevel), Timeout = options.        Timeout.getvalueordefault (_defaultunitofworkoptions.timeout)}); }
        public void complete ()        {            try            {                _dbcontext.savechanges ();                _transactionscope?.complete ();                _completed?. Invoke ();            }            Catch            {                _failed?. Invoke ();                throw;            }                   }

Third, the implementation of AOP through castle, will UOW parcel Applicationservice layer

Defines the unitofworkinterceptor, which behaves as a unitofwork implementation of an existing method package.

        private void Performuow (iinvocation invocation, unitofworkoptions options)        {            using (var UOW = _ Unitofworkmanager.begin (options))            {                invocation. Proceed ();                Uow.complete ();            }        }

For specific implementations, please refer to: https://git.oschina.net/richieyangs/BookLibrary.git

Unit of work Design

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.