У меня есть базовая настройка хранилища. Я хочу создать общий общий репозиторий для использования общего метода, который возвращает логическое значение с именем:
DoesRecordExist ()
У меня есть базовое репо и настройка общего репо, но у меня проблема со ссылкой на ICommonRepository в сервисе. Как мне вызвать этот метод?
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; }
}