title: strategy pattern with dictionary lookup
body: <p>I'm writing a validation engine.</p>
  
  <p>I'm given with an a object payload containing around list of ~40 different properties. Every property will undergo different validation.</p>
  
  <p>The validations include checking if the field is a string and validating if the length exceeds permissible limit set by db and there are conditions to check null values and empty fields as well.</p>
  
  <p>So, I thought of picking the strategy pattern.</p>
  
  <p>Code: </p>
  
  <pre><code> interface IValidationEngine
  {
      Error Validate(string propName, dynamic propValue);
  }
  
  public class StringLengthValidator : IValidationEngine
  {
  
      static Dictionary&lt;string, int&gt; sLengths = new Dictionary&lt;string, int&gt;();
  
      public StringLengthValidator()
      {
          if (sLengths.Count == 0)
          {
              sLengths = new Dictionary&lt;string, int&gt;(){....};
          }
      }
  
      public Error Validate(string name, dynamic value)
      {
  
          var err = default(Error); 
          //logic here
  
          return err;
      }
  }
  
  public class MandatoryValidator : IValidationEngine
  {
  
      public Error Validate(string name, dynamic value)
      {
  
          var err = default(Error); 
          //logic here
  
          return err;
      }
  }
  
  public class MandatoryStringLengthValidator : IValidationEngine
  {
  
      public Error Validate(string name, dynamic value)
      {
  
          var e = default(Error);
  
          if (value == null || (value.GetType() == typeof(string) &amp;&amp; string.IsNullOrWhiteSpace(value)))
          {
              e = new Error()
              {
                  //.. err info
              };
          }
          else
          {
              StringLengthValidator sl = new StringLengthValidator();
              e = sl.Validate(name, value);
          }
  
          return e;
      }
  }
  </code></pre>
  
  <p>There is another class which I named it <code>ValidationRouter</code>. The job of this is to route to the specific validator (Think of this as a <a href="http://www.dofactory.com/net/strategy-design-pattern" rel="nofollow noreferrer">Strategy as per pattern</a>).</p>
  
  <p>I wrote the initial router to have a dictionary of keys and its respective objects map so that whatever comes in will match its key and calls its appropriate <code>validate()</code> method on the dictionary value. But as adding keys and values with new object creation. I think the class will take lot of memory as I've the object creation in the constructor.</p>
  
  <p>So, I've added Lazy to the object creation and it ended up like this</p>
  
  <p>Here is the code for router</p>
  
  <pre><code>public class ValidationRouter
  {
      public Dictionary&lt;string, Lazy&lt;IValidationEngine&gt;&gt; strategies = new Dictionary&lt;string, Lazy&lt;IValidationEngine&gt;&gt;();
  
      public ValidationRouter()
      {
          var mandatoryStringLength = new Lazy&lt;IValidationEngine&gt;(() =&gt; new MandatoryStringLengthValidator());
          var mandatory = new Lazy&lt;IValidationEngine&gt;(() =&gt; new MandatoryValidator());
          var stringLength = new Lazy&lt;IValidationEngine&gt;(() =&gt; new StringLengthValidator());
  
          strategies.Add("position", mandatoryStringLength);
          strategies.Add("pcode", stringLength);
          strategies.Add("username", mandatoryStringLength);
          strategies.Add("description", stringLength);
          strategies.Add("sourcename", stringLength);
  
          //OMG: 35 more fields to be added to route to specific routers
      }
  
      public Error Execute(string name, dynamic value)
      {
          //Find appropriate field and route to its strategy
          var lowered = name.ToLower();
          if (!strategies.ContainsKey(lowered)) return default(Error);
  
          return strategies[lowered].Value.Validate(name, value);
      }
  }
  </code></pre>
  
  <p>As you can see I need to add 35 more keys to dictionaries with appropriate strategies in the constructor.</p>
  
  <p>Is the approach correct or is there any better way of routing to specific validator algorithms ?</p>
  
  <p>I thought of object creation to be done by factory pattern with <code>Activator.CreateInstance</code> but not sure how to achieve as each of my properties will have different strategies.</p>
  
  <p>Any ideas would be appreciated.</p>