2017-09-03 7 views
0

リポジトリパターンと作業単位を使用してMVCプロジェクトを試しています。リポジトリからIRepository UOWリポジトリパターンに変換できません

次は私のInitOfWorkから

public interface IUnitOfWork 
{ 
    IRepository<User> UserRepository { get; } 
    void Save(); 
} 

であり、これはUnitOfWorkの中に次の行が「エラーが発生したUnitOfWork

public class UnitOfWork:IUnitOfWork, IDisposable 
{ 
    private JNContext context = new JNContext(); 
    private bool disposed = false; 


    private IRepository<User> userRepository; 
    public IRepository<User> UserRepository 
    { 
     get 
     { 
      if (this.userRepository == null) 
      { 
       this.userRepository = new Repository<User>(this.context); 
      } 

      return this.userRepository; 
     } 
    } 

    public void Save() 
    { 
     this.context.SaveChanges(); 
    }} 

からである暗黙のうちにリポジトリからIRepository

this.userRepository = new Repository<User>(this.context); 
を変換できません。

私は何が欠けています。私は答えを見つけることができず、私は一日中立ち往生しています。

+0

あなたはリポジトリがIRepositoryを実装していることを、確信していますか? – wheeler

+0

@wheeler:いいえ、そうではありませんでした。私は**公開クラスリポジトリを持っていたところで、TEntity:クラス**。 ** IRepository **を実装する必要がありますか? –

+0

はい、IRepositoryを実装する必要があります。リポジトリにある。コンパイラはこれがなければそれを理解することはできません。 – RedgoodBreaker

答えて

0

は、ここでそれについての良い記事を見つけたこの

public interface IRepository<T> where T : class 
{ 
    IQueryable<T> Entities { get; } 
    void Remove(T entity); 
    void Add(T entity); 
} 
public class GenericRepository<T> : IRepository<T> where T : class 
{ 
    private readonly MyDbContext _dbContext; 
    private IDbSet<T> _dbSet => _dbContext.Set<T>(); 
    public IQueryable<T> Entities => _dbSet; 
    public GenericRepository(MyDbContext dbContext) 
    { 
     _dbContext = dbContext; 
    } 
    public void Remove(T entity) 
    { 
     _dbSet.Remove(entity); 
    } 
    public void Add(T entity) 
    { 
     _dbSet.Add(entity); 
    } 
} 

のようなものを試してみてください:https://medium.com/@utterbbq/c-unitofwork-and-repository-pattern-305cd8ecfa7a

関連する問題