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.
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.
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()
.
Why?
Do I have to forget about inheriting interfaces from interfaces and just type out in each interface definition the Init()
method?
The above is a simplified scenario. My "base" interface contains many more definitions than just Init().
Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.
C# allows the user to inherit one interface into another interface. When a class implements the inherited interface then it must provide the implementation of all the members that are defined within the interface inheritance chain.
Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one parent interface.
An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces. Interfaces can contain methods, properties, events, and indexers.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With