Interfaces that inherit from a base interface

29,290

In the inheritance chain, I did a number of dumb things, too many and too dumb to detail here. The valid point is that it works, what was wrong was the chain.

When I looked up on MSDN there was no simple example of this simple technique to back up my understanding, let alone any real documentation of this kind of use of inheritance with interfaces.

Share:
29,290

Related videos on Youtube

awrigley
Author by

awrigley

ASP.NET MVC and CMS

Updated on March 15, 2020

Comments

  • awrigley
    awrigley about 4 years

    Scenario:

    I am using ASP.NET MVC 3 and C#. I have a lot of services that all have an Init() method.

    So, I thought, inheritance is my new best friend. I have tried to inherit interfaces from other interfaces.

    However, I am running into problems.

    What I have done:

    As I understand it, one interface can inherit from another interface. i.e, you can do this:

    public interface ICaseService : IBaseService
    {
        CaseViewModel ViewModel { get; }
    
        Case Case { get; set; }
    }
    

    Where:

    public interface IBaseService
    {
        void Init();
    }
    

    So when I derive CaseService from ICaseService I will have to implement the Init() method as well as the Case property and the ViewModel property.

    The Problem:

    Lets say I now have a controller that has a reference to ICaseService:

    private readonly ICaseService service;
    

    In my actions, I reckon I should be able to do:

    public virtual ActionResult MyAction()
    {
        service.Init();
    }
    

    But I get an error message stating that ICaseService 'does not contain a definition for' Init().

    Questions:

    1. Why?

    2. Do I have to forget about inheriting interfaces from interfaces and just type out in each interface definition the Init() method?

    Note:

    The above is a simplified scenario. My "base" interface contains many more definitions than just Init().

    • BrokenGlass
      BrokenGlass over 12 years
      check your inheritance chain - this should (and does) work
    • musefan
      musefan over 12 years
      Maybe because Init is not a public method
    • SWeko
      SWeko over 12 years
      Just checked, and yes, I have the exact same setup, and it works for me. Maybe the IBaseService is declared in a assembly that is not referenced from the controller's assembly?
    • musefan
      musefan over 12 years
      Actually, your code works for me fine
    • awrigley
      awrigley over 12 years
      Thanks the quick response. It was in effect a stupid error in the inheritance chain. My dog is dying of heart failure under my desk, so not concentrating as usual.
  • awrigley
    awrigley over 12 years
    Sadly, I work at home, so my wife would call the men in white coats. It's one thing shouting at a computer, quite another talking to a rubber duck.