#StackBounty: #c# #oop #generics #abstract-class Issue calling a generic common repository's method

Bounty: 100

I have a base repository setup. I want to create a generic common repository to use a common method which returns a boolean called:

DoesRecordExist()

I have the base repo and common repo setup, but i’m having issue referencing the ICommonRepository in the service. How do I call this method?

BaseRepository:

   public abstract class BaseRepository<TModel> : IBaseRepository<TModel> where TModel : BaseClass
    {
        private readonly IDbContext _context;
        private readonly IValidator<TModel> _validator;

        public BaseRepository(IDbContext context, IValidator<TModel> validator = null)
        {
            _context = context;
            _validator = validator ?? new InlineValidator<TModel>();
        }

        public bool DoesRecordExist(Guid id)
        {
            return _context.Set<TModel>().Any(x => x.Guid == id);
        }
    }

CommonRepository:

 public  class CommonRepository<TModel> : BaseRepository<TModel> where TModel : BaseClass, ICommonRepository<TModel>
{
    private readonly IDbContext _context;
    private readonly IValidator<TModel> _validator;
    public CommonRepository(IDbContext context, IValidator<TModel> validator = null) : base(context, validator)
    {
        _context = context;
        _validator = validator ?? new InlineValidator<TModel>();
    }
    public bool CommonDoesRecordExist(Guid id)
    {
        return DoesRecordExist(id);
    }
}

GlobalService:

private readonly ICategoryRepository _categoryRepository;
private readonly ISubcategoryRepository _subCategoryRepository;
private readonly ISubcategoryDescriptionRepository _subcategoryDescriptionRepository;
private readonly ICommonRepository<??????> _commonRepository;

public GlobalDataService(
    ICategoryRepository categoryRepository, 
    ISubcategoryRepository subCategoryRepository, 
    ISubcategoryDescriptionRepository subcategoryDescriptionRepository, 
    ICommonRepository<????> commonRepository)
{
    _categoryRepository = categoryRepository;
    _subCategoryRepository = subCategoryRepository;
    _subcategoryDescriptionRepository = subcategoryDescriptionRepository;
    _commonRepository = commonRepository;
}

 public bool DoesUserRecordExist(Guid userId)
    {
        //PROBLEM ON THIS LINE... bool existingData = _commonRepository.CommonDoesRecordExist(userId); 
            if (existingData)
            {
                //do stuff
            }
            else
            {
                //do other stuff
            }
        }

ICommonRepository.cs

   public interface ICommonRepository<T> : IBaseRepository
    {
        bool CommonDoesRecordExist(Guid id);
    }

IBaseRepository.cs

public interface IBaseRepository<T> : IBaseRepository
{
    bool DeleteAll();
    bool DoesRecordExist(Guid id, Expression<Func<T, bool>> filter);
    List<T> GetAll();
    T GetOne(Guid id);
    T Save(T item);
    bool Delete(Guid id);
    bool Delete(T item);
    IQueryable<T> Include(params Expression<Func<T, object>>[] includes);

}

public interface IBaseRepository
{
    string CollectionName { get; }
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.