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.Text;
- using System.Globalization;
- using LambdaRefactor.Processing.PreRefactor.Numeric;
- using LambdaRefactor.Processing.PreRefactor.String;
- namespace LambdaRefactor.Processing.PreRefactor
- {
- public abstract class Processor : IProcessor
- {
- private readonly object _importantReference;
- public Processor(object mandatoryArgument)
- {
- if (mandatoryArgument == null)
- {
- throw new ArgumentNullException("mandatoryArgument");
- }
- _importantReference = mandatoryArgument;
- }
- 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 GreaterProcessor(mandatoryArgument, value);
- case ProcessorType.StringEqual:
- return new StringEqualsProcessor(mandatoryArgument, value);
- /*
- * we still have to go implement all the other classes!
- */
- default:
- throw new NotImplementedException("The processor type '" + type + "' has not been implemented in this factory.");
- }
- }
- }
- }
- namespace LambdaRefactor.Processing.PreRefactor.String
- {
- public class StringEqualsProcessor : Processor
- {
- private readonly string _value;
- public StringEqualsProcessor(object mandatoryArgument, object value)
- : base(mandatoryArgument)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- _value = (string)value; // will throw exception on mismatch
- }
- protected override bool Process(object importantReference, object input)
- {
- return Convert.ToString(input, System.Globalization.CultureInfo.InvariantCulture).Equals(_value);
- }
- }
- }
- namespace LambdaRefactor.Processing.PreRefactor.Numeric
- {
- public class GreaterProcessor : Processor
- {
- private readonly decimal _value;
- public GreaterProcessor(object mandatoryArgument, object value)
- : base(mandatoryArgument)
- {
- if (value == null)
- {
- throw new ArgumentNullException("value");
- }
- _value = Convert.ToDecimal(value, CultureInfo.InvariantCulture); // will throw exception on mismatch
- }
- protected override bool Process(object importantReference, object input)
- {
- decimal numericInput;
- try
- {
- numericInput = Convert.ToDecimal(input, CultureInfo.InvariantCulture);
- }
- catch (Exception)
- {
- return false;
- }
- return numericInput > _value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement