SHARE
TWEET

Lambda Refactor Tutorial - Pre-Refactor Classes

ncosentino Nov 12th, 2013 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Lambda Refactor Tutorial - Dev Leader
  2.  *
  3.  * Blog Post:
  4.  * http://devleader.ca/2013/11/14/lambdas-example-refactoring-code
  5.  *
  6.  * Classes:
  7.  * Program.cs:              http://pastebin.com/HsvqqBka
  8.  * Base Classes:            http://pastebin.com/s99Swhuk
  9.  * Pre Refactor Classes:    http://pastebin.com/4645gEgz
  10.  * Post Refactor Classes:   http://pastebin.com/ySLG2KqU
  11.  *
  12.  */
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. using System.Globalization;
  17.  
  18. using LambdaRefactor.Processing.PreRefactor.Numeric;
  19. using LambdaRefactor.Processing.PreRefactor.String;
  20.  
  21. namespace LambdaRefactor.Processing.PreRefactor
  22. {
  23.     public abstract class Processor : IProcessor
  24.     {
  25.         private readonly object _importantReference;
  26.  
  27.         public Processor(object mandatoryArgument)
  28.         {
  29.             if (mandatoryArgument == null)
  30.             {
  31.                 throw new ArgumentNullException("mandatoryArgument");
  32.             }
  33.  
  34.             _importantReference = mandatoryArgument;
  35.         }
  36.  
  37.         public bool TryProcess(object input)
  38.         {
  39.             if (input == null)
  40.             {
  41.                 return false;
  42.             }
  43.  
  44.             return Process(_importantReference, input);
  45.         }
  46.  
  47.         protected abstract bool Process(object importantReference, object input);
  48.     }
  49.  
  50.     public class ProcessorFactory : IProcessorFactory
  51.     {
  52.         public IProcessor Create(ProcessorType type, object mandatoryArgument, object value)
  53.         {
  54.             switch (type)
  55.             {
  56.                 case ProcessorType.GreaterThan:
  57.                     return new GreaterProcessor(mandatoryArgument, value);
  58.                 case ProcessorType.StringEqual:
  59.                     return new StringEqualsProcessor(mandatoryArgument, value);
  60.                 /*
  61.                  * we still have to go implement all the other classes!
  62.                  */
  63.                 default:
  64.                     throw new NotImplementedException("The processor type '" + type + "' has not been implemented in this factory.");
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. namespace LambdaRefactor.Processing.PreRefactor.String
  71. {
  72.     public class StringEqualsProcessor : Processor
  73.     {
  74.         private readonly string _value;
  75.  
  76.         public StringEqualsProcessor(object mandatoryArgument, object value)
  77.             : base(mandatoryArgument)
  78.         {
  79.             if (value == null)
  80.             {
  81.                 throw new ArgumentNullException("value");
  82.             }
  83.  
  84.             _value = (string)value; // will throw exception on mismatch
  85.         }
  86.  
  87.         protected override bool Process(object importantReference, object input)
  88.         {
  89.             return Convert.ToString(input, System.Globalization.CultureInfo.InvariantCulture).Equals(_value);
  90.         }
  91.     }
  92. }
  93.  
  94. namespace LambdaRefactor.Processing.PreRefactor.Numeric
  95. {
  96.     public class GreaterProcessor : Processor
  97.     {
  98.         private readonly decimal _value;
  99.  
  100.         public GreaterProcessor(object mandatoryArgument, object value)
  101.             : base(mandatoryArgument)
  102.         {
  103.             if (value == null)
  104.             {
  105.                 throw new ArgumentNullException("value");
  106.             }
  107.  
  108.             _value = Convert.ToDecimal(value, CultureInfo.InvariantCulture); // will throw exception on mismatch
  109.         }
  110.  
  111.         protected override bool Process(object importantReference, object input)
  112.         {
  113.             decimal numericInput;
  114.             try
  115.             {
  116.                 numericInput = Convert.ToDecimal(input, CultureInfo.InvariantCulture);
  117.             }
  118.             catch (Exception)
  119.             {
  120.                 return false;
  121.             }
  122.  
  123.             return numericInput > _value;
  124.         }
  125.     }
  126. }
RAW Paste Data
Top