Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Lambda Refactor Tutorial - Dev Leader
- *
- * Blog Post:
- * http://devleader.ca/2013/11/14/lambdas-example-refactoring-code
- *
- * Classes:
- * Program.cs: http://pastebin.com/HsvqqBka
- * Base Classes: http://pastebin.com/s99Swhuk
- * Pre Refactor Classes: http://pastebin.com/4645gEgz
- * Post Refactor Classes: http://pastebin.com/ySLG2KqU
- *
- */
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Text;
- using LambdaRefactor.Processing.PostRefactor.Numeric;
- using LambdaRefactor.Processing.PostRefactor.String;
- namespace LambdaRefactor.Processing.PostRefactor
- {
- public abstract class Processor : IProcessor
- {
- private readonly object _importantReference;
- public Processor(object mandatoryArgument)
- {
- if (mandatoryArgument == null)
- {
- throw new ArgumentNullException("mandatoryArgument");
- }
- _importantReference = mandatoryArgument;
- }
- public delegate bool ProcessDelegate<T>(object importantReference, T processorValue, T input);
- public bool TryProcess(object input)
- {
- if (input == null)
- {
- return false;
- }
- return Process(_importantReference, input);
- }
- protected abstract bool Process(object importantReference, object input);
- }
- public class ProcessorFactory : IProcessorFactory
- {
- public IProcessor Create(ProcessorType type, object mandatoryArgument, object value)
- {
- switch (type)
- {
- case ProcessorType.GreaterThan:
- return new NumericProcessor(mandatoryArgument, value, (_, x, y) => x < y);
- case ProcessorType.StringEqual:
- return new StringProcessor(mandatoryArgument, value, (_, x, y) => x == y);
- /*
- * Look how easy it is to add new processors! Exercise for you:
- * implement the remaining processors in the enum!
- */
- default:
- throw new NotImplementedException("The processor type '" + type + "' has not been implemented in this factory.");
- }
- }
- }
- }
- namespace LambdaRefactor.Processing.PostRefactor.String
- {
- public class StringProcessor : Processor
- {
- private readonly string _value;
- private readonly ProcessDelegate<string> _processDelegate;
- public StringProcessor(object mandatoryArgument, object value, ProcessDelegate<string> processDelegate)
- : base(mandatoryArgument)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- if (processDelegate == null)
- {
- throw new ArgumentNullException("processDelegate");
- }
- _value = (string)value; // will throw exception on mismatch
- _processDelegate = processDelegate;
- }
- protected override bool Process(object importantReference, object input)
- {
- return _processDelegate(importantReference, _value, Convert.ToString(input, System.Globalization.CultureInfo.InvariantCulture));
- }
- }
- }
- namespace LambdaRefactor.Processing.PostRefactor.Numeric
- {
- public class NumericProcessor : Processor
- {
- private readonly decimal _value;
- private readonly ProcessDelegate<decimal> _processDelegate;
- public NumericProcessor(object mandatoryArgument, object value, ProcessDelegate<decimal> processDelegate)
- : base(mandatoryArgument)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- if (processDelegate == null)
- {
- throw new ArgumentNullException("processDelegate");
- }
- _value = Convert.ToDecimal(value, CultureInfo.InvariantCulture); // will throw exception on mismatch
- _processDelegate = processDelegate;
- }
- protected override bool Process(object importantReference, object input)
- {
- decimal numericInput;
- try
- {
- numericInput = Convert.ToDecimal(input, CultureInfo.InvariantCulture);
- }
- catch (Exception)
- {
- return false;
- }
- return _processDelegate(importantReference, _value, numericInput);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement