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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:23:03+00:00 2026-06-16T00:23:03+00:00

I am working on a unit of work class and I’m curious how the

  • 0

I am working on a unit of work class and I’m curious how the connection should be handled. My repositories take in a unit of work, and use that connection for Get() commands.

Obviously, Commit() will handle all Add, Updates and Deletes. This would open the connection and begin the transaction and close when finished. How should Gets be handled?

Should the UOW, open the connection in the constructor and close when completely finished? Meaning while I pass UOW from repo to repo, the connection is open. Or should I be opening and closing it only as needed?

Approach #1: Unit of work opens connection and connection remains open until processing is finished?

public UnitOfWork(IDbConnection connection)
{
    Connection = connection;
    Connection.Open();
    Transaction = Connection.BeginTransaction();
}

Approach #2: Snippet of a Get method that opens right before read and closes right after. If passing to multiple repos, same connection is used, just opened and closed a bunch.

 using (var reader = manager.GetReader())
 {
     UOW.Connection.Open();

     while (reader.Read())
         list.Add(factory.CreateTFromReader(reader));

     UOW.Connection.Close();
 }
  • 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-06-16T00:23:04+00:00Added an answer on June 16, 2026 at 12:23 am

    No, you should always open the connection when needed and be absolutely sure to close it when the work is complete. The Connection Pooling mechanism will take care of keeping the connection available for your current user or for other users connecting concurrently on the same server

    I think also that the second example is not quite right. You should have something like this

     using (IDbConnection cn = manager.GetConnection)
     using (var reader = manager.GetReader())
     {
         cn.Open();
    
         while (reader.Read())
             list.Add(factory.CreateTFromReader(reader));
     }
    

    This will ensure that the connection is closed even in the event of exceptions, and that is returned to the pool in order to be reused

    Connection Pooling

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

Sidebar

Related Questions

I was working on a Unit of Work implementation that works both in Entity
I am working with a unit-testing suite that hijacks function calls and tests expected
now i'm working on a project for creating audio unit instrument that provide the
I've been trying to get started with unit-testing while working on a little cli
At the moment I'm working on Asp.Net MVC using: Repository, Unit-Of-Work patterns, Service Layer
I'm running into an issue while unit-testing a Python project that I'm working on
We are working using the Unit Of Work pattern combined with the Generic Repository
so I'm currently creating a unit test for a class that I've implemented a
I'm writing a unit test for a Grails controller that renders a domain class
When working with Domain Objects, how do you typically unit test a method that

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.