私はデータアクセスクラスの基本クラスを持っています。このクラスはIDisposableを実装します。この基本クラスにはIDbConnectionが含まれ、コンストラクタでインスタンス化されます。IDbCommandはIDisposableを実装するクラス内から処理されますか?
:FooDALは処分が、このようなコードでFooDALに呼び出されることを保証するために用いたパターンを使用して使用public class FooDAL : DALBase
{
public int CreateFoo()
{
// Notice that the cmd here is not wrapped in a using or try-finally.
IDbCommand cmd = CreateCommand("create foo with sql", cn);
Open();
int ident = int.Parse(cmd.ExecuteScalar().ToString());
Close();
cmd.Dispose();
return ident;
}
}
クラス:このクラスから継承し
public class DALBase : IDisposable
{
protected IDbConnection cn;
public DALBase()
{
cn = new MySqlConnection(connString);
}
public void Dispose()
{
if (cn != null)
{
if (cn.State != ConnectionState.Closed)
{
try
{
cn.Close();
}
catch
{
}
}
cn.Dispose();
}
}
}
クラスは、実際にデータベースにアクセスします
using(FooDAL dal = new FooDAL())
{
return dal.CreateFoo();
}
私の質問は、IDbCommandが使用パターンでラップされていなくても正しく処理されていることを保証しますか?コマンドの実行中に例外が発生した場合はどうなりますか?
また、パフォーマンス上の理由から、基本クラスのコンストラクタではなくCreateFooで接続をインスタンス化する方が良いでしょうか?
何か助けていただければ幸いです。
副次的に、IDisposableフィールドを持つクラスがある場合、このクラスもIDisposableでなければなりません(http://msdn.microsoft.com/en-us/library/ms182172%28v=vs.80を参照)。 %29.aspx) – yorah