views:

21

answers:

1

I have to support a new input file format in a system which uses Windsor. I also need to support the old version of the input file during a transition phase. This will probably be repeated in future, and we'll again need to support the new and the next most recent format.

The import processing is handled by a component, and the new version has had significant improvements in the code which makes it lots more efficient compared to the old version. So what I'd like to do is to have the new component and the old component in the system, and dynamically use the new or the old component based upon the file metadata.

Is there a pattern for this type of scenario anyone can suggest?

+2  A: 

The fact that you're using Windsor is pretty much irrelevant here. Always strive to find a container-independent solution. Here's one:

interface IImportProcessor {
    bool CanHandleVersion(int version);
    Stream Import(Stream input);
}

class ImportProcessorVersion1 : IImportProcessor {
    public bool CanHandleVersion(int version) {
        return version == 1;
    }

    public Stream Import(Stream input) {
        // do stuff
        return input;
    }
}

class ImportProcessorVersion2 : IImportProcessor {
    public bool CanHandleVersion(int version) {
        return version == 2;
    }

    public Stream Import(Stream input) {
        // do stuff
        return input;
    }
}

class MainImportProcessor: IImportProcessor {
    private readonly IImportProcessor[] versionSpecificProcessors;

    public MainImportProcessor(IImportProcessor[] versionSpecificProcessors) {
        this.versionSpecificProcessors = versionSpecificProcessors;
    }

    public bool CanHandleVersion(int version) {
        return versionSpecificProcessors.Any(p => p.CanHandleVersion(version));
    }

    private int FetchVersion(Stream input) {
        // do stuff
        return 1;
    }

    public Stream Import(Stream input) {
        int version = FetchVersion(input);
        var processor = versionSpecificProcessors.FirstOrDefault(p => p.CanHandleVersion(version));
        if (processor == null)
            throw new Exception("Unsupported version " + version);
        return processor.Import(input);
    }
}

Your app would take a dependency on IImportProcessor. The container is wired so that the default implementation of this interface is MainImportProcessor. The container is also wired so that MainImportProcessor gets all other implementations of IImportProcessor. This way you can add implementations of IImportProcessor and each will be selected when appropriate.

It might be easier to wire things up if MainImportProcessor implements an interface different from IImportProcessor.

Another possibility could be implementing a chain of responsibility.

Mauricio Scheffer
Thanks Mauricio, this sounds like a great approach. Is it possible to wire up Windsor that way using the XML config?
nocache
Yes, it's possible. You'll have to wire the versionSpecificProcessors manually though. See *Passing a list of notifiers to HttpServiceWatcher* : http://www.castleproject.org/container/gettingstarted/part1/config.html
Mauricio Scheffer
Fantastic! Thank you so much. This is the value of open source: the support is so much better.
nocache