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.