简体   繁体   中英

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):

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:

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. 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. 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. 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.

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. But in your case, the method name DoSomethingElse seems to indicate that IBarRepository is implementing some completely different behavior from the UpdateFoo method. If that's true, then you are correct. The interfaces which inherit from a specific type of of IMyRepository are probably your best bet.

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