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

How can I find all the MS Access security groups in VBA

ms-access ms-access-2003

How can I find a list of all the MS Access security groups in my application through VBA?

Reload com.apple.Terminal.plist

osx terminal reload defaults

I want use a script to modify the preferences of my terminal for my Mac, so it can close the window when exit the terminal. I use this command in the script:

Where did CCSpriteSheet go in Cocos2D version 1.0.1

cocos2d-iphone

Did they remove CCSpriteSheet in version 1.0.1 or did am I just missing it? if they removed it, is there something else that replaced it?