0

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

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

  1. 封装 Ado.Net 逻辑。
  2. 尝试在 DAL 编码方面为我的开发人员设置一个通用标准。
  3. 灵活且易于使用。
  4. SQL Server / Oracle / Access / Excel / Generic Database 代码块方法(SQL Server & Oracle)等的相同代码块
  5. 即插即用或可重复使用的方法。

在代码优化方面

  1. 此帮助程序类或程序集符合 CLS。
  2. 它通过 FxCop / 静态代码分析成功通过。

我给你示例代码块 (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 接口。

DBQuery ”是仅用于SQL Server的SQL Helper。这是一个密封类。根据需要采用T-SQL/SP。打开关闭的数据库连接。无需开发人员处理任何事情。

为什么我做DAO单例类,因为我所有的DAO方法都是静态方法。我为了内存优化我把它做成单例。它也是一个设计校长。

注: 需要评论。是否需要更改设计或某些需要纠正的错误。

4

1 回答 1

0

我个人会选择:

DALFileItem dalF = new DALFileItem(DSN);
List<FileItem> list = dalF.GetAll(""); 
  • 允许访问多个数据库,而无需使 DSN 成为静态成员。
  • 在多线程环境中,只有一个线程可以访问静态方法。
  • 更加面向对象:您可以通过这种方式更轻松地添加接口、基类和泛型。
于 2011-06-10T15:15:13.380 回答