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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:41:52+00:00 2026-05-17T23:41:52+00:00

I am using .NET 3.5 C#. I have a simple connection class with only

  • 0

I am using .NET 3.5 C#. I have a simple connection class with only two methods: OpenConnection() and CloseConnection(). I defined this class as Static so that I don’t have to create an instance while calling methods. I want to know whether:

(1) I should create an interface with method definitions for OpenConnection and CloseConnection and thereby use this Interface with Connection class. There is no reason to use an interface but I was thinking whether the Connection can be made more professional.

(2) Is it fine to declare this class as Static?

  • 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-17T23:41:52+00:00Added an answer on May 17, 2026 at 11:41 pm

    There are two entirely different approaches

    • Singleton: Single object across the application.

    In this case, you will have to take care of the locking mechanism as well.

    class Connection
    {
        public static Connection Instance() {
            if (_instance == null) {
                lock (typeof(Connection)) {
                    if (_instance == null) {
                        _instance = new Connection();
                    }
                }
            }
            return _instance;      
        }
        protected Connection() {}
        private static volatile Connection _instance = null;
    }
    
    • Implement IDisposable:

    Alternatively, you can implement IDisposable in your Connection class, and let it disposed automatically using the using keyword. For instance:

    using(Connection c = new Connection(SomeConfiguration)) //Opens the connection.
    {
        Something(c);
    }// Closes the connection. Dispose is implicitly called in the scope of the using statement.
    

    Or if you want Generic Connection class, then Marc has responded with an excellent database connection class example here.

    Regarding point 1; you can use interface; iff:

    • You want loose coupling, componentization, and maintainability in your code.

    • You want to provide guarantee that the classes shall behave exactly as methods/contracts defined in interface. For instance, in this case, if you extend your class from IDisposable, the interface imposes that not only objects can be disposed without getting compiler errors, but that they should be disposed. So, similar guarantees can be provided in your code by simply adhering to an interface.

    • You have a team that is going to working on a large module; and you want to keep code consistent; so you can define an interface and add some restrictions, that would help integrate the it easily. For instance:

    You know you have a module that is going to handle alot of connections, different types of connections – some of which may be identified later during the development. But what you know for sure is that all types of connection shall be able to Open and Close; and you also want all of the developers to stick to this rule. So you can come up with an interface like:

    interface IMyConnection
    {
        Open();//Opens the connection
        Close();//Closes the connection
    }
    
    • You expect have certain classes that does a lot of complex work and won’t be finished before the rest of the project; then rest of the project can use the interface and avoid being dependent on that class.

    • You plan to deploy your product commercially; and want it to be extendable by 3rd party developers.

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

Sidebar

Related Questions

I have a simple form with some plain html input like bellow using ASP.NET
I have a fairly simple ASP.NET 2.0 menu control using a sitemap file and
Using .NET 1.1, I have a DataGrid that contains three columns for each row.
I'm using .NET 3.5 and I have a class, A, marked as internal sealed
I'm using asp.net/c# and have a number of Collapsible Panel Extenders that have been
I'm using .NET 3.5. I have two string arrays, which may share one or
Using the .net framework you have the option to create temporary files with Path.GetTempFileName();
We have an application written in C#, using .NET Framework 3.0 or 3.5 or
I am in the start up of a project using ASP.NET MVC and have
How can I modify the PDF document properties programmatically using .NET code? I have

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.