简体   繁体   English

从抽象类和接口继承的问题是否存在,其中抽象从基本接口继承

[英]Are there any issue inheriting from an abstract class and interface where the abstract inherits from base interface

Are there any issue inheriting from an abstract class and interface where the abstract inherits from base interface. 从抽象类和接口继承的问题是否存在,其中抽象从基本接口继承。 The example below is a much cut down just to show the concept. 下面的示例仅是为了说明概念而进行了精简。 I doing this so that I create a fake class based on IFooRepository and also the FooRepository can reuse all the code in my abstract class (which will be shared by many other classes): 我这样做是为了创建一个基于IFooRepository的伪类,并且FooRepository可以重用我的抽象类中的所有代码(这些代码将由许多其他类共享):

public interface IMyRepository<T> where T : class
{
    List<T> GetEntity();
}

public abstract class MyRepository<T> : IMyRepository<T> where T : class
{
    protected readonly string _connectionString;

    public virtual T CommonFunction(int Id)
    {
        //do my common code here
    }

    public List<T> GetEntity()
    {
    }

}

public interface IFooRepository : IMyRepository<Foo>
{
    void UpdateFoo(int id, string foo);
}


public class FooRepository : MyRepository<Foo>, IFooRepository
{
    public void UpdateFoo(int id, string foo)
    {
        throw new NotImplementedException();
    }
}

public class FakeFooRepository :  IFooRepository
{
    public List<Foo> GetEntity()
    {
        throw new NotImplementedException();
    }

    public void UpdateFoo(int id, string foo)
    {
        throw new NotImplementedException();
    }
}

public interface IBarRepository : IMyRepository<Bar>
{
    void DoSomethingElse(int id);
}

public class BarRepository : MyRepository<Bar>, IBarRepository
{
    public void DoSomethingElse(int id)
    {

    }
}

Also is it better if IFooRepository, doesn't inherit from IMyRepository and instead contains all members like this: 如果IFooRepository不继承自IMyRepository,而是包含所有像这样的成员,那就更好了:

public interface IFooRepository
{
    void UpdateFoo(int id, string foo);
    List<Foo> GetEntity();
}

Either way the whole thing compiles and works as I expected, just wanted to know if there would be any issues as the interfaces are overlapping. 无论哪种方式,整个过程都可以按我预期的方式编译和运行,只是想知道由于接口重叠而引起的问题。

Thanks 谢谢

您无需在具体类中实现接口,如果您的基类实现了某些接口,则其所有子级将自动成为该接口的实现者

Your first example looks very sound. 您的第一个示例看起来非常合理。 IFooRepository inherits from IMyRepository because that interface is inherently an IMyRepository of type Foo. IFooRepository继承自IMyRepository,因为该接口本质上是类型为Foo的IMyRepository。 For that same reason, your second sample where IFooRepository does not inherit from IMyRepository definitely seems to break the relationship you are trying to establish. 出于同样的原因,您的第二个样本(其中IFooRepository不继承自IMyRepository)肯定似乎破坏了您尝试建立的关系。 That does not sound like a good plan. 听起来这不是一个好计划。

The other way you might normally consider doing this is to make both Foo and Bar (and T) of a common type (IRepositable or something), and the behavior specific to a Foo or Bar repository is represented in their individual implementation of IRepositable. 通常,您可能会考虑这样做的另一种方法是使Foo和Bar(和T)都具有相同的类型(IRepositable或其他类型),并且特定于Foo或Bar的行为在其IRepositable的单独实现中表示。 If this could be done in a way that maintains proper encapsulation of Foo and Bar, you would end up with a very elegant solution. 如果可以通过保持Foo和Bar正确封装的方式来完成此操作,那么您将得到一个非常优雅的解决方案。

That would work if Foo needed to do UpdateFoo and Bar, similarly, needed to do UpdateBar using the same method signature, and so on for any other IRepositable. 如果Foo需要执行UpdateFoo和Bar,类似地,需要使用相同的方法签名来执行UpdateBar,以此类推,对于其他任何IRepositable来说都是如此。 But in your case, the method name DoSomethingElse seems to indicate that IBarRepository is implementing some completely different behavior from the UpdateFoo method. 但是在您的情况下,方法名称DoSomethingElse似乎表明IBarRepository正在实现与UpdateFoo方法完全不同的行为。 If that's true, then you are correct. 如果是这样,那么您是正确的。 The interfaces which inherit from a specific type of of IMyRepository are probably your best bet. 从特定类型的IMyRepository继承的接口可能是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从基抽象类和 IDisposable 的正确位置继承接口 - Inheriting an interface from a base abstract class and IDisposable's correct place 在 AutoMapper 中,如何从抽象基础 class 项目到接口 - In AutoMapper how does one project from an abstract base class to an interface 从实现接口的抽象类继承的类,并进行反射性加载 - Class which inherits from abstract class which implements interface, and loading this reflectively 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 在抽象基类上声明接口 - Declaring interface on abstract base class 从一个类和Interface或Abstract类继承时,隐藏其他类的方法 - Hiding methods from other classes when inheriting from a class and Interface or Abstract class 从通用接口继承的通用基类 - Generic Base class inheriting from Generic Interface 通用接口/抽象类的向下转换失败 - Downcasting failing from common interface/abstract class 从抽象引用创建接口+类包装器 - Create interface + Class wrapper from abstract references 在运行时创建类型,继承抽象类并实现接口 - Create type at runtime that inherits an abstract class and implements an interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM