调用通用公共存储库方法的问题

Issue calling a generic common repository's method

我有一个基本存储库设置。我想创建一个通用的通用存储库以使用一个通用方法,该方法 returns 一个布尔值叫做:

DoesRecordExist()

我有基础回购和公共回购设置,但我在引用服务中的 ICommonRepository 时遇到问题。如何调用此方法?

基础资料库:

   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);
        }
    }

公共存储库:

 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);
    }
}

全球服务:

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; }
}

您不能使用父 class 引用直接调用子 class 的方法。您需要将 _commonRepository 转换为 CommonRepository

但是,如果CommonDoesRecordExist只有returnsDoesRecordExist,那你为什么不直接调用DoesRecordExist

然后您将需要一个具有通用方法的工厂来获取所需的存储库

public interface ICommonProvider {
    ICommonRepository<T> GetRepository<T>();
}

public class CommonProvider : ICommonProvider {
    private readonly ILifetimeScope lifetimeScope;

    public CommonProvider(ILifetimeScope lifetimeScope) {
        this.lifetimeScope = lifetimeScope;
    }

    public ICommonRepository<T> GetRepository<T>() {
        return lifetimeScope.Resolve<ICommonRepository<T>>();
    }
}

在启动时注册

builder.RegisterType<CommonProvider>().As<ICommonProvider>();

并将其注入服务

//...removed for brevity

private readonly ICommonProvider commonProvider;

public GlobalDataService(
    ICategoryRepository categoryRepository, 
    ISubcategoryRepository subCategoryRepository, 
    ISubcategoryDescriptionRepository subcategoryDescriptionRepository, 
    ICommonProvider commonProvider) {

    //...removed for brevity

    this.commonProvider = commonProvider;
}

public bool DoesUserRecordExist(Guid userId) {
    ICommonRepository<User> repository = commonProvider.GetRepository<User>();
    var existingData = repository.CommonDoesRecordExist(userId);
    if (existingData) {
        //do stuff
    } else {
        //do other stuff
    }
}

//...

也就是说,我建议放弃全局数据服务,并以更 SOLID 的方式遵循显式依赖原则。

简单示例

public class UserService {
    private ICommonRepository<User> repository;

    public UserService(ICommonRepository<User> repository) {
        this.repository = repository;
    }

    public bool DoesUserRecordExist(Guid userId) {
        var existingData = repository.DoesRecordExist(userId);
        if (existingData) {
            //do stuff
        } else {
            //do other stuff
        }
    }        
}

并在您的 DI 容器中注册,例如

builder.RegisterGeneric(typeof(CommonRepository<>))
    .As(typeof(ICommonRepository<>))
    .InstancePerLifetimeScope();

参考Autofac: Registration Concepts - Open Generic Components