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

时间:2019-04-12 21:25:16

标签: c# oop generics abstract-class

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

  

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

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

2 个答案:

答案 0 :(得分:1)

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

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

//...

也就是说,我建议您放弃使用全局数据服务,并采用更可靠的方法遵循显式依赖原则。

简单的例子

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

答案 1 :(得分:0)

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

但是,如果CommonDoesRecordExist仅返回DoesRecordExist,那么为什么不直接拨打DoesRecordExist