専門家からのコメントが必要です。DBと通信するための独自のSQLヘルパークラスを作成します。
なぜ私がしようとしているのでそれを使うのか
- 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」はSQLServer専用のSQLヘルパーです。封印されたクラスです。必要に応じてT-SQL/SPが必要です。データベース接続を閉じます。開発者が何かを手渡す必要はありません。
DAOを使用するすべてのメソッドが静的メソッドであるため、DAOシングルトンクラスを作成する理由。私はそれをシングルトンにするメモリ最適化を注文します。デザインプリンシパルでもあります。
注: コメントが必要です。デザインを変更する必要があるのか、それとも修正する必要のある何かが間違っているのか。