2012-08-28 12 views
5

私はEF/Repository/Unit of Workを使用していますが、いくつかの詳細を理解するのは苦労しています。 UnitOfWorkの中で、新しいEF DbContext(EmmaContext)を作成しますが、リポジトリ内を見て、間違っていると思ってキャストします。リポジトリ内のコンテキストを正しく取得するにはどうすればいいですか?たぶん私は間違った道を歩いていますか?ここでエンティティフレームワークと作業単位

は私のUnitOfWorkである:ここでは

//Interface 
public interface IUnitOfWork : IDisposable 
{ 
    void Commit(); 
} 

//Implementation 
public class UnitOfWork : IUnitOfWork 
{ 
    #region Fields/Properties 
    private bool isDisposed = false; 
    public EmmaContext Context { get; set; } 
    #endregion 

    #region Constructor(s) 
    public UnitOfWork() 
    { 
     this.Context = new EmmaContext(); 
    } 
    #endregion 

    #region Methods 
    public void Commit() 
    { 
     this.Context.SaveChanges(); 
    } 

    public void Dispose() 
    { 
     if (!isDisposed) 
      Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    private void Dispose(bool disposing) 
    { 
     isDisposed = true; 
     if (disposing) 
     { 
      if (this.Context != null) 
       this.Context.Dispose(); 
     } 
    } 
    #endregion 
} 

はリポジトリです:こので

//Interface 
public interface IRepository<TEntity> where TEntity : class 
{ 
    IQueryable<TEntity> Query(); 
    void Add(TEntity entity); 
    void Attach(TEntity entity); 
    void Delete(TEntity entity); 
    void Save(TEntity entity); 
} 

//Implementation 
public abstract class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class 
{ 
    #region Fields/Properties 
    protected EmmaContext context; 
    protected DbSet<TEntity> dbSet; 
    #endregion 

    #region Constructor(s) 
    public RepositoryBase(IUnitOfWork unitOfWork) 
    { 
     this.context = ((UnitOfWork)unitOfWork).Context; 
     this.dbSet = context.Set<TEntity>(); 
    } 
    #endregion 

    #region Methods 
    public void Add(TEntity entity) 
    { 
     dbSet.Add(entity); 
    } 

    public void Attach(TEntity entity) 
    { 
     dbSet.Attach(entity); 
    } 

    public void Delete(TEntity entity) 
    { 
     dbSet.Remove(entity); 
    } 

    public IQueryable<TEntity> Query() 
    { 
     return dbSet.AsQueryable(); 
    } 

    public void Save(TEntity entity) 
    { 
     Attach(entity); 
     context.MarkModified(entity); 
    } 
    #endregion 
} 

答えて

4

サム:私は通常、具体的なリポジトリがctorのコンクリートのUnitOfWorkを服用して快適に感じる:

public RepositoryBase(UnitOfWork unitOfWork) 
    { 
     this.context = unitOfWork.Context; 
     this.dbSet = context.Set<TEntity>(); 
    } 

リポジトリとUOWは、通常のコンサートで作業し、お互いについて少し知っておく必要があります。

もちろん、これらのクラスを使用するコードは、インターフェイス定義についてのみ認識し、具体的な型については認識しません。

+0

これは基本的に私がやったことですが、IUnitOfWorkインターフェイスを追加しましたが、単なるユニットテストのように思いますか?あなたは本当にあなたのリポジトリと作業ユニットをユニットテストするつもりですか? – Sam

+0

少し考えた後、実装を呼び出しコードに抽象化しても、IoCコンテナからリポジトリにコンテキストを取得する方法が不思議でしたか?コードサンプルがありますか?ありがとう! BTW - 私はちょうどあなたのMVC 3.0の本、素晴らしい本を読んで! – Sam

+0

@Sam - ありがとう:) – OdeToCode

1

はあなたのリポジトリベースでIUnitOfWorkインタフェースを実装する必要があることを述べています。

こちらがお役に立てば幸いです。 よろしく

4

これはbest article I've readです。その例では、彼らはこのようなリポジトリを管理

private SchoolContext context = new SchoolContext(); 
    private GenericRepository<Department> departmentRepository; 
    private GenericRepository<Course> courseRepository; 

    public GenericRepository<Department> DepartmentRepository 
    { 
     get 
     { 

      if (this.departmentRepository == null) 
      { 
       this.departmentRepository = new GenericRepository<Department>(context); 
      } 
      return departmentRepository; 
     } 
    } 

仕事のあなたのユニットは、コンテキストを保持しており、それがリポジトリを参照する必要がある場合、それが作成されていない場合、それを作成し、それが保持している文脈で渡されます。

この記事では、通常のMVCコントローラ実装を作業パターンの単位に変換する方法についても説明します。

+0

すばらしい記事!リンクありがとう。 –

+7

悪い記事! Doツールで両方を注入するのではなく、ユニットワーククラス内にコンテキストとリポジトリを作成します。 – Elisabeth

0

Unit of Work
Repository

たUnitOfWorkは、アトミック操作を管理するためのものです。

リポジトリは、データストアに保持されているオブジェクトのセットと、それらのオブジェクトに対して実行される操作をカプセル化します。

UnitOfWork +リポジトリパターンを実装しない場合よりも、コンテキストまたはUnitOfWorkを渡すと、UnitOfWorkの責任から引き抜くことになります。あなたはそれを必要としません。

DbSetを渡すだけの場合の正しい実装。実際にはもっと必要はありません。

関連する問題