我需要专家的意见。我编写了自己的 SQL Helper 类来与 DB 通信。
为什么我使用它,因为我试图
- 封装 Ado.Net 逻辑。
- 尝试在 DAL 编码方面为我的开发人员设置一个通用标准。
- 灵活且易于使用。
- SQL Server / Oracle / Access / Excel / Generic Database 代码块方法(SQL Server & Oracle)等的相同代码块
- 即插即用或可重复使用的方法。
在代码优化方面
- 此帮助程序类或程序集符合 CLS。
- 它通过 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方法都是静态方法。我为了内存优化我把它做成单例。它也是一个设计校长。
注: 需要评论。是否需要更改设计或某些需要纠正的错误。