简体   繁体   中英

Interfaces that inherit from a base interface

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. ie, 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().

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM