c# - 接口(interface)版本控制问题

标签 c# .net interface

我暴露给用户的旧版界面是这样的:

public interface IReporter
{
    void  Write(int site, DateTime start, DateTime end);
} 

现在我想替换函数中的参数如下:

public interface IReporter
{
    void  Write(int site, SiteLocalDateTime start, SiteLocalDateTime end);
}

我希望现有客户使用旧方法,新客户使用新方法。

关于如何通过公开单一接口(interface)来实现这一点有什么想法吗?

选项:

  1. 保持两个接口(interface) IReporterIReporterNew : IReporter 现在,我所有的新实现都需要使用这两种方法。

  2. 暴露两个接口(interface)是不可能的。

最佳答案

如果方法的实现在您这一端,您可以将接口(interface)合并为一个。然后用户可以决定使用哪个。 (最好将旧方法标记为过时,谢谢 3dd )

public interface IReporter
{
    [Obsolete]
    void  Write(int site, DateTime start, DateTime end);
    void  Write(int site, SiteLocalDateTime start, SiteLocalDateTime end);
}

如果这不是您所追求的,您实际上应该对您的软件进行版本控制。一个版本供新客户使用,一个“旧版”版本供现有客户使用。最终您可以将老客户迁移到新版本。

您甚至可以创建 conversion between the two types如果知道实际的实现类型,但我不确定这在这种情况下是否有用。

此外,您的意思是使用 DateTimeOffset而不是 SiteLocalDateTime

关于c# - 接口(interface)版本控制问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673557/

相关文章:

c# - "Bad Version of provider."使用 RSACryptoServiceProvider 加载公钥时

c# - Mono.Security加载问题

c# - 从 DAL 返回数据对象

mysql - 连接 MySQL 和 C

java - 通过 Java 接口(interface)传递设置 - 可能吗?

c# - 从 iframe 中读取查询字符串参数

c# - 如何在 stub 上没有 setter 的情况下设置属性的值?

c# - Unity 的垃圾收集器——为什么是非分代和非压缩的?

c# - 通过反射内部属性获取有关当前属性的信息

performance - Delphi界面性能问题