简体   繁体   English

使用基础接口的最佳实践

[英]The best practice in using the base interface

Our purpose of using Interface was to have the same method for each class, we realized that the method we previously determined was not sufficient for the task of each class.我们使用接口的目的是让每个 class 都有相同的方法,我们意识到我们之前确定的方法不足以完成每个 class 的任务。

I decided to solve this problem by creating an overload method我决定通过创建一个重载方法来解决这个问题

Is the usage in the example below a correct method?以下示例中的用法是否正确? What do you recommend你有什么建议吗

Base Interface基础接口

public interface IBankMapper<T1,T2,T3>
    {
        public  T1 Test(T2 t2,T3 t3);
    
    }

Interface that inherits from the base interface for X bank继承自 X bank 基本接口的接口

 public interface IXBank : IBankMapper<string, string, string>
    {
        public int Test(string t2, string t3, string t4,int t5);
    }

use of the interface界面的使用

   public class XBank : IXBank
    {
        public string Test(string t2, string t3)
        {
            throw new NotImplementedException();
        }


        public int Test(string t2, string t3, string t4, int t5)
        {

            return t2.Length + t3.Length + t4.Length + t5;
        }

    }

we used it like in this example before we needed new parameters.(Y Bank)在我们需要新参数之前,我们像这个例子一样使用它。(Y Bank)

  public interface IYBank : IBankMapper<string, string, string>
    {
     
    }




   public class YBank : IYBank
    {
        public string Test(string t2, string t3)
        {
            return (t2.Length + t3.Length).ToString();
        }
    }

As seen here, the method named Test from the base interface throw new NotImplementedException();如此处所示,基接口中名为 Test 的方法 throw new NotImplementedException(); to be left with the body is this wrong?留在身体里这是错误的吗? What's the solution?解决办法是什么?

FLOW在此处输入图像描述

is this wrong?这是错的吗?

This would be a rather clear violation of liskovs substitution principle , ie all objects implementing a interface should interchangeable.这将明显违反liskovs 替换原则,即所有实现接口的对象都应该可以互换。 Implementing an interface is a promise of providing some functionality, throwing NotImplementedException violates that promise.实现接口是提供一些功能的 promise,抛出 NotImplementedException 违反了 promise。

What's the solution?解决办法是什么?

This depend on the specific situation.这要视具体情况而定。

In some cases it is possible to provide a default value for added parameters.在某些情况下,可以为添加的参数提供默认值。 If so, you can make the parameter optional, make the old method a default interface method or an extension method to avoid changing all the call sites.如果是这样,您可以使参数可选,使旧方法成为默认接口方法或扩展方法以避免更改所有调用站点。

It might even be possible to remove the old method completely, and change all the callers, but this assumes the callers can provide meaning full values for all parameters.甚至可以完全删除旧方法并更改所有调用者,但这假设调用者可以为所有参数提供有意义的完整值。

In other cases it might be a sign that the interfaces should be separate, so that each can be used where appropriate.在其他情况下,这可能表明接口应该是独立的,这样每个接口都可以在适当的地方使用。 c# allow multiple interface implementations, so a class can still implement both interfaces. c# 允许多个接口实现,所以一个 class 仍然可以实现两个接口。

It is difficult to provide concrete recommendations since I assume the example is fictional.很难提供具体的建议,因为我假设这个例子是虚构的。

Tested this, runs with no problem, however as pointed by Jonas, it's not the best solution:对此进行了测试,运行没有问题,但是正如 Jonas 所指出的,这不是最佳解决方案:

using System;

namespace ConsoleApp2
{
class Program
{
    static void Main(string[] args)
    {
        var xB = new XBank();
        var yB = new YBank();

        Console.WriteLine(xB.Test("a", "b"));
        Console.WriteLine(yB.Test("a", "b"));
    }
}

public interface IBankMapper<T1, T2, T3>
{
    public T1 Test(T2 t2, T3 t3);

}

public interface IXBank : IBankMapper<string, string, string>
{
    public int Test(string t2, string t3, string t4, int t5);
}

public class XBank : IXBank
{
    public string Test(string t2, string t3)
    {
        return (t2.Length + t3.Length).ToString();
    }


    public int Test(string t2, string t3, string t4, int t5)
    {

        return t2.Length + t3.Length + t4.Length + t5;
    }

}

public interface IYBank : IBankMapper<string, string, string>
{

}


public class YBank : IYBank
{
    public string Test(string t2, string t3)
    {
        return (t2.Length + t3.Length).ToString();
        }
    }
}

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

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