SOLID principles in .NET revisited part 10: concentrating on enumerations resolved
May 25, 2015 2 Comments
Introduction
In the previous post we built some bad code. We built it like that deliberately to make you aware of how easy it is to misuse enumerations. We ended up with classes that are coupled in a way that if you extend one then you’ll need to extend at least one other.
In this post we’ll refactor the code to make it better.
Objects instead of enumerations
The main point I want to make in this exercise is that building “proper” objects instead of enumerations can make your code base much more flexible. It’s important to note that there are multiple ways to improve the code. I’m only going to present a solution that I think is good. You might come up with a different solution and it may well be just as good or even superior. The main thing is that the components of your code base should be as loosely coupled as possible.
The PerformanceMetricType enumeration and its members will be transformed into an abstract base class and two implementations. The abstract class will have a single “description” parameter so that the Metric type can be described. It will also have an abstract method where the implementing type will have to extract the correct value from the PerformanceSummary object:
public abstract class Metric { private string _description; public Metric(String description) { if (string.IsNullOrEmpty(description)) throw new ArgumentNullException("Metric description"); _description = description; } public string Description { get { return _description; } } public abstract double ExtractActualValueFrom(PerformanceSummary performanceSummary); }
Here come the implementations as well. First AverageResponseTimeMetric:
public class AverageResponseTimeMetric : Metric { public AverageResponseTimeMetric() : base("Average response time per page") {} public override double ExtractActualValueFrom(PerformanceSummary performanceSummary) { return performanceSummary.AverageResponseTimePerPage; } }
…and second the UrlCallsPassedPerMinuteMetric:
public class UrlCallsPassedPerMinuteMetric : Metric { public UrlCallsPassedPerMinuteMetric() : base("Url calls passed per minute") {} public override double ExtractActualValueFrom(PerformanceSummary performanceSummary) { return performanceSummary.TotalPassedCallsPerMinute; } }
We’ll do the same with the EvaluationOperator enumeration. The abstract base class EvaluationOperation also has a description field and a single abstract method. The implementing class will have to determine whether the actual value breaks a certain limit:
public abstract class EvaluationOperation { private string _description; public EvaluationOperation(String description) { if (string.IsNullOrEmpty(description)) throw new ArgumentNullException("Description"); _description = description; } public string Description { get { return _description; } } public abstract bool LimitBroken(double limit, double actual); }
Here’s the GreaterThan implementation of EvaluationOperation:
public class GreaterThan : EvaluationOperation { public GreaterThan() : base ("Greater than") {} public override bool LimitBroken(double limit, double actual) { return actual > threshold; } }
…and here comes LessThan:
public class LessThan : EvaluationOperation { public LessThan() : base("Less than") { } public override bool LimitBroken(double limit, double actual) { return actual < threshold; } }
ThresholdEvaluationResult is the same as before:
public class ThresholdEvaluationResult { public bool ThresholdBroken { get; set; } }
The updated Threshold class looks quite different from what we had before. The enumeration parameters are gone from the constructor and have been replaced by abstract classes. Also, the logic of evaluating whether a threshold is broken has been placed within the Threshold class instead of some specialised “service”:
public class Threshold { private Metric _metric; private EvaluationOperation _operation; private double _thresholdValue; public Threshold(Metric metric, EvaluationOperation operation, double thresholdValue) { if (metric == null) throw new ArgumentNullException("Metric"); if (operation == null) throw new ArgumentNullException("Operation"); _metric = metric; _operation = operation; _thresholdValue = thresholdValue; } public ThresholdEvaluationResult Evaluate(PerformanceSummary performanceSummary) { ThresholdEvaluationResult res = new ThresholdEvaluationResult(); double actualValue = _metric.ExtractActualValueFrom(performanceSummary); bool broken = _operation.LimitBroken(_thresholdValue, actualValue); res.ThresholdBroken = broken; return res; } }
As you see the Evaluate method calls upon the injected abstract classes in order to determine whether some limit has been broken. The Threshold class knows nothing about the actual implementations. Also, if you need to extend your domain with other Operation types, such as EqualTo, LessThanOrEqualTo or implement new metric types, like CpuUsageMetric, then you can do that in isolation. Other classes in the model won’t need to be updated at all. The Threshold class will happily accept the new implementations without any complaints.
This post concludes our discussion of SOLID in .NET for the time being.
View the list of posts on Architecture and Patterns here.
Pingback: Architecture and patterns | Michael's Excerpts
Thank You !! Nicely Explained !!