Singleton数据访问对象(Dao)& SQL Helper实例,这里有任何性能缺陷吗?

时间:2011-06-10 14:46:39

标签: data-access-layer dao sqlhelper

我需要专家的意见。我编写自己的SQL Helper类来与DB通信。

为什么我使用它,因为我试图

  1. 封装Ado.Net逻辑。
  2. 尝试在DAL编码方面为我的开发人员设置一个通用标准。
  3. 灵活&易于使用。
  4. 用于SQL Server / Oracle / Access / Excel /通用数据库代码块方法(SQL Server和Oracle)的相同类型的代码块e.t.c。
  5. Plug&播放或可重复使用的方法。
  6. 在代码优化方面

    1. 此助手类或程序集符合CLS。
    2. 通过FxCop /静态代码分析成功通过。
    3. 我给你样本代码块(DAO)。请检查下面的

                         using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Text;
              using System.Data;
      
              #region include SQL Helper Namespace
              using DBManager;
              #endregion
      
              #region include SQL Helper Namespace
              using DBManager;
              #endregion
      
      
      namespace ContentManagementSystem.DataAccessLayer
      {
      /// <summary>
      /// 
      /// </summary>
      public sealed class DaoContentOwner : DataAccessBase
      {
          #region CONSTRUCTOR
          /// <summary>
          /// 
          /// </summary>
          private DaoContentOwner()
          {
              GetInstance = new DaoContentOwner();
          }
          #endregion
      
          //############################################# M E T H O D S ##########################
      
          #region Retrieve Content Owner
          /// <summary>
          /// Retrieve Content Owner
          /// </summary>
          /// <returns></returns>
          public static DataTable RetrieveContentOwner()
          {
              DataTable dt = null;
      
              try
              {
                  using (DBQuery dq = new DBQuery("stpGetContentOwner"))
                  {
      
                      dt = dq.ResultSetAsDataTable();
      
                      return dt;
                  }
              }
              catch (Exception)
              {
                  throw;
              }
              finally
              {
                  if (dt != null)
                  {
                      dt.Dispose();
                  }
              }
          }
          #endregion
      
      
          //############################################# P R O P E R T Y ########################
      
          #region READ ONLY PROPERTY
          /// <summary>
          /// GetInstance of DaoContentOwner Class
          /// </summary>
          public static DaoContentOwner GetInstance { get; private set; }
          #endregion
      }
      

      }


      理由:

      DataAccessBase ”这是一个抽象类。实现IDisposable接口。

      对于仅限SQL Server

      DBQuery ”是 SQL Helper 。这是一个密封课程。根据需要需要T-SQL / SP。打开关闭数据库连接。没有必要由开发人员处理任何事情。

      为什么我要制作DAO Singleton类,因为我使用DAO的所有方法都是静态方法。我命令内存优化我使它成为Singleton。它也是设计负责人。

      注意:需要评论。是否需要改变设计或某些事情是错误的,需要纠正。

1 个答案:

答案 0 :(得分:0)

就个人而言,我会选择:

DALFileItem dalF = new DALFileItem(DSN);
List<FileItem> list = dalF.GetAll(""); 
  • 允许访问多个数据库,而无需创建DSN静态成员。
  • 在多线程环境中,只有一个线程可以访问静态方法。
  • 更多面向对象:您可以更轻松地添加接口,基类和泛型。