Tutorial :Approaching singleton connection class



Question:

    namespace AV.Connections  {        protected class MyConnection      {            protected ConnectionStringSettings connectionSettings          {              get { return ConfigurationManager.ConnectionStrings["mySQLConnection"]; }          }            protected DbConnection connection          {              get { return new OdbcConnection(connectionSettings.ConnectionString); }          }            protected DbCommand command          {              get { return connection.CreateCommand(); }          }            protected DbParameter parameter          {              get { return command.CreateParameter(); }          }        }  }  

I was trying to create singleton class which could return connection objects as well as other related objects. The above was the approach I thought of. I am sure I am wrong somehow. Please point out the how? Or if not what's the best approach for this.

This is not Singleton....I understand but I think I can't declare the class static as it involves connections. So I am really confused about this situation.


Solution:1

That isn't a singleton (unless it's accessed through a factory class with responsbility to maintain only one instance of this class). Check out this article for how to implement the singleton pattern: http://csharpindepth.com/Articles/General/Singleton.aspx


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »