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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:03:46+00:00 2026-05-12T05:03:46+00:00

Okay, so here’s a problem I’m running into. I have some classes in my

  • 0

Okay, so here’s a problem I’m running into.

I have some classes in my application that have methods that require a database connection. I am torn between two different ways to design the classes, both of which are centered around dependency injection:

  1. Provide a property for the connection that is set by the caller prior to method invocation. This has a few drawbacks.

    • Every method relying on the connection property has to validate that property to ensure that it isn’t null, it’s open and not involved in a transaction if that’s going to muck up the operation.

    • If the connection property is unexpectedly closed, all the methods have to either (1.) throw an exception or (2.) coerce it open. Depending on the level of robustness you want, either case is appropriate. (Note that this is different from a connection that is passed to a method in that the reference to the connection exists for the lifetime of the object, not simply for the lifetime of the method invocation. Consequently, the volatility of the connection just seems higher to me.)

    • Providing a Connection property seems (to me, anyway) to scream out for a corresponding Transaction property. This creates additional overhead in the documentation, since you’d have to make it fairly obvious when the transaction was being used, and when it wasn’t.

    On the other hand, Microsoft seems to favor the whole set-and-invoke paradigm.

  2. Require the connection to be passed as an argument to the method. This has a few advantages and disadvantages:

    • The parameter list is naturally larger. This is irksome to me, primarily at the point of call.

    • While a connection (and a transaction) must still be validated prior to use, the reference to it exists only for the duration of the method call.

    • The point of call is, however, quite clear. It’s very obvious that you must provide the connection, and that the method won’t be creating one behind your back automagically.

    • If a method doesn’t require a transaction (say a method that only retrieves data from the database), no transaction is required. There’s no lack of clarity due to the method signature.

    • If a method requires a transaction, it’s very clear due to the method signature. Again, there’s no lack of clarity.

    • Because the class does not expose a Connection or a Transaction property, there’s no chance of callers trying to drill down through them to their properties and methods, thus enforcing the Law of Demeter.

I know, it’s a lot. But on the one hand, there’s the Microsoft Way: Provide properties, let the caller set the properties, and then invoke methods. That way, you don’t have to create complex constructors or factory methods and the like. Also, avoid methods with lots of arguments.

Then, there’s the simple fact that if I expose these two properties on my objects, they’ll tend to encourage consumers to use them in nefarious ways. (Not that I’m responsible for that, but still.) But I just don’t really want to write crappy code.

If you were in my shoes, what would you do?

  • 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-12T05:03:46+00:00Added an answer on May 12, 2026 at 5:03 am

    Here is a third pattern to consider:

    • Create a class called ConnectionScope, which provides access to a connection
    • Any class at any time, can create a ConnectionScope
    • ConnectionScope has a property called Connection, which always returns a valid connection
    • Any (and every) ConnectionScope gives access to the same underlying connection object (within some scope, maybe within the same thread, or process)

    You then are free to implement that Connection property however you want, and your classes don’t have a property that needs to be set, nor is the connection a parameter, nor do they need to worry about opening or closing connections.

    More details:

    • In C#, I’d recommend ConnectionScope implement IDisposable, that way your classes can write code like “using ( var scope = new ConnectionScope() )” and then ConnectionScope can free the connection (if appropriate) when it is destroyed
    • If you can limit yourself to one connection per thread (or process) then you can easily set the connection string in a [thread] static variable in ConnectionScope
    • You can then use reference counting to ensure that your single connection is re-used when its already open and connections are released when no one is using them

    Updated: Here is some simplified sample code:

    public class ConnectionScope : IDisposable
    {
       private static Connection m_Connection;
       private static int m_ReferenceCount;
    
       public Connection Connection
       {
          get
          {
              return m_Connection;
          }
       }
    
       public ConnectionScope()
       {
          if ( m_Connection == null )
          {
              m_Connection = OpenConnection();
          }
          m_ReferenceCount++;
       }
    
       public void Dispose()
       {
          m_ReferenceCount--;
          if ( m_ReferenceCount == 0 )
          {
             m_Connection.Dispose();
             m_Connection = null;
          }
       }
    }
    

    Example code of how one (any) of your classes would use it:

    using ( var scope = new ConnectionScope() )
    {
       scope.Connection.ExecuteCommand( ... )
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer \d{3}:[a-z]{3}T\d\d-\d{3} 3 digits, colon, 3 letters (if you need uppercase… May 13, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer Your recent comments indicate that you will only be keeping… May 13, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer To only see the error output messages, redirect the standard… May 13, 2026 at 7:30 pm

Related Questions

Okay, so here's my problem: We use FOP for creating pretty report output. We
Okay, so here's a problem I'm running into. I have some classes in my
Okay, so here's the scenario. Project A has a class library developed for it
okay, so here is what im doing: class Connection { public int SERVERID; private
Okay so here's what I'm doing. I'm making a request to a server to

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.