Problem z IRepository

0

Cześć. Stworzyłem sobie IRepository jest to interface z deklaracjami metod CRUD. Wygląda to tak :

    public interface IRepository<T>
    {
        Task<T> GetAsync(long id);
        Task<IReadOnlyList<T>> GetAllAsync();
        Task AddAsync(T entity);
        Task UpdateAsync(T entity);
        Task DeleteAsync(T entity);
    }

Mam też klasę TenureRepository która implementuje ją :

public class TenureRepository : IRepository<Tenure>, ITenureRepository
    {
        private readonly ApplicationDbContext db;

        public TenureRepository(ApplicationDbContext _db)
        {
            db = _db;
        }

        public async Task AddAsync(Tenure entity)
        {
            db.Tenures.Remove(entity);
            db.SaveChanges();
            await Task.CompletedTask;
        }

        public async Task DeleteAsync(Tenure entity)
        {
            db.Tenures.Remove(entity);
            db.SaveChanges();
            await Task.CompletedTask;
        }

        public async Task<IReadOnlyList<Tenure>> GetAllAsync()
        {
            return await db.Tenures.ToListAsync();
        }

        public async Task<Tenure> GetAsync(long id)
        {
            return await db.Tenures.FirstOrDefaultAsync(x => x.Id == id);
        }

        public async Task UpdateAsync(Tenure entity)
        {
            db.Entry(entity).State = EntityState.Modified;
            db.SaveChanges();
            await Task.CompletedTask;
        }
    }

Teraz stwprzyłem sobie serwis TenureService i chciałbym pobrać metodę GetAllAsync ale jej nie znajduje dlaczego ?
wygląda to tak:

    internal class TenureBusinessService : ITenureBusinessService
    {
        private readonly IMapper mapper;
        private readonly ITenureRepository tenureRepository;

        public TenureBusinessService(IMapper _mapper, ITenureRepository _tenureRepository)
        {
            mapper = _mapper;
            tenureRepository = _tenureRepository;
        }
        public async Task<IReadOnlyList<TenureListDto>> GetAllAsync()
        {
            var payload = await tenureRepository.GetAllAsync(); // Tutaj jest błąd
        }
    }
1

Pewnie dlatego, że ITenureRepository nie ma tej metody. Ale tego nie wiemy, bo jak wygląda ten interfejs, to nie pokazałeś.

0

o przepraszam już dodaje :)

    public interface ITenureRepository
    {
    }

Jak go zedytować aby dobrze to wyglądało ?

2

albo tak

public interface ITenureRepository<T> : IRepository<T>
{
}

albo

public interface ITenureRepository
{
 Task<TenureListDto> GetAsync(long id);
}

Jesteś pewny, że jest Ci potrzebny ten antypatern Irepository?

1

Czyli tak jak myślałem. Nie masz tej metody w tym interfejsie.
Zmień typ pola tenureRepository w TenureBusinessService na TenureRepository albo IRepository<Tenure>.

A najlepiej nie przesadzaj z tyloma interfejsami, repozytorium też Ci raczej w niczym nie pomaga.

1 użytkowników online, w tym zalogowanych: 0, gości: 1