开发者

Approaching singleton connection class

开发者 https://www.devze.com 2023-01-09 15:14 出处:网络
namespace AV.Connections { protected class MyConnection { protected ConnectionStringSettings connectionSettings
    namespace AV.Connections
{

    protected class MyConnection
    {

        protected ConnectionStringSettings connectionSettings
        {
            get { return ConfigurationManager.ConnectionStrings["mySQLConnection"]; }
        }

        protected DbConnection connection
        {
            get { return new OdbcConnection(connectionSettings.ConnectionString); }
        }

   开发者_如何学JAVA     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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消