c# - 基于类的隐式接口(interface)

标签 c#

我的业务层中有很多类都有一个接口(interface),该接口(interface)必须始终具有与我的业务类的公共(public)方法和属性相同的方法和属性。这是一个常见的场景,因为在单元测试时依赖注入(inject)和模拟需要接口(interface)。

如果我能以某种方式将接口(interface)定义为与类的公共(public)方法和属性相同,那将是最佳选择。这样我就不必一直将我实现的类中的粘贴方法定义复制到我的接口(interface)中。我看不出这有任何逻辑问题,但我知道在 C# 中不可能直接这样做。

也许有人可以想出一个合理的方法来完成这个?

这是一个例子。这是一个界面。

public interface IAccountBusiness
{
    Guid GetAccountIdByDomain(string domain);

    void CreateAccount(string accountType, string accountName);
}

实现如下:

public class AccountBusiness : IAccountBusiness
{
    public Guid GetAccountIdByDomain(string domain)
    {
        // Implementation
    }

    public void CreateAccount(string accountType, string accountName)
    { 
        // Implementation
    }
}

如果我想在CreateAccount中多加一个参数,比如“Email”,那么我就得在界面和业务类中都加。在这个例子中,这是一个小问题,但在更大规模的项目中,它是……好吧……仍然是一个小问题,但它不一定是。

最佳答案

Resharper's Change Signature 重构使您可以轻松地做到这一点:

enter image description here

关于c# - 基于类的隐式接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23979447/

相关文章:

c# - 处理后是否有可能获得 Timer.Tick 事件

c# - XSD 到数据集(这也是一个 xsd?)

c# - 将以西里尔文命名的文件上传到 blob 容器

c# - Brushes.Red 和 new SolidBrush(Color.Red) 之间有什么区别?

c# - DataTemplateSelector 中的项目为空

c# - 在带有 .net 包装器版本或 quickfix/n 版本的 quickfix 之间进行选择?

c# - MVC BundleConfig Css/脚本未加载

c# - 实例化多个 GameObject 预制件并删除原始文件后,Unity 中缺少引用异常

c# - 在 C# 中格式化 MAC 地址

c# - 如果我的 gRPC 项目中缺少 Google.Api.AnnotationsReflection,我缺少什么库?