Data Access Application Block用のとっても良い感じのツールが登場しました。
「The Data Tier Generator Tool」
どうやらメキシコのMSMVPカップルが開発したらしいんですが、これはTableDataGatewayパターンをモデルにしてデータ層におけるDALC(Data Access Layer Compornet)クラスを任意のデータベースのストアドプロシージャに合わせて自動生成するという優れものです。
例えばpubsデータベースを登録すると、Employeeテーブルに該当するEmployee.csファイルが生成されます。(サンプルコードは追記を参照)
実は近日中に「Enterprise Library概説」の連載第2回が公開される予定なんですが、ぶっちゃけた話第3回でやりたいことが色々とありすぎて困ってます。
基本はこのツールで生成されたコードのようなTableDataGetewayか行DataGatewayの実装をサンプル付きで従来のADO.NETネイティブな場合とEntLibを利用した場合とで比較して見てもらうのが良いかなぁ〜とも思いましたが、やはりData Mapping Application Blockは参考程度でも掲載したいし、このツールや他にもDynamicDatabaseFactory Extensionもあるし、DAAB用のConfigコンソール拡張もいくつか出てるしぃ〜これだけで2,3回は連載出来ちゃうよ(^^;
う〜ん、EntLib1.1リリースが間に合えばやっぱりDataAccessApplicationBlockとDataMappingApplicationBlockを併用して今までのDataAccessLayerCompornentの設計/実装の常識を覆してやりたいなぁ〜って欲もあったり(^^;
インピーダンスミスマッチ問題やらビジネスロジックからのCRUD操作の隠蔽などなどDALC周りの設計は何かと難解を極めることも多いんですが、データ層を担当するApplicationBlock、ツール(又はそこに興味を持つ開発者達)もこの辺りを重点的に意識して動いているという印象を受けますね。
あ、ツールが自動生成したコードのサンプルを追記に書いときます。
全て静的メソッドなMonoStateなクラスなんですが、コンストラクタがちゃっかりprivateになっているところがお利口さんですね(^^;
namespace TableGateway
{
public sealed class Employee
{
private Employee() {}
///
/// Inserts a record into the employee table.
///
public static void Insert(string emp_id, string fname, string minit, string lname, short job_id, byte job_lvl, string pub_id, DateTime hire_date)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeInsert");
myCommand.AddInParameter("@emp_id", DbType.String, emp_id);
myCommand.AddInParameter("@fname", DbType.String, fname);
myCommand.AddInParameter("@minit", DbType.String, minit);
myCommand.AddInParameter("@lname", DbType.String, lname);
myCommand.AddInParameter("@job_id", DbType.Int16, job_id);
myCommand.AddInParameter("@job_lvl", DbType.Byte, job_lvl);
myCommand.AddInParameter("@pub_id", DbType.String, pub_id);
myCommand.AddInParameter("@hire_date", DbType.DateTime, hire_date);
myDatabase.ExecuteNonQuery(myCommand);
}
///
/// Updates a record in the employee table.
///
public static void Update(string emp_id, string fname, string minit, string lname, short job_id, byte job_lvl, string pub_id, DateTime hire_date)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeUpdate");
myCommand.AddInParameter("@emp_id", DbType.String, emp_id);
myCommand.AddInParameter("@fname", DbType.String, fname);
myCommand.AddInParameter("@minit", DbType.String, minit);
myCommand.AddInParameter("@lname", DbType.String, lname);
myCommand.AddInParameter("@job_id", DbType.Int16, job_id);
myCommand.AddInParameter("@job_lvl", DbType.Byte, job_lvl);
myCommand.AddInParameter("@pub_id", DbType.String, pub_id);
myCommand.AddInParameter("@hire_date", DbType.DateTime, hire_date);
myDatabase.ExecuteNonQuery(myCommand);
}
///
/// Deletes a record from the employee table by a composite primary key.
///
public static void Delete(string emp_id)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeDelete");
myCommand.AddInParameter("@emp_id", DbType.String, emp_id);
myDatabase.ExecuteNonQuery(myCommand);
}
///
/// Deletes a record from the employee table by a foreign key.
///
public static void DeleteByJob_id(short job_id)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeDeleteByJob_id");
myCommand.AddInParameter("@job_id", DbType.Int16, job_id);
myDatabase.ExecuteNonQuery(myCommand);
}
///
/// Deletes a record from the employee table by a foreign key.
///
public static void DeleteByPub_id(string pub_id)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeDeleteByPub_id");
myCommand.AddInParameter("@pub_id", DbType.String, pub_id);
myDatabase.ExecuteNonQuery(myCommand);
}
///
/// Selects a single record from the employee table.
///
public static IDataReader Select(string emp_id)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeSelect");
myCommand.AddInParameter("@emp_id", DbType.String, emp_id);
return myDatabase.ExecuteReader(myCommand);
}
///
/// Selects all records from the employee table.
///
public static IDataReader SelectAll()
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeSelectAll");
return myDatabase.ExecuteReader(myCommand);
}
///
/// Selects all records from the employee table by a foreign key.
///
public static IDataReader SelectByJob_id(short job_id)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeSelectByJob_id");
myCommand.AddInParameter("@job_id", DbType.Int16, job_id);
return myDatabase.ExecuteReader(myCommand);
}
///
/// Selects all records from the employee table by a foreign key.
///
public static IDataReader SelectByPub_id(string pub_id)
{
Database myDatabase = DatabaseFactory.CreateDatabase();
DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("employeeSelectByPub_id");
myCommand.AddInParameter("@pub_id", DbType.String, pub_id);
return myDatabase.ExecuteReader(myCommand);
}
}
}
Posted by GAMMARAY at 2005年06月10日 00:06
| TrackBack