单例数据访问对象(Dao)& SQL Helper 实例,这里有性能缺陷吗?
我需要专家的意见。我编写自己的 SQL Helper 类来与数据库通信。
为什么我使用它是因为我尝试
- 封装 Ado.Net 逻辑。
- 尝试为我的开发人员制定 DAL 编码方面的通用标准。
- 灵活&便于使用。
- SQL Server / Oracle / Access / Excel / 通用数据库代码块方法(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 Helper,仅适用于SQL Server。这是一个密封类。根据需要采用T-SQL/SP。打开关闭数据库连接。无需开发商处理任何事情。
为什么我创建 DAO Singleton 类,因为我的 DAO 中的所有方法都是静态方法。我为了内存优化我把它做成了单例。它也是设计校长。
注意:需要评论。是否需要改变设计或某些地方是错误的需要纠正。
I need comments from Experts. I write my own SQL Helper Class to Communicate with DB.
Why I use it as because I try to
- Encapsulate the Ado.Net logic.
- Try to set a common standard for my developer in terms of DAL coding.
- Flexible & Easy to use.
- Same kind of code block for SQL Server/ Oracle / Access / Excel / Generic Database code block approach (SQL Server & Oracle) e.t.c.
- Plug & Play or Reusable approach.
In terms of code optimization
- This helper class or Assembly is CLS Compliant.
- It pass Successfully by FxCop / Static Code Analysis.
I give you the sample Code Block (DAO). Please check bellow
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
}
}
Justification:
"DataAccessBase" It is a Abstract class. Implement IDisposable Interface.
"DBQuery" is SQL Helper for SQL Server Only. It is a Sealed class. It is takes T-SQL / SP according to needs. Open an close database connection. No need to handel any thing by developer.
Why I make DAO Singleton Class, Because my all the methods withing DAO is a static method. I order to memory optimization I make it Singleton. It is also a design Principal.
Note: Needs comments. Whether need to change in the design or some thing is wrong that need to correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我个人会选择:
Personally I would go with: