Don't Repeat Yourself (DRY) - Part Two

In this article, we will be discussing one of the DRY issues - Repeated Logic - and some of the possible solutions to address this issue.

Let us continue with our discussion on DRY. We discussed the below two issues in our previous tutorials. 

In this tutorial, we are going to discuss another issue -- Repeated Logic. First, you should brush up your knowledge about Don't Repeat Yourself (DRY) Design Principle.

Repeated Logic

Consider that we have the below two domain objects.

Material Object

  1. public class Material {  
  2.     public long Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Description {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  

Order Object

  1. public class Order {  
  2.     public long Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6. }  

Let’s say that the IDs are not automatically generated when inserting a new row in the database. Instead, it must be calculated. So, we come up with the following function to construct an ID which is probably unique.

  1. private long GenerateId() {  
  2.     TimeSpan ts = DateTime.UtcNow - (new DateTime(1960, 1, 1, 0, 0, 0));  
  3.     long id = Convert.ToInt64(ts.TotalMilliseconds);  
  4.     return id;  
  5. }  

You may include this type of logic in both domain objects.

Update the Material Object as below.

  1. public class Material {  
  2.     public long Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Description {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     Material() {  
  11.         Id = GenerateId();  
  12.     }  
  13.     private long GenerateId() {  
  14.         TimeSpan ts = DateTime.UtcNow - (new DateTime(1960, 1, 1, 0, 0, 0));  
  15.         long id = Convert.ToInt64(ts.TotalMilliseconds);  
  16.         return id;  
  17.     }  
  18. }  

Update the Order Object as below.

  1. public class Order {  
  2.     public long Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     Order() {  
  7.         Id = GenerateId();  
  8.     }  
  9.     private long GenerateId() {  
  10.         TimeSpan ts = DateTime.UtcNow - (new DateTime(1960, 1, 1, 0, 0, 0));  
  11.         long id = Convert.ToInt64(ts.TotalMilliseconds);  
  12.         return id;  
  13.     }  
  14. }  
  • This situation may arise if two domain objects have been added to your application with a long time delay and you’ve forgotten about the ID generation solution. 
  • Also, if you want to keep the ID generation logic independent for each object, then you might continue with this solution thinking that someday the ID generation strategies may be different.

At some point, the rules change and all IDs of type long must be constructed using the GenerateId method. Then you absolutely want to have this logic in one place only. Otherwise if the rule changes, then you probably don’t want to make the same change for every single domain object

A very common solution would be to factor out this logic to a static method.

A Helper class would be added.

  1. public class IDGenerationHelper {  
  2.     public static long GenerateId() {  
  3.         TimeSpan ts = DateTime.UtcNow - (new DateTime(1960, 1, 1, 0, 0, 0));  
  4.         long id = Convert.ToInt64(ts.TotalMilliseconds);  
  5.         return id;  
  6.     }  
  7. }  

Updated the Material Domain Object as below.

  1. public class Material {  
  2.     public long Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Description {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     Material() {  
  11.         Id = IDGenerationHelper.GenerateId();  
  12.     }  
  13. }  

Update the Order Object as below.

  1. public class Order {  
  2.     public long Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     Order() {  
  7.         Id = IDGenerationHelper.GenerateId();  
  8.     }  
  9. }  

If you have followed the article's SOLID design principles, then you will know by now that static method indicates tight coupling. Here, there is a hard dependency on Material and Product classes on IDGenerationHelper.

If all objects in our domain must have an Id of type long then we may have let every object be derived from a superclass. A solution to factor out this logic is an abstract class.

The base class would be as below.

  1. public abstract class DomainBase {  
  2.     public long Id {  
  3.         get;  
  4.         private set;  
  5.     }  
  6.     public DomainBase() {  
  7.         Id = GenerateId();  
  8.     }  
  9.     private long GenerateId() {  
  10.         TimeSpan ts = DateTime.UtcNow - (new DateTime(1960, 1, 1, 0, 0, 0));  
  11.         long id = Convert.ToInt64(ts.TotalMilliseconds);  
  12.         return id;  
  13.     }  
  14. }  

Updated Material Object

  1. public class Material: DomainBase {  
  2.     public string Description {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public Material() {}  
  7. }  

Updated Order Object

  1. public class Order: DomainBase {  
  2.     public Order() {}  
  3. }  

If you construct a new Order or Material class elsewhere then the Id will be assigned by the DomainBase constructor automatically.

If we are not happy with the base class approach then Constructor Injection is another approach that can work. We delegate the Id generation logic to the external class which hides behind an interface:

Interface

  1. public interface IIdGenerator {  
  2.     long GenerateId();  
  3. }  

We have the below implementation class,

  1. public class IdGenerator: IIdGenerator {  
  2.     public long GenerateId() {  
  3.         TimeSpan ts = DateTime.UtcNow - (new DateTime(1970, 1, 1, 0, 0, 0));  
  4.         long id = Convert.ToInt64(ts.TotalMilliseconds);  
  5.         return id;  
  6.     }  
  7. }  

We can inject the interface dependency into Order Objects as below,

  1. public class Order {  
  2.     private IIdGenerator _idGenerator;  
  3.     public long Id {  
  4.         get;  
  5.         private set;  
  6.     }  
  7.     public Order(IIdGenerator idGenerator) {  
  8.         if (idGenerator == nullthrow new ArgumentNullException();  
  9.         this._idGenerator = idGenerator;  
  10.         Id = this._idGenerator.GenerateId();  
  11.     }  
  12. }  

We can apply the same method into Material Object,

  1. public class Material {  
  2.     private IIdGenerator _idGenerator;  
  3.     public string Description {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public long Id {  
  8.         get;  
  9.         private set;  
  10.     }  
  11.     public Material(IIdGenerator idGenerator) {  
  12.         if (idGenerator == nullthrow new ArgumentNullException();  
  13.         this._idGenerator = idGenerator;  
  14.         Id = this._idGenerator.GenerateId();  
  15.     }  
  16. }  

You can mix the above two solutions with the following DomainBase Super class.

  1. public abstract class DomainBase {  
  2.     private IIdGenerator _idGenerator;  
  3.     public long Id {  
  4.         get;  
  5.         private set;  
  6.     }  
  7.     public DomainBase(IIdGenerator idGenerator) {  
  8.         if (idGenerator == nullthrow new ArgumentNullException();  
  9.         this._idGenerator = idGenerator;  
  10.         Id = this._idGenerator.GenerateId();  
  11.     }  
  12. }  

So far, we have discussed some of the possible solutions that we can employ to factor out common logic so that it becomes available for different objects.

Obviously, if this logic occurs only within the same class then just simply create a private method for it.

Next, we will see how can we avoid If statements in software. 

  • If statements are very important building blocks of an application. 
  • It would probably be impossible to write any real-life app without them.
  • It does not mean they should be used without any limitation.

Consider the following domains,

  1. public abstract class Shape {}  
  2. public class Triangle: Shape {  
  3.     public int Base {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     public int Height {  
  8.         get;  
  9.         set;  
  10.     }  
  11. }  
  12. public class Rectangle: Shape {  
  13.     public int Width {  
  14.         get;  
  15.         set;  
  16.     }  
  17.     public int Height {  
  18.         get;  
  19.         set;  
  20.     }  

We can simulate a database mock as below.

  1. private static IEnumerable < Shape > GetAllShapes() {  
  2.     var shapes = new List < Shape > () {  
  3.         new Triangle() {  
  4.                 Base = 7, Height = 3  
  5.             },  
  6.             new Rectangle() {  
  7.                 Height = 8, Width = 4  
  8.             },  
  9.             new Triangle() {  
  10.                 Base = 10, Height = 5  
  11.             },  
  12.             new Rectangle() {  
  13.                 Height = 5, Width = 2  
  14.             }  
  15.     };  
  16.     return shapes;  
  17. }  

If we want to calculate the total area of the shapes in the collection, then first approach may look like below,

  1. public static double CalculateTotalArea(IEnumerable < Shape > shapes) {  
  2.     var area = 0.0;  
  3.     foreach(Shape shape in shapes) {  
  4.         if (shape is Triangle) {  
  5.             Triangle triangle = shape as Triangle;  
  6.             area += (triangle.Base * triangle.Height) / 2;  
  7.         } else if (shape is Rectangle) {  
  8.             Rectangle recangle = shape as Rectangle;  
  9.             area += recangle.Height * recangle.Width;  
  10.         }  
  11.     }  
  12.     return area;  
  13. }  

This is quite a common approach in a software design where our domain objects are plain collections of properties and are void of any self-contained logic. Look at the Triangle and Rectangle classes, they contain no logic whatsoever, they only have properties. They are reduced to the role of data-transfer-objects (DTOs). If you don’t understand at first what’s wrong with the above solution then I would suggest you go through the Liskov Substitution Principle here.

You may ask what this method has to do with DRY at all as we do not seem to repeat anything. Yes, we do, although indirectly. Our initial intention was to create a class hierarchy so that we can work with the abstract class Shape elsewhere. Well, guess what, we’ve failed miserably. In this method, we need to reveal not only the concrete implementation types of Shape but we’re forcing an external class to know about the internals of those concrete types.

Tell-Don’t-Ask (TDA) principle

 
It basically states that you should not ask object questions about its current state before you ask it to perform something.

The above piece of code is a clear violation of TDA although the lack of logic in the Triangle and Rectangle classes forced us to ask these questions.

The solution – or at least one of the viable solutions – will be to hide this calculation logic behind each concrete Shape class,

  1. public abstract class Shape {  
  2.     public abstract double CalculateArea();  
  3. }  
  4. public class Triangle: Shape {  
  5.     public int Base {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     public int Height {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public override double CalculateArea() {  
  14.         return (Base * Height) / 2;  
  15.     }  
  16. }  
  17. public class Rectangle: Shape {  
  18.     public int Width {  
  19.         get;  
  20.         set;  
  21.     }  
  22.     public int Height {  
  23.         get;  
  24.         set;  
  25.     }  
  26.     public override double CalculateArea() {  
  27.         return Width * Height;  
  28.     }  
  29. }  

The updated total Area calculation method looks like below,

  1. public static double CalculateTotalArea(IEnumerable < Shape > shapes) {  
  2.     var area = 0.0;  
  3.     foreach(Shape shape in shapes)  
  4.     area += shape.CalculateArea();  
  5.     return area;  
  6. }  

We’ve got rid of the “if” statements, we don’t violate TDA and the logic to calculate the area is hidden behind each concrete type. This allows us even to follow the above mentioned Liskov Substitution Principle.

In the upcoming tutorial, we will discuss the below DRY issue.

  • RepeatedExecution Pattern