How To Maintain Transaction in N-Tier Architecture

左心房为你撑大大i 提交于 2019-12-03 00:43:53

Here is a representative pattern of software layers you can follow:

Database <-> DAL <-> Repository <-> BLL <-> Controller <-> View Model <-> UI

Where

DAL == Data Access Layer (aka ORM, Object-Relational mapper)
BLL == Business Logic Layer*

In this model, the transaction takes place in the Repository, where a "unit of work" is arranged. Typically, this happens by requesting data from the DAL, performing work on it, and saving changes. The DAL will generally wrap a transaction around your unit of work.

The Database, DAL, Repository and BLL collectively form what is known as the Model in MVC (Model-view-controller) architecture. All business logic and data manipulation takes place in the Model. The controller acts as a go-between between the model and the View Model/UI, which collectively form the view.

The Repository is where you would set up your "Unit of Work."

*Optional

As long as your code is running on the same machine, without any WCF or Web Service calls between the layers, you could use TransactionScope.

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx

Just place the calls that you want to occur with a transaction, inside the transaction scope.

Another option is to place the logic in a stored procedure. This way your DAL makes a single call to the db that rolls back on failure.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!