Repository, Unit of Work, C#

220
06 июня 2018, 15:00

Есть два интерфейса репозитория один для получения данных, второй для добавления, редактирования и удаления.

public interface IRepository<T> : IDisposable where T : class, IReadDataRepository<T> 
{
    void Add(T entity);
    void Delete(T entity);
    void Edit(T entity);
    void Save();
}

public interface IReadDataRepository<T> : IDisposable where T : class
{
    IQueryable<T> GetAll();
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
}

Имеется класс репозиторий, где реализую интерфейсы репозиториев

public class Repository<T> : IRepository<T> where T : class, IReadDataRepository<T>
{
    Context context;
    public Repository(Context context)
    {
        this.context = context;
    }
    public void Add(T entity)
    {
        context.Set<T>().Add(entity);
    }
 ...
}

и Unit of Work с которым и возникла проблема

public class UnitOfWork : IUnitOfWork, IDisposable
{
    private Context context = new Context();
    private Repository.Repository<Student> studentRepository;
    private Repository.Repository<Course> courseRepository;
    private Repository.Repository<CourseStudent> courseStudentRepository;
    public UnitOfWork(Context context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("Context was not supplied");
        }
        this.context = context;
    }
 ...
}

Не пойму, что не так

Ошибка

Answer 1

В записи

public interface IRepository<T> : IDisposable where T : class, IReadDataRepository<T>

IReadDataRepository<T> является ограничением "T унаследован от IReadDataRepository<T>"

Переставьте местами, так, чтобы он означал интерфейс, реализуемый классом:

public interface IRepository<T> : IDisposable, IReadDataRepository<T> where T : class 

И просто уберите его упоминание в объявлении Repository<T> - там достаточно только указания IRepository<T>, оно включает в себя IReadDataRepository<T>:

public class Repository<T> : IRepository<T> where T : class 
READ ALSO
Не могу изменить стиль границ datagridview

Не могу изменить стиль границ datagridview

Пробую изменить тип границ, но не выходитЧто не так?Код:

280
Своя кнопка с 3 разными полями

Своя кнопка с 3 разными полями

Как создать свою кнопку с 3 разными полями?

218
Как вернуть custom json response в .net core web api?

Как вернуть custom json response в .net core web api?

Вот метод, который должен возвращать json вида { "status":"ready","timestamp":"2017-12-17"}

211