tshoemake
tshoemake

Reputation: 1351

Issue calling a generic common repository's method

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

Upvotes: 0

Views: 175

Answers (2)

Nkosi
Nkosi

Reputation: 247123

You will then need a factory that has a generic way to get the desired repository

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

that is registered at startup

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

and inject that into the service

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

//...

That said, I would suggest moving away from a global data service and follow explicit dependency principle in a more SOLID approach.

Simple example

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

and registered with your DI container something like

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

Reference Autofac: Registration Concepts - Open Generic Components

Upvotes: 1

stinepike
stinepike

Reputation: 54682

You cannot directly call the method of a child class using parent class reference. You need to cast _commonRepository to CommonRepository.

However, if CommonDoesRecordExist only returns DoesRecordExist, then why don't you just call DoesRecordExist directly.

Upvotes: 0

Related Questions