Elegant ways of avoiding numerous code changes in the following legacy code

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?

Answers


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;
    }
}

Need Your Help

Crontab and sh script

bash node.js cron crontab sh

I have an sh script I want called every three hours via cron. The script calls a bunch of node.js scripts and works perfectly when called directly. However, when called via cron, I get a date logge...

Correct way to quit a Qt program?

qt resources exit quit

How should I quit a Qt Program, e.g when loading a data file, and discovered file corruption, and user need to quit this app or re-initiate data file?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.