Interfaces that inherit from a base interface
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.
Related videos on Youtube
Comments
-
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 theCase
property and theViewModel
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:
Why?
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 over 12 yearscheck your inheritance chain - this should (and does) work
-
musefan over 12 yearsMaybe because
Init
is not a public method -
SWeko over 12 yearsJust 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 over 12 yearsActually, your code works for me fine
-
awrigley over 12 yearsThanks 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 over 12 yearsSadly, 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.