Best C # partial interface implementation in base / abstract class

.net does not allow the implementation of a partial interface in base classes. As a mitigation, I came up with 3 alternative solutions. Please help me decide which is more universal in terms of refactoring, compilation errors / runtime, readability. But first a few comments.

  • Of course, you can always use an object for IFoo and call any method without warning the compiler. But this is not logical, you would not do it normally. This construct will not result from refactoring.
  • I want maximum separation. The direct class contract (public methods and properties) must be separated by the implementation of the interface. I use interfaces to separate the interaction of objects.

My comparison:

  • BaseClass1 / MyClass1:
    • con: you need to create a virtual abstract in BaseClass1 for every non-implemented IFoo method.
    • con: An additional method transfer is a minor performance impact at runtime.
  • BaseClass2 / MyClass2:
    • con: no compiler warning if Method2 function is not implemented in MyClass2. Runtime exception. Poor refactoring unit test can potentially destabilize code.
    • con: should add an additional obsolete construct to prevent direct method invocation from child classes.
    • con: Method2 is publicly available for BaseClass1, so now it is part of the class contract. Must put an "Deprecated" construct to prevent a direct call, not via IFoo.
  • BaseClass3 / MyClass3:
    • pro: ( № 2). . , MyClass2.Method2 - IFoo, overriden.
public interface IFoo
{
    void Method1();
    void Method2();
}
public abstract class BaseClass1 : IFoo
{
    void IFoo.Method1()
    { 
        //some implementation
    }

    void IFoo.Method2()
    {
        IFooMethod2();
    }

    protected abstract void IFooMethod2();
}

public class MyClass1 : BaseClass1
{
    [Obsolete("Prohibited direct call from child classes. only inteface implementation")]
    protected override void IFooMethod2()
    {
        //some implementation
    }
}
public abstract class BaseClass2 : IFoo
{
    void IFoo.Method1()
    {
        //some implementation
    }

    [Obsolete("Prohibited direct call from child classes. only inteface implementation")]
    public virtual void Method2()
    {
        throw new NotSupportedException();
    }
}

public abstract class MyClass2 : BaseClass2
{
    public override void Method2()
    {
        //some implementation
    }
}
public abstract class BaseClass3 : IFoo
{
    void IFoo.Method1()
    {
        //some implementation
    }

    void IFoo.Method2()
    {
        throw new NotSupportedException();
    }
}

public abstract class MyClass3 : BaseClass3, IFoo
{
    void IFoo.Method2()
    {
        //some implementation
    }
}
+5
4

, , , IFoo , , . , , , ( IFoo: Method1), .

public interface IFoo
{
    void Method1();
    void Method2();
}

public abstract class BaseClass1
{
    public void Method1()
    {
        //some implementation
    }
}

public class MyClass1 : BaseClass1, IFoo
{
    public void Method2()
    {
        //some implementation
    }
}
+8

, . , , - . , , , , , , . , , .

  • (NonImplementedException NotSupportedException, . )
  • ( )

  • , ( )

  • ( )
+6

, : BaseClass :

public interface IFoo
{
    void Method1();

    void Method2();
}

public abstract class BaseClass : IFoo
{
    public void Method1()
    {
        // Common stuff for all BaseClassX classes
    }

    // Abstract method: it ensures IFoo is fully implemented
    // by all classes that inherit from BaseClass, but doesn't provide
    // any implementation right here.
    public abstract void Method2();
}

public class MyClass1 : BaseClass
{
    public override void Method2()
    {
        // Specific stuff for MyClass1
        Console.WriteLine("Class1");
    }
}

public class MyClass2 : BaseClass
{
    public override void Method2()
    {
        // Specific stuff for MyClass2
        Console.WriteLine("Class2");
    }
}

private static void Main(string[] args)
{
    IFoo test1 = new MyClass1();
    IFoo test2 = new MyClass2();

    test1.Method2();
    test2.Method2();

    Console.ReadKey();
}
+5

, , protected abstract, , , ( " IList " "; protected virtual , NotSupportedException.

, - ( ).

The correct template in VB.net will be something of a kind MustOverride Sub IFoo_Method1() Implements IFoo.Method1, which will allow you to avoid unnecessary service calls of additional functions, but C # does not provide any means for implementing an interface with a protected member. Using an explicit interface implementation for any method that can be overridden in a child class is somewhat ridiculous because it is not possible to re-implement a child interface of an interface with a parent implementation.

0
source

All Articles