ncosentino

Lambda Refactor Tutorial - Post Refactor Classes

Nov 12th, 2013
129
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.Globalization;
  16. using System.Text;
  17.  
  18. using LambdaRefactor.Processing.PostRefactor.Numeric;
  19. using LambdaRefactor.Processing.PostRefactor.String;
  20.  
  21. namespace LambdaRefactor.Processing.PostRefactor
  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 delegate bool ProcessDelegate<T>(object importantReference, T processorValue, T input);
  38.  
  39.         public bool TryProcess(object input)
  40.         {
  41.             if (input == null)
  42.             {
  43.                 return false;
  44.             }
  45.  
  46.             return Process(_importantReference, input);
  47.         }
  48.  
  49.         protected abstract bool Process(object importantReference, object input);
  50.     }
  51.  
  52.     public class ProcessorFactory : IProcessorFactory
  53.     {
  54.         public IProcessor Create(ProcessorType type, object mandatoryArgument, object value)
  55.         {
  56.             switch (type)
  57.             {
  58.                 case ProcessorType.GreaterThan:
  59.                     return new NumericProcessor(mandatoryArgument, value, (_, x, y) => x < y);
  60.                 case ProcessorType.StringEqual:
  61.                     return new StringProcessor(mandatoryArgument, value, (_, x, y) => x == y);
  62.                 /*
  63.                  * Look how easy it is to add new processors! Exercise for you:
  64.                  * implement the remaining processors in the enum!
  65.                  */
  66.                 default:
  67.                     throw new NotImplementedException("The processor type '" + type + "' has not been implemented in this factory.");
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. namespace LambdaRefactor.Processing.PostRefactor.String
  74. {
  75.     public class StringProcessor : Processor
  76.     {
  77.         private readonly string _value;
  78.         private readonly ProcessDelegate<string> _processDelegate;
  79.  
  80.         public StringProcessor(object mandatoryArgument, object value, ProcessDelegate<string> processDelegate)
  81.             : base(mandatoryArgument)
  82.         {
  83.             if (value == null)
  84.             {
  85.                 throw new ArgumentNullException("value");
  86.             }
  87.  
  88.             if (processDelegate == null)
  89.             {
  90.                 throw new ArgumentNullException("processDelegate");
  91.             }
  92.  
  93.             _value = (string)value; // will throw exception on mismatch
  94.             _processDelegate = processDelegate;
  95.         }
  96.  
  97.         protected override bool Process(object importantReference, object input)
  98.         {
  99.             return _processDelegate(importantReference, _value, Convert.ToString(input, System.Globalization.CultureInfo.InvariantCulture));
  100.         }
  101.     }
  102. }
  103.  
  104. namespace LambdaRefactor.Processing.PostRefactor.Numeric
  105. {
  106.     public class NumericProcessor : Processor
  107.     {
  108.         private readonly decimal _value;
  109.         private readonly ProcessDelegate<decimal> _processDelegate;
  110.  
  111.         public NumericProcessor(object mandatoryArgument, object value, ProcessDelegate<decimal> processDelegate)
  112.             : base(mandatoryArgument)
  113.         {
  114.             if (value == null)
  115.             {
  116.                 throw new ArgumentNullException("value");
  117.             }
  118.  
  119.             if (processDelegate == null)
  120.             {
  121.                 throw new ArgumentNullException("processDelegate");
  122.             }
  123.  
  124.             _value = Convert.ToDecimal(value, CultureInfo.InvariantCulture); // will throw exception on mismatch
  125.             _processDelegate = processDelegate;
  126.         }
  127.  
  128.         protected override bool Process(object importantReference, object input)
  129.         {
  130.             decimal numericInput;
  131.             try
  132.             {
  133.                 numericInput = Convert.ToDecimal(input, CultureInfo.InvariantCulture);
  134.             }
  135.             catch (Exception)
  136.             {
  137.                 return false;
  138.             }
  139.  
  140.             return _processDelegate(importantReference, _value, numericInput);
  141.         }
  142.     }
  143. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×