你认为"自动接口实现"将在.NET / C#有用 [英] Do you think "auto interface implementation" would be useful in .NET / C#

查看:211
本文介绍了你认为"自动接口实现"将在.NET / C#有用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下:

public class interface Person : IPerson
{
  int ID { get; protected set; }
  string FirstName { get; set; }
  string LastName { get; set; }
  string FullName { get { return FirstName + " " + LastName; } }
}

和这样的:

public class StubPerson : IPerson
{
    int ID { get { return 0; protected set { } }
    string FirstName { get { return "Test" } set { } }
    string LastName { get { return "User" } set { } }
    string FullName { get { return FirstName + " " + LastName; } }
}

用法:

IPerson iperson = new Person();

或者

IPerson ipersonStub = new StubPerson();

或者

IPerson ipersonMock = mocks.CreateMock<IPerson>();

所以,实际上,我们在声明 IPerson 接口和类在同一时间:

So in effect we are declaring the IPerson interface and the Person class at the same time:

public class interface Person : IPerson

你觉得有这样的支持在.NET / C#将是有益的?

编辑:

由于大规模的混乱,我想我需要澄清所提出的目的:

Due to mass confusion I think I need to clarify the proposed purpose:

如果没有这个功能,你会写:

Without this feature you would have to write:

interface IPerson
{
  int ID { get; }
  string FirstName { get; set; }
  string LastName { get; set; }
  string FullName { get; }
}

还有这样的:

as well as this:

public class Person : IPerson
{
  int ID { get; protected set; }
  string FirstName { get; set; }
  string LastName { get; set; }
  string FullName { get { return FirstName + " " + LastName; } }
}

我不建议任何语义变化都没有。

推荐答案

我认为的同样的事情在不久前,特别是用在情况下,你只能有一个生产实现一个接口,但你要模拟它出用于测试。目前,它最终被昔日有点像.C / .h文件。

I considered the same sort of thing a while ago, particularly for use in the case where you only have one production implementation of an interface, but you want to mock it out for testing. At the moment it ends up being a bit like the .c/.h files of yore.

我怀疑在它的好处双方在语言,然后读取code事后额外的复杂性都胜过结束。我仍然有兴趣看到它探索更彻底虽然。即使这样,还有其他对我的优先级列表一路走高的东西 - 对于不变性是在顶部更好的支持:)

I suspect in the end that the benefits of it are outweighed by the extra complexity both in the language and then reading the code afterwards. I'd still be interested in seeing it explored more thoroughly though. Even then, there are other things way higher on my priority list - better support for immutability being at the top :)

这篇关于你认为&QUOT;自动接口实现&QUOT;将在.NET / C#有用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆