ncosentino

Lambda Refactor Tutorial - Program.cs

Nov 12th, 2013
192
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.  
  17. using LambdaRefactor.Processing;
  18.  
  19. using PreRefactorFactory = LambdaRefactor.Processing.PreRefactor.ProcessorFactory;
  20. using PostRefactorFactory = LambdaRefactor.Processing.PostRefactor.ProcessorFactory;
  21.  
  22. namespace LambdaRefactor
  23. {
  24.     internal class Program
  25.     {
  26.         private static void Main(string[] args)
  27.         {
  28.             // this entire example is based on a real-world example. i've
  29.             // opted to try and introduce some complexity to the system by
  30.             // creating some artificial dependency on an object reference.
  31.             // throughout the code, you'll see that many of the classes depend
  32.             // on having this set, although we never really do anything with
  33.             // it. this is to simulate something closer to production code
  34.             // where you may actually have additional dependencies passed down
  35.             // your call/class hierarchy that you need to worry about.
  36.             Console.WriteLine("Creating our super important dependency...");
  37.             var superImportantReference = new object();
  38.  
  39.             // let's create an instance of our pre-refactor factory and an
  40.             // instance of our factory after the lambda refactor.
  41.             Console.WriteLine("Creating our pre/post refactor factory instances...");
  42.             var preRefactorFactory = new PreRefactorFactory();
  43.             var postRefactorFactory = new PostRefactorFactory();
  44.  
  45.             // run all of the tests! (i opted to not include any real test
  46.             // framework, but the concepts still apply)
  47.             Console.WriteLine("Running 'GreaterThan' processor tests...");
  48.             TestGreaterThan(preRefactorFactory, superImportantReference);
  49.             TestGreaterThan(postRefactorFactory, superImportantReference);
  50.  
  51.             Console.WriteLine("Running 'StringEquals' processor tests...");
  52.             TestStringEqual(preRefactorFactory, superImportantReference);
  53.             TestStringEqual(postRefactorFactory, superImportantReference);
  54.  
  55.             /*
  56.              * Exercise for you: implement the other processors in the pre-
  57.              * refactor code and in the post refactor code. which method
  58.              * involved more work?
  59.              */
  60.  
  61.             //TestLessThan(preRefactorFactory, superImportantReference);
  62.             //TestLessThan(postRefactorFactory, superImportantReference);
  63.  
  64.             /*
  65.              * Code some of the remaining test functions to see if your
  66.              * implementations match up!
  67.              */
  68.  
  69.             Console.WriteLine("Press 'enter' to exit.");
  70.             Console.ReadLine();
  71.         }
  72.  
  73.         private static void TestGreaterThan(IProcessorFactory factory, object superImportantReference)
  74.         {
  75.             const int FILTER_VALUE = 10;
  76.             var processor = factory.Create(
  77.                 ProcessorType.GreaterThan,
  78.                 superImportantReference,
  79.                 FILTER_VALUE);
  80.  
  81.             if (processor.TryProcess(FILTER_VALUE - 5))
  82.             {
  83.                 throw new InvalidOperationException("This should not have been greater than.");
  84.             }
  85.  
  86.             if (!processor.TryProcess(FILTER_VALUE + 5))
  87.             {
  88.                 throw new InvalidOperationException("This should have been greater than.");
  89.             }
  90.  
  91.             if (processor.TryProcess("This isn't a number."))
  92.             {
  93.                 throw new InvalidOperationException("This should have failed because the input is invalid.");
  94.             }
  95.  
  96.             if (processor.TryProcess(new object()))
  97.             {
  98.                 throw new InvalidOperationException("This should have failed because the input is invalid.");
  99.             }
  100.         }
  101.  
  102.         private static void TestLessThan(IProcessorFactory factory, object superImportantReference)
  103.         {
  104.             const int FILTER_VALUE = 10;
  105.             var processor = factory.Create(
  106.                 ProcessorType.LessThan,
  107.                 superImportantReference,
  108.                 FILTER_VALUE);
  109.  
  110.             if (!processor.TryProcess(FILTER_VALUE - 5))
  111.             {
  112.                 throw new InvalidOperationException("This should have been less than.");
  113.             }
  114.  
  115.             if (processor.TryProcess(FILTER_VALUE + 5))
  116.             {
  117.                 throw new InvalidOperationException("This should not have been less than.");
  118.             }
  119.  
  120.             if (processor.TryProcess("This isn't a number."))
  121.             {
  122.                 throw new InvalidOperationException("This should have failed because the input is invalid.");
  123.             }
  124.  
  125.             if (processor.TryProcess(new object()))
  126.             {
  127.                 throw new InvalidOperationException("This should have failed because the input is invalid.");
  128.             }
  129.         }
  130.  
  131.         private static void TestStringEqual(IProcessorFactory factory, object superImportantReference)
  132.         {
  133.             const string FILTER_VALUE = "Hello, World!";
  134.             var processor = factory.Create(
  135.                 ProcessorType.StringEqual,
  136.                 superImportantReference,
  137.                 FILTER_VALUE);
  138.  
  139.             if (!processor.TryProcess(FILTER_VALUE))
  140.             {
  141.                 throw new InvalidOperationException("This should have matched.");
  142.             }
  143.  
  144.             if (processor.TryProcess("Goodbye, Cruel World!"))
  145.             {
  146.                 throw new InvalidOperationException("This should not have matched.");
  147.             }
  148.  
  149.             if (processor.TryProcess(10))
  150.             {
  151.                 throw new InvalidOperationException("This should not have matched.");
  152.             }
  153.  
  154.             if (processor.TryProcess(new object()))
  155.             {
  156.                 throw new InvalidOperationException("This should not have matched.");
  157.             }
  158.         }
  159.     }
  160. }
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.

×