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

  • Home
  • SEARCH
  • 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 544493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:39:39+00:00 2026-05-13T10:39:39+00:00

I have a repository like so: public interface IRepository { void Save<T>(T entity); void

  • 0

I have a repository like so:

public interface IRepository
{
    void Save<T>(T entity);
    void Create<T>(T entity);
    void Update<T>(T entity);
    void Delete<T>(T entity);
    IQueryable<T> GetAll<T>();
}

My question is, where should my transaction boundaries be? Should I open a new transaction on every method and commit it before returning? Or should it be around the entire repository so that the transaction is only committed when the repository is disposed/garbage collected?

  • 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. Editorial Team
    Editorial Team
    2026-05-13T10:39:39+00:00Added an answer on May 13, 2026 at 10:39 am

    Unit of Work is definitely the way to go. If you’re using SQL Server with a local database, TransactionScope will do most of the heavy lifting for you; as long as you share sessions between repositories (which you’re doing through constructor injection, right…?), then you can nest these to your heart’s content. By default, it enlists in the “ambient” transaction if there is one, and starts a new one if there isn’t, which is exactly the behaviour you want for a unit of work.

    So your repositories might look like this:

    public class UserRepository : IUserRepository
    {
        public UserRepository(ISession session)
        {
            this.Session = session;
        }
    
        public void Save(User user)
        {
            using (TransactionScope tsc = new TransactionScope())
            {
                Session.Save(user);
                tsc.Complete();
            }
        }
    
        protected ISession Session { get; set; }
    }
    
    public class OrderRepository : IOrderRepository
    {
        public OrderRepository(ISession session)
        {
            this.Session = session;
        }
    
        public void Save(Order order)
        {
            using (TransactionScope tsc = new TransactionScope())
            {
                Session.Save(order);
                tsc.Complete();
            }
        }
    
        protected ISession Session { get; set; }
    }
    

    Then you can perform the complete unit of work like so:

    User currentUser = GetCurrentUser();
    using (TransactionScope tsc = new TransactionScope())
    {
        ISession session = SessionFactory.OpenSession();
    
        Order order = new Order(...);
        order.User = currentUser;
        IOrderRepository orderRepository = GetOrderRepository(session);
        orderRepository.Save(order);
    
        currentUser.LastOrderDate = DateTime.Now;
        IUserRepository userRepository = GetUserRepository(session);
        userRepository.Save(currentUser);
    
        tsc.Complete();
    }
    

    If you don’t like TransactionScope or your environment prevents you from using it effectively then you can always implement your own UOW or use an existing implementation. Or if you’re just an architectural neat-freak then you can do both – use a generic unit-of-work interface with one of the main DI libraries, and implement your concrete UOW using the TransactionScope.

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

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The database isn't automagically efficient either; you actually need to… May 14, 2026 at 4:46 pm
  • Editorial Team
    Editorial Team added an answer There's nothing special about glTexCoord*i, it's just one variant of… May 14, 2026 at 4:45 pm
  • Editorial Team
    Editorial Team added an answer In framework 3.5, dt.Rows.Cast<System.Data.DataRow>().Take(n) Otherwise the way you mentioned May 14, 2026 at 4:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.