[英]Implicit interface based on class
I have a lot of classes in my business layer that have an interface which must always have the same methods and properties as the public methods and properties of my business class.我的业务层中有很多类都有一个接口,这些接口必须始终具有与业务类的公共方法和属性相同的方法和属性。 This is a common scenario, as the interface is needed for dependency injection and mocking while unit testing.
这是一个常见的场景,因为在单元测试时需要接口进行依赖注入和模拟。
It would be optimal if I could somehow define the interface as the same as the public methods and properties of a class.如果我能以某种方式将接口定义为与类的公共方法和属性相同,那将是最佳选择。 That way I don't have to copy paste method definitions from my implemented class to my interface all the time.
这样我就不必一直将粘贴方法定义从我实现的类复制到我的界面。 I don't see any logical problem with this, but I know that it's not directly possible to do in C#.
我没有看到任何逻辑问题,但我知道在 C# 中不能直接做到这一点。
Perhaps someone can come up with a reasonable way to accomplish this?也许有人可以想出一个合理的方法来实现这一目标?
Here is an example.这是一个例子。 Here is an interface.
这是一个界面。
public interface IAccountBusiness
{
Guid GetAccountIdByDomain(string domain);
void CreateAccount(string accountType, string accountName);
}
Here is the implementation:这是实现:
public class AccountBusiness : IAccountBusiness
{
public Guid GetAccountIdByDomain(string domain)
{
// Implementation
}
public void CreateAccount(string accountType, string accountName)
{
// Implementation
}
}
If I want to add a parameter more in CreateAccount, for example "Email", then I have to add it to both the interface and the business class.如果我想在 CreateAccount 中添加更多参数,例如“电子邮件”,那么我必须将它添加到界面和业务类中。 In this example it's a minor nuisance but in larger scale projects it's ... well ... still a minor nuisance, but it doesn't have to be.
在这个例子中,这是一个小麻烦,但在更大规模的项目中,它......好吧......仍然是一个小麻烦,但它不一定是。
Resharper's
Change Signature
refactoring allows you to do that easily: Resharper's
Change Signature
重构使您可以轻松做到这一点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.