避免在以下遗留代码中进行大量代码更改的优雅方法

发布于 2024-12-19 05:46:44 字数 506 浏览 1 评论 0原文

在某些遗留代码中,以下代码片段出现数百次:

myObj.ReportGenerator.Preview = reportingObj.PreviewDocument;

...而“ReportGenerator”和“ReportingObj”都是第三方库的实例,因此不可修改。

这段代码在 Windows XP 下运行良好,但在 Windows 7 中运行该程序确实需要以下额外的代码行:

reportingObj.Render();
myObj.ReportGenerator.Preview = reportingObj.PreviewDocument;

不幸的是,这段代码在所有代码库中出现了数百次,手动搜索它们听起来就像一个非常容易出错的过程。

由于“ReportGenerator”和“reportingObj”是第三方,我无法更改它们的 getter / setter。

解决此类问题的优雅方法是什么?

In some legacy code, there are hundreds of occurrences of the following code snippets:

myObj.ReportGenerator.Preview = reportingObj.PreviewDocument;

... whereas both the "ReportGenerator" and the "ReportingObj" are instances of a third party library and therefore not modifyable.

This code did work well under Windows XP, but running the program in Windows 7 does require the following additional line of code:

reportingObj.Render();
myObj.ReportGenerator.Preview = reportingObj.PreviewDocument;

Unfortunately, there are hundreds of occurences of this piece of code all of the code base, and manually searching for them sounds like quite error-prone a process.

As "ReportGenerator" and "reportingObj" are third party, I cannot change their getter / setter.

What are elegant ways of approaching such an issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几度春秋 2024-12-26 05:46:44

您可以将 ReportingObj 包装在您自己的类中,在该类中您只需委托给原始 ReportingObj,但对于 PreviewDocument 属性检查是否 Render()< /code> 被调用,如果没有调用它 - 像这样:

public Foo PreviewDocument
{
    get
    {
        if (!_rendered)
        {
            _originalreportingObj.Render();
            _rendered = true;
        }
        return _originalreportingObj.PreviewDocument;
    }
}

You could wrap ReportingObj in a class of your own in which you just delegate to the original ReportingObj, but for the PreviewDocument property check to see if Render() was called and if not call it - something like this:

public Foo PreviewDocument
{
    get
    {
        if (!_rendered)
        {
            _originalreportingObj.Render();
            _rendered = true;
        }
        return _originalreportingObj.PreviewDocument;
    }
}
岁月打碎记忆 2024-12-26 05:46:44

您可以更改 myObj 的类(我认为该类在您的控制之下),并让 ReportGenerator 属性返回一个包装类,该包装类可以调用 的原始设置器预览或在调用Render()后调用它:

public class ReportGeneratorWrapper
{
     private ReportGenerator m_InnerReportGenerator;

     public PreviewDocument Preview
     {
         get
         {
             return m_InnerReportGenerator;
         }
         set
         {
             if (IsNT6OrAbove)
                 value.Render();

             m_InnerReportGenerator = value;
         }
     }
}

You could change the class of myObj, which I assume is under your control, and have the ReportGenerator property return a wrapper class that either calls the original setter of the Preview or calls it after calling Render():

public class ReportGeneratorWrapper
{
     private ReportGenerator m_InnerReportGenerator;

     public PreviewDocument Preview
     {
         get
         {
             return m_InnerReportGenerator;
         }
         set
         {
             if (IsNT6OrAbove)
                 value.Render();

             m_InnerReportGenerator = value;
         }
     }
}
等待圉鍢 2024-12-26 05:46:44

您可能会发现最少的返工量是创建一个静态类,如下所示:

public class Previewer
{
     public static PreviewDocumentType PreviewDocument(ReportingObject reportingObj) {
       reportingObj.Render();
       return reportingObj.PreviewDocument;
     }
}

其中 PreviewDocumentType 是从 PreviewDocument 返回的类型,ReportingObject 是报告对象的类型。

然后您可以替换

reportingObj.PreviewDocument;

Previewer.PreviewDocument(reportingObj);

You might find that the least amount of rework will be to create a static class, something like:

public class Previewer
{
     public static PreviewDocumentType PreviewDocument(ReportingObject reportingObj) {
       reportingObj.Render();
       return reportingObj.PreviewDocument;
     }
}

where PreviewDocumentType is the type returned from PreviewDocument and ReportingObject is the type of reporting object.

You can then replace

reportingObj.PreviewDocument;

with

Previewer.PreviewDocument(reportingObj);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文