4

I am developing application in N-Tier Architecture. as we all know that we need to implement transactions while insert/update/delete operation. please tell me how to use transaction in c#.net in N-Tier architecture. my architecture is like this Applicationform->middle_Layre->Factory->DataAccessLayre->StoredProcedure->Table in application form i create object of middleLayer and pass data in Insert/update/delete function of middle layer. i am creating object of sqlcommand in factoryclass and fill the data which i gets from middle layer and pass that object os sqlcommand to DAL.

3 Answers 3

8

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

8
  • i am not using MVC architecture. Nov 24, 2009 at 5:43
  • i want transaction for multiple operation in same transaction. Nov 24, 2009 at 5:46
  • 2
    Yes, that's what a "unit of work is." The MVC architecture is just an illustration. In short, your transaction should be in the same layer as your business logic. Nov 24, 2009 at 5:50
  • Your answer seems to contradict itself. You said "the transaction takes place in the BLL", but then later you said "The DAL will generally wrap a transaction around your unit of work. So which is it? That was the original question. :) Does the BLL start the transaction, or the DAL?
    – Beaker
    Apr 17, 2013 at 16:16
  • @RobertPappas: That's a good question. You can start a transaction in the BLL, and the DAL (the database, by extension) will maintain that transaction until you commit it. The DAL always executes the transaction, in other words, even if the transaction is started and committed from somewhere else. Apr 17, 2013 at 16:21
2

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.