Situation:
I’m looking to create a framework in which I will crunch data. The output of the data crunch will be a string (which I need for the bulk of my program). I will be consuming Data Crunchers made by other people.
I have an interface that looks like this:
public interface IDataCruncher {
string Execute( ICruncherInput input );
}
The Problem:
Since each DataCruncher will perform differently and requires different inputs, I have resorted to passing the information in as dictionary<string, object>, however the packing/unpacking process is tedious to implement and is not compile safe for packing up the material (you could misspell a dictionary key, or forget one, causing the data cruncher to crash).
public interface ICruncherInput {
Dictionary<string, object> Data { get; }
}
The Question:
Is it possible to pass data to the Data Cruncher implementation with compile time safety, or at least stronger safety than casting from objects?
Is there a way to define these key contracts in the code? (As opposed to assuming the dictionary will contain the keys I need, and defining the keys in a document outside of the code)
I common way to pass data to a class, is to pass it to the constructor. Since the constructor is not part of the interface, different classes implementing the same interface can have different constructor signatures.
But of cause it works only, if you are not instantiating the class through the default constructor.
And of cause it is fully type-safe and will not compile if the wrong parameters are passed.