2016-09-21 5 views
-1

とデータアクセス層を生成します。たとえば、INSERTなどのすべてのステートメントとそれを実装するC#メソッド私はいくつかの助けやT4テンプレートと全体のデータアクセス層を生成するには、もう少し説明するポインタを探していたT4テンプレート

+1

。 @Marco Munnik – zahed

+1

あなたがEFを使用したくない理由は? EFはすでにリポジトリパターン/作業単位を実装しているため、これは必要ありません。このような「リポジトリ」構造を持つ –

答えて

1

あなたは、代わりに一般的なリポジトリのパターンを試してみてください、あなたはあなたのモデルに任意のタイプのために使用することができるジェネリックを使用して単一の実装を持つ単一のインターフェイスで終わるだろうことをやるべきではありません。

public interface IRepository<T, K> where T : class 
    { 
     T Add(T item); 
     bool Update(T item); 
     bool DeleteById(K id); 
    } 

実装あなたはデータアクセス層を生成しているのはなぜ

public class EFRepository<T, K> : IRepository<T, K>, IDisposable where T : class 
    { 
     protected readonly DbContext _dbContext; 
     private readonly DbSet<T> _entitySet; 

     public EFRepository(DbContext context) 
     { 
      _dbContext = context; 
      _entitySet = _dbContext.Set<T>(); 
     } 

     public T Add(T item) 
     { 
      item = _entitySet.Add(item); 
      _dbContext.SaveChanges(); 
      return item; 
     } 

     public bool Update(T item) 
     { 
      _entitySet.Attach(item); 
      _dbContext.Entry(item).State = EntityState.Modified; 
      _dbContext.SaveChanges(); 
      return true; 
     } 

     public bool DeleteById(K id) 
     { 
      var item = _entitySet.Find(id); 
      _entitySet.Remove(item); 
      _dbContext.SaveChanges(); 
      return true; 
     } 
} 
+1

ラッピングEFは無用です。この機能は 'DbSet'と' DbContext'によって既に提供されています。 – Stijn

+2

@Haitham Shaddad私は同じ言葉を使います。「あなたはそれを行うべきではありません」http://rob.conery.io/2014/03/04/repositories-and-unitofwork-are-not-a-good-idea /機能@Stijn –

+2

DbSetに存在していますが、あなたがEntityFrameworkを削除したい場合にも、ドメインサービスまたはUI層に直接それを使用し、別のデータアクセス層を使用することはできません、それは大きなrefactoryingなしで行うことはできません –

関連する問題