繁体   English   中英

如何安全,干净地重写关键/生产代码? [关闭]

[英]How to safely and cleanly rewrite critical/production code? [closed]

我有一些关键的生产程序需要从头开始重写。 举一个简单的例子:

public class ProductionClass {
    public IList<Values> WillBeFiredIfThisBreaks(Input input) {
       ...
    }
}

输入对象有太多的排列方式来彻底进行单元测试,我想要安全地使用它,因为这些例程每天都在大量使用。 所以,我的想法是:

1)将当前实现重命名并标记为过时。

2)如果有任何问题,写一个新的实现,然后再回到旧的实现(见下文)

3)在新的实现在prod中运行一个月或两个没有问题之后删除旧的实现。

public class ProductionClass {

    public IList<Values> WillBeFiredIfThisBreaks(Input input) {
       try{
          var ret = NewImpl(input);
       } catch(Exception){
          ret = null;
       }

       if(ret == null || ret.Count == 0){
          Log.Error("NewImpl Failed! Inputs: {0}", inputs);
          return OldImpl(input);
       }
       return ret;
    }


    public IList<Values> NewImpl(Input input) {
       ...
    }

    [Obsolete("Rewritten 03/18/2013, throw away once NewImpl confirmed stable", false)]
    public IList<Values> OldImpl(Input input) {
       ...
    }
}

上面的方法有点难看,因为我需要为每个需要重写的方法重新创建这个逻辑。 (而且我相应地需要删除回退逻辑并在新代码确认稳定时删除每个位置的过时方法)。 所以我的问题是:是否有任何.NET框架或设计模式使这种“超偏执”代码重写更优雅?

由于您要求使用模式,因此可能的响应是使用AOP进行方法编织。 但更好的回应是集中精力创建单元测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM