Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can a C# class inherit from a base class and an interface and have both classes' interfaces resolved by IOC?

This is all quickly typed pseudo code to illustrate the problem. I have a base class that implements an interface. I then inherit that class in two child classes which implement their own interfaces. Finally, I inject one of the child classes into a controller.

My hope was that I could call the Delete and GetSearches methods of the base class as well as the GetCustomerSearchResults method of the child class. But that doesn’t compile. So, apparently the base class interface can’t be found even though the child class inherits from the class implementing that interface.

How else could I do this to keep the more generic find, save, delete methods in a base class while having more specific entity type methods in the child class and have both interface methods available in the controller? Is there a certain design pattern that works well for this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public interface ISearchService{
    Task<List<Search>> GetSearches(int userId);
    Task DeleteSearch(int id);
}

public class SearchService : ISearchService {
    private IRepository _repository;

    public SearchService(IRepository repository){
        _repository = repository;
    }

    public async Task<List<Search>> GetSearches(int userId){
        return await _repository.GetUserSearches(userId);
    }

    public async Task DeleteSearch(int id){
        await _repository.DeleteSearch(id);
    }
}


public interface ISalesSearchService{
    Task<SearchResultReport> GetSalesSearchResults(Search search);
}

public class SalesSearchService : SearchService, ISalesSearchService{

    public ISalesSearchRepository _salesRepository;
    public SalesSearchService(IRepository repository, ISalesSearchRepository salesRepository) : base(repository){   
        _salesRepository = salesRepository;
    }

    public async Task<SearchResultReport> GetSalesSearchResults(Search search){
        return _salesRepository.GetSalesSearchResults(search);
    }
}


public interface ICustomerSearchService{
    Task<SearchResultReport> GetCustomerSearchResults(Search search);
}

public class CustomerSearchService : SearchService, ISalesSearchService{

    public ICustomerSearchRepository _customerRepository;
    public SalesSearchService(IRepository repository, ICustomerSearchRepository customerRepository) : base(repository){ 
        _customerRepository = customerRepository;
    }

    public async Task<SearchResultReport> GetCustomerSearchResults(Search search){
        return _customerRepository.GetCustomerSearchResults(search);
    }
}

public class CustomerSearchController : Controller {

    private ICustomerSearchService _customerSearchService;

    public CustomerSearchController(ICustomerSearchService customerSearchService){
        _customerSearchService = customerSearchService;
    }

    public async Task Delete(int id){
        await _customerSearchService.Delete(1);
    }

    public async Task<IActionResult> GetSearchResults(int id){
        var searches = await _customerSearchService.GetSearches(_userId);
        var search = searches.FirstOrDefault(x => x.Id == id);

        var results = await _customerSearchService.GetCustomerSearchResults(search);
        return View(results);
    }
}

>Solution :

Use inheritance with the interfaces.

 ICustomerSearchService : ISalesSearchService

and

 ISalesSearchService : ISearchService
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading