Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 173639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:22:44+00:00 2026-05-11T13:22:44+00:00

I have a situation (I’m guessing is pretty standard) where I need to perform

  • 0

I have a situation (I’m guessing is pretty standard) where I need to perform some business calculations and create a bunch of records in the database. If anything goes wrong at any point I need to roll everything back from the database. Obviosly I need some kind of transaction. My question is where do I implement transaction support. Here’s my example

//BillingServices - This is my billing service layer. called from the UI public Result GenerateBill(BillData obj) {      //Validate BillData       //Create a receivable line item in the receivables ledger       BillingRepository.Save(receivableItem);       //Update account record to reflect new billing information      BillingRepository.Save(accountRecord);       //...do a some other stuff      BillingRepository.Save(moreStuffInTheDatabase); } 

If any of the updates to the database fail I need to roll the other ones back and get out. Do I just expose a Connection object through my Repository in which I can call

Connection.BeginTransaction()

or do I do I just validate in the service layer and just call one method in the repository that saves all the objects and handles the transaction? This doesn’t quite seem right to me. It’s seem like it would force me to put to much business logic in the data layer.

What’s the right approach? What if I need to span repositories (or would that be bad design)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:22:44+00:00Added an answer on May 11, 2026 at 1:22 pm

    I’m assuming that you are using .NET here. That being the case, you can simply wrap the entire code section in a using statement with a TransactionScope instance and it will handle the transaction semantics for you. You simply have to call the Complete method at the end:

    //BillingServices - This is my billing service layer. called from the UI public Result GenerateBill(BillData obj) {      // Create the transaction scope, this defaults to Required.      using (TransactionScope txScope = new TransactionScope())      {           //Validate BillData            //Create a receivable line item in the receivables ledger            BillingRepository.Save(receivableItem);            //Update account record to reflect new billing information           BillingRepository.Save(accountRecord);            //...do a some other stuff           BillingRepository.Save(moreStuffInTheDatabase);            // Commit the transaction.           txScope.Complete();      } } 

    If an exception occurs, this has the effect of not calling Complete when the code block is exited; the Dispose method on the TransactionScope implementation of the IDisposable interface is called when the scope of the using statement is exited.

    In the Dispose call, it checks to see if the transaction completed (this state is set when Complete succeeds). If that state isn’t set, it performs a rollback.

    You can then nest this within other TransactionScope instances (deeper in your call stack on the same thread) to create larger transactions across multiple repositories.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation in which I read a record from a database. And if
I have situation where I need to change the order of the columns/adding new
I have this situation: $(#button).toggle(function(){ $(#window).animate({top:'0%'},1000); },function(){ $(#window).animate({top:'-100%'},1000); }); but I need change it
I have situation, when user prepare some data in ASP.NET application, it save them
I have situation when the current directory becomes invalid (ie, when some program deletes
I have situation in which i'm compelled to retrieve 30,000 records each to 2
I have situation. I have to create a Sports Club system in JAVA. There
i have situation in which i have some library projects, say DataProcessors,Lib2 , included
Possible Duplicate: What is my script src URL? I have situation: <script type=text/javascript src=http://server/some.js>
I have a situation in which I need to extract Data Annotations information from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.