views:

30

answers:

1

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.

  1. A direct copy of a specific source value.
  2. A default value.
  3. 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.

A: 

Is it slow? Profile it under test and then worry about how slow or not it is.,

Preet Sangha