Search code examples
c#.netautofacgeneric-programmingabstraction

Implement different concretions to an abstract class that implements a generic service


I have a generic interface and one generic class with generic methods to query the database.

    public interface IRepositoryBase<Entity> {
        IEnumerable<TEntity> GetAll(Func<IQueryable<TEntity>, IncludableQueryable<TEntity, object>> include = null);
    }

    public class RepositoryBase<TEntity> 
        : IDisposable, IRepositoryBase<TEntity>
        where TEntity : class
    {
        public IEnumerable<TEntity> GetAll(Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null)
        {
            IQueryable<TEntity> query = _context.Set<TEntity>();

            if (include != null)
                query = include(query);

            return query.ToList();
        }
    }

I also have several classes that I call "services" that have the business logic and implement another generic interface.

    public interface IServiceBase<TEntity>
        where TEntity : class
    {
        IEnumerable<TEntity> GetAll(Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null);
    }

    public class ServiceBase<TEntity>
        : IDisposable, IServiceBase<TEntity> 
        where TEntity : class
    {
        private readonly IRepositoryBase<TEntity>
            _repository;

        public ServiceBase(
            IRepositoryBase<TEntity> repository)
        {
            _repository = repository;
        }

        public ServiceBase()
        {
        }

        public IEnumerable<TEntity> GetAll(Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null)
        {
            return _repository.GetAll(include);
        }
    }

    public class PizzaService : ServiceBase<Piza>, IPizzaService
    {
        private readonly IPizzaRepository _pizzaRepository;
        public PizzaService (IPizzaRepository pizzaRepository)
            : base(pizzaRepository)
        {
            _pizzaRepository= pizzaRepository;
        }
    }

This way each service have their methods acessing their own table plus the methods in the ServiceBase.

There is a scenario where I have 3 concrete services like PizzaService, each one querying its own table, with really similar code, because of the table and the logic similarity.

I want to refactor those concrete services into one, changing only the method param and the repository being acessed, to be in compliance to DRY and ISP.

What I currently have:

    public interface IStopRule
    {
        string DsTerm { get; set; }
        bool ShouldDelete { get; set; }
    }

    public interface IExampleRuleStopWordsBase<TEntity> : IServiceBase<TEntity>
        where TEntity : class
    {

    }

    public abstract class ExampleRuleStopWordsBase
        : ServiceBase<IStopRule>, IExampleRuleStopWordsBase<IStopRule>
    {
        private readonly IRepositoryBase<IStopRule> _repo;

        public ExampleRuleStopWordsBase(IRepositoryBase<IStopRule> repo) 
            : base()
        {
            _repo = repo;
        }

        public virtual string ApplyRule(string input)
        {
            var terms = GetAll();
            foreach (var item in terms)
            {
                string regexPattern = @"\b(" + item.DsTerm + @")\b";
                if (item.ShouldDelete && Regex.Match(input, regexPattern, RegexOptions.IgnoreCase).Success)
                    input = input.Replace(item.DsTerm, string.Empty);
            }

            input = input.Trim();

            return input;
        }
    }

    public class PizzaService : ExampleRuleStopWordsBase, IImportRule 
    {

        public PizzaService(IRepositoryBase<IStopRule> repo)
            : base(repo)
        {
        }

        public void ApplyRule(Pizza pizza)
        {
            base.ApplyRule(pizza.Name);
        }
    }

    public class PizzaProducerService : ExampleRuleStopWordsBase, IImportRule 
    {

        public PizzaProducerService(IRepositoryBase<IStopRule> repo)
            : base(repo)
        {
        }

        public void ApplyRule(Pizza pizza)
        {
            base.ApplyRule(pizza.Producer.Name);
        }
    }

But I can't figure it out how to pass to the consturctor of ImportRuleStopWordsBase the right entity to make it use the right repository...

Obs: All interfaces and service implementations reside in Domain layers, whereas the implementation of the repository resides in Infrastructure layer.


Solution

  • It looks like you're looking for .RegisterGeneric here if I understand correctly. The example for your classes might be:

    var containerBuilder = new ContainerBuilder();
    containerBuilder.RegisterType<PizzaService>();
    containerBuilder.RegisterType<PizzaProducerService>();
    
    containerBuilder.RegisterGeneric(typeof(RepositoryBase<>))
        .As(typeof(IRepositoryBase<>));
    
    var container = containerBuilder.Build();
    using (var scope = container.BeginLifetimeScope())
    {
        var pizzaService = scope.Resolve<PizzaService>();
        var pizzaProducerService = scope.Resolve<PizzaProducerService>();
    }