さまざまなアプリケーション間でコードを共有するために、社内で小さなフレームワークの開発を開始しています。データ アクセスには EF4 を使用しています。カスタム DbContext クラスと汎用リポジトリがあります。
public class RMDbContext : DbContext
{
// ....
}
public interface IRepository
{
IQueryable<T> All();
void Delete(T entity) where T : class;
void Add(T entity) where T : class;
void Update(T entity) where T : class;
int SaveChanges();
void RollbackChanges();
}
ここでの問題は、カスタム DbContext クラス (RMDbContext) を使用してリポジトリを実装する方法です。私の同僚は、RMDbContext に IRepository インターフェイスを実装させるのが最善の方法だと考えています。
public class RMDbContext : DbContext, IRepository
{
// ....
}
正直なところ、コンテキストが特定のコントラクト (IRepository) に関連付けられているため、このアプローチは好きではありません。IMO RMDbContext を使用するリポジトリ実装を作成することをお勧めします。次のようになります。
public class Repository<T> : IRepository where T : RMDbContext, new()
{
protected readonly RMDbContext context;
public class Repository()
{
context = new T();
}
// ....
}
これら2つのアプローチについてどう思いますか?どちらを選びますか。なぜですか。