接近单例连接类

时间:2010-07-27 15:46:38

标签: c# asp.net singleton

    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(); }
        }

    }
}

我试图创建可以返回连接对象以及其他相关对象的单例类。以上是我想到的方法。我确信我错了。请指出如何?或者如果不是,最好的办法是什么。

这不是Singleton ....我理解但我认为我不能声明类是静态的,因为它涉及连接。所以我对这种情况感到很困惑。

1 个答案:

答案 0 :(得分:0)

这不是单例(除非通过工厂类访问,并且只能维护此类的一个实例)。查看本文,了解如何实现单例模式:http://csharpindepth.com/Articles/General/Singleton.aspx