Acla
Acla

Reputation: 141

c# interface inheritance - not recognizing base interface methods

I'm creating an ASP layered web application and I have the following structure:

IService<T>

public interface IService<T> {

    IEnumerable<T> GetAll();
    void Add(T entity);
    void Delete(T entity);

Service<T>

    public class Service<T> where T : class {

        private IRepository<T> _repository;

        public Service(IRepository<T> repo) {
            this._repository = repo;
        }

        public IEnumerable<T> GetAll() {
            return _repository.GetAll();
        }

etc

And then I also have some 'custom services':

ICategoryService

  public interface ICategoryService : IService<Category> {

       IEnumerable<Category> GetProductCategories(int productId);

    }

CategoryService

public class CategoryService : Service<Category>, ICategoryService {

        private IRepository<Category> _repository;

        public CategoryService(IRepository<Category> repo) : base(repo) {
            this._repository = repo;
        }

         public IEnumerable<Category> GetProductCategories(int productId) {
            // implementation
        }

Controller:

 public class ProductController : Controller {
        private ICategoryService _cservice;

        public ProductController(ICategoryService cservice) {
            this._cservice = cservice;
        }
        ´ 
      // other methods

      public ActionResult Categories() {
                IEnumerable<Category> categories = _cservice.GetAll(); // doesn't work
        }

I'm trying to access the .GetAll() method in my controller, which was defined in IService and implemented in Service, but I'm getting a 'ICategoryService contains no definition for GetAll' error and I have no idea why.

I can access the .GetAll() method from my CategoryService, so I don't know why I can't access it from a CategoryService instance (via dependency injection).

Upvotes: 0

Views: 946

Answers (1)

Itay Podhajcer
Itay Podhajcer

Reputation: 2654

You might have made a typo when posting your code, but, it seems that Service<T> doesn't implement IService<T> change the class's implementation to:

public class Service<T> : IService<T>
    where T : class 
{

    private IRepository<T> _repository;

    public Service(IRepository<T> repo) {
        this._repository = repo;
    }

    public IEnumerable<T> GetAll() {
        return _repository.GetAll();
    }

I would also move the where T : class constraint from the class to interface.

Upvotes: 5

Related Questions