I have a set of properties defined, the values are all stored as strings. I need to return a new set of properties where the value of each new property may derived in one of 3 ways.
- A direct copy of a specific source value.
- A default value.
- The result of some logic applied to the values of 1 or more source properties.
My approach to this problem was to create a IMappable interface that defines a method GetValue(Dictionary SourceValues).
For each result I defined an implementation of this interface with the required logic in this method.
I have a factory method that returns an IMappable based on the property name:
private IMappable GetMapper(string LocalPropertyName)
{
char[] Chars = LocalPropertyName.ToCharArray();
Chars[0] = Char.ToUpper(Chars[0]);
string ClassName = new string(Chars) + "Mapping";
try
{
Assembly AssemblyInstance = Assembly.GetExecutingAssembly();
Type ClassType = AssemblyInstance.GetType("MyNamespace.Rules." + ClassName, false, true);
return (IMappable) System.Activator.CreateInstance(ClassType);
}
catch (System.Exception e)
{
//No Mapper exists
return new DefaultMapping(LocalPropertyName);
}
}
Is this the best way to approach this problem?
It feels like the most elegenat but I'm worried about the performance hit of dynamic class loading.