Improve Your Model Classes With OOP - Part Two - Constructors, Interfaces And More

In part 1 of this series, I discussed building model classes properly with Object-Oriented Programming (OOP), specifically encapsulation that must include data validation. In this article, I’m going to show you constructors, interfaces and more that you should implement for your model classes. We will build upon the Person.cs type from part 1 and end up with a new Person type as shown to the right. Some of what I will show can speed up the performance of Person during sorting.
Improve Your Model Classes With OOP - Part Two - Constructors, Interfaces And More


Types in .NET require a constructor so that they can be created. When a class is created using Visual Studio, it does not add it (for some reason). If you don’t implement one, then the compiler will add an empty constructor for you during the build process. This is called the “magic constructor”, since it’s added for you. If you want to use a constructor to set data, then that can be done like this.
  1. public Person(string id, string email)  
  2. {  
  3.     Email = email;  
  4.     Id = id;  
  5. }  
My rule for constructors is that the data being set is required for the type. For Person, I am making Id and Email required. I also use this for other types of classes such as data context classes where the connection string is required.
I’d like to point out that I am using the properties to set the data, not directly setting the private fields. This way the data set in the constructor goes through the same validation as outside code setting the properties.
When you create a constructor like this, the compiler won’t add the “magic constructor” anymore. The empty constructor is required for serialization. You need to add it yourself like this.
  1. /// <summary>  
  2. /// Initializes a new instance of the   
  3. /// <see cref="T:Article2.Person"/> class.  
  4. /// </summary>  
  5. public Person()  
  6. {}  
If this empty constructor is not added, then deserialization for the type will not work. I never add constructors to make it easy for someone to set data. Since we now have object initialization in .NET, there isn’t’ a reason to. Here is an example.
  1. var person = new Person("ASODIDADLVOD109A""[email protected]")  
  2. {  
  3.      FirstName = "David",  
  4.      LastName = "McCarter"  
  5. };  
When I design types like this, I want to require users of the type to send in the email address and id and not use the empty constructor. There is a way to hide from Intellisense, the empty constructor like this.
  1. [EditorBrowsable(EditorBrowsableState.Never)]  
  2. public Person()  
  3. {}  
Using the EditorBrowsable attribute will hide the constructor, or any other method, from types outside of the assembly that the model is located in.
By default, when objects are compared in .NET, reflection is used, which can affect the performance of that type. The next three topics will tackle this issue. Make sure to benchmark the performance of your type.

Implementing IComparable & IComparable<>

To make the ordering of object better, it’s recommended to implement the IComparable and IComparable<> interfaces. Now the definition of the type looks like this.
  1. public class Person : IEquatable<Person>, IComparable,  
  2.                       IComparable<Person>  
For Person, I am implementing the two methods like this.
  1. public int CompareTo(object obj)  
  2. {  
  3.     if (obj == null)  
  4.     {  
  5.         return 1;  
  6.     }  
  8.     var other = obj as Person;  
  9.     if (other == null)  
  10.     {  
  11.         throw new ArgumentException(nameof(obj) +   
  12.                   " is not a " + nameof(Person));  
  13.     }  
  15.     return CompareTo(other);  
  16. }  
  18. public int CompareTo(Person other)  
  19. {  
  20.     if (other == null)  
  21.     {  
  22.         return 1;  
  23.     }  
  25.     int result = 0;  
  27.     result = _email.CompareTo(other._email);  
  28.     if (result != 0)  
  29.     {  
  30.         return result;  
  31.     }  
  33.     return result;  
  34. }  
As you can see in the second CompareTo() method, I am only showing comparing the email address. All the properties are compared in the actual class.

Performance Increase

Implementing these two interfaces can increase the performance when sorting a collection of that type in .NET Core 3 and the .NET Framework 4.8. In the chart below, the performance is benchmarked by comparing a Person class without these interfaces (Mean in the chart) against a Person class that implement these interfaces (Overloaded in the chart).
Improve Your Model Classes With OOP - Part Two - Constructors, Interfaces And More 
As you can see, implanting these two interfaces can cause a 17-26% performance increase. So why not implement them?

Overloading GetHashCode()

Overloading the GetHashCode() method, could be a performance increase and you can choose what data will used to create the hash code. Here is how I did it for Person.
  1. /// <summary>Returns a hash code for this instance.</summary>  
  2. /// <returns>  
  3. /// A hash code for this instance, suitable for use in hashing   
  4. /// algorithms and data structures like a hash table.  
  5. /// </returns>  
  6. public override int GetHashCode()  
  7. {  
  8.     HashCode.Combine(Email, Id);  
  9. }  
As you can see, I am only using the data for the email address and id for the hash code. Overriding GetHashCode() is simple when using refactoring tools.

Making Classes Easier to Debug

Finally, for this article, is making your type easier to use when debugging. Here is why let’s say I have a collection of Person and I want to look at its values. This is the default experience in Visual Studio.
Improve Your Model Classes With OOP - Part Two - Constructors, Interfaces And More 
As you can see, by default it shows the full type name. Not very useful is it? Well, there is a new attribute called DebuggerDisplay that you can use to fix this.
  1. [DebuggerDisplay("{Email}")]  
  2. public class Person : IEquatable<Person>, IComparable,   
  3.                       IComparable<Person>  
Using DebuggerDisplay, I can set what properties that will be used for the display while debugging.
Improve Your Model Classes With OOP - Part Two - Constructors, Interfaces And More 
As you can see, this is much more useful and anyone using this type will thank you!

Override ToString()

While you are at it, you should override ToString() too. As with debugging, ToString() will just return the type name that again isn’t very useful. Here is how I did it for Person.
  1. public override string ToString()  
  2. {  
  3.     return $"{Id} - {Email}";  
  4. }  


As you can see, there is much more to building good model classes than just implementing properties. But we are not done yet. In the next article, I will tackle serialization. Most models get serialized and deserialized when going through a web API (service) or something similar.
The Person class is now over 600 lines of code and documentation, too large to post in this article. You can view it by going to here.
Do you practice good OOP design? Well, let’s see what you think at the end of this series. I’d be interested to know. Do have any tips you’d like to share? Please make a comment below.

Similar Articles
McCarter Consulting
Software architecture, code & app performance, code quality, Microsoft .NET & mentoring. Available!