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 777023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:32:28+00:00 2026-05-14T19:32:28+00:00

I have three methods: public void Save<T>(T entity) { using (new Transaction()) { Session.Save(entity);

  • 0

I have three methods:

public void Save<T>(T entity)
{
    using (new Transaction())
    {
        Session.Save(entity);
    }
}

public void Create<T>(T entity)
{
    using (new Transaction())
    {
        Session.Create(entity);
    }
}

public void Delete<T>(T entity)
{
    using (new Transaction())
    {
        Session.Delete(entity);
    }
}

As you can see, the only thing that differs is the method call inside the using block. How can I rewrite this so it’s something like this instead:

public void Save<T>(T entity)
{
    TransactionWrapper(Session.Save(entity));
}

public void Create<T>(T entity)
{
    TransactionWrapper(Session.Create(entity));
}

public void Save<T>(T entity)
{
    TransactionWrapper(Session.Save(entity));
}

So in other words, I pass a method call as a parameter, and the TransactionWrapper method wraps a transaction around the method call.

  • 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-14T19:32:29+00:00Added an answer on May 14, 2026 at 7:32 pm

    You can pass an Action<T> to a method to specify an action to execute:

    private void ExecuteInTransaction<T>(Action<T> action, T entity)
    {
        using (new Transaction())
        {
            action(entity);
        }
    }
    
    public void Save<T>(T entity)
    {
        ExecuteInTransaction(Session.Save, entity);
    }
    

    But IMO this isn’t a worthy improvement over your original code if your ExecuteInTransaction method isn’t more complex.

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

Sidebar

Related Questions

I have three database operations like so: public void Add<T>(T entity) { using (var
I have a method in c# like so. public void Save(int id) { SaveImage(first
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
My question is related to exception handling. If i have for example three methods
I have a convenience class for encoding URI's. In it I've created three methods
Lets say I have a test class called ServiceTest with three test methods test1,
I have three classes (House, Room, Address) which are persistent (Mapped with JPA/Hibernate) @Entity
I have the following BL method public static void SomeMethod (List<SomeClass> group) { IEnumerable<SomeClass>
i am new developer in android applications.i would like to save the data using
I have an JPA entity like this: @Entity @Table(name = category) public class Category

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.