界面版本控制中的问题

use*_*317 2 .net c# interface

我向用户公开的旧版接口是这样的:

public interface IReporter
{
    void  Write(int site, DateTime start, DateTime end);
} 
Run Code Online (Sandbox Code Playgroud)

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

public interface IReporter
{
    void  Write(int site, SiteLocalDateTime start, SiteLocalDateTime end);
}
Run Code Online (Sandbox Code Playgroud)

我希望现有客户使用旧方法和新客户来使用新方法.

有关如何通过暴露单一界面实现这一点的任何想法?

选项:

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

  2. 不可能暴露两个界面.

Pat*_*man 9

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

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

如果这不是您所追求的,您实际上应该对您的软件进行版本控制.一个适用于新客户的版本,一个针对现有客户的"遗留"版本.最终,您可以将旧客户迁移到新版本.

如果已知实际实现类型,您甚至可以在两种类型之间创建转换,但我不确定在这种情况下这是否有用.

另外,你的意思是用DateTimeOffset而不是SiteLocalDateTime

  • 旧方法也可以标记为"[Obsolete]" (4认同)