Make Encapsulation Easy With dotNetTips.Utility

Encapsulation is the first pillar of Object Oriented Programming (OOP), yet most code that I see does not implement encapsulation correctly or not all. Like I say in many of my conference sessions "If you do not implement encapsulation, you aren't doing OOP!" I also say "Bad data in, bad data out!".

Several years ago, Microsoft Labs came out with Code Contracts which made encapsulation easy so I implemented in all my projects and even did conference sessions about it. Unfortunately, the future of Code Contracts looks dim and it does not work in .NET Core or .NET Standard. Since I moved some of the code I’ve been writing since .NET came out in my open source project dotNetTips.Utility to .NET Standard, I needed a way to mimic the encapsulation features of Code Contracts.

Introducing the dotNetTips.Utility OOP Namespace

I created a new namespace in my open source project called OOP in both my .NET Portable project and .NET Standard project. The first class in this namespace is named Encapsulation. In this class, you will find overloaded methods called TryValidateParam, designed to make encapsulation easier. Here is the main method in this class.

  1. public static void TryValidateParam < TException > (bool condition, string paramName = "", string message = "")  
  2. where TException: ArgumentException, new() {  
  3.     // Confirm proper Exception type  
  4.     var t = typeof(TException);  
  5.     if (t.Name == nameof(Exception)) {  
  6.         throw new InvalidCastException(Resources.CannotBeOfTypeException, nameof(TException)));  
  7. }  
  8. var defaultMessage = Resources.ParameterIsInvalid;  
  9. if (string.IsNullOrEmpty(message) == false) {  
  10.     defaultMessage = message;  
  11. }  
  12. if (condition == false) {  
  13.     var ex = Activator.CreateInstance(typeof(TException), paramName, defaultMessage).As < TException > ();  
  14.     throw ex;  
  15. }  
  16. }  

There are a few things I would like to point out in this method. First, it prevents users from throwing the type Exception, which you should never do since it is not descriptive enough. The only Exception type that should be thrown when checking parameters is one derived from ArgumentException. The exceptions included in the .NET Framework are:

  • ArgumentNullException
  • ArgumentOutOfRangeException

If one of these types does not meet your needs, then create your own inheriting ArgumentException. I did this in dotNetTips.Utility with the ArgumentInvalidException and ArgumentReadOnlyException.

The overloaded methods in this class makes it easy to check strings, collections, enums, GUID’s and more.


Here is an example on how to use TryValidateParam (also from dotNetTips.Utility).

  1. public static DateTime GetLastDay(this DateTime input, DayOfWeek dayOfWeek) {  
  2.     Encapsulation.TryValidateParam(dayOfWeek, nameof(dayOfWeek));  
  3.     var daysToSubtract = input.DayOfWeek > dayOfWeek ? input.DayOfWeek - dayOfWeek : (7 - (int) dayOfWeek) + (int) input.DayOfWeek;  
  4.     return input.AddDays(daysToSubtract * -1);  
  5. }  

I hope you will implement this in your code too. To download from GitHub, click here. If you have any comments about this project, please add one on the GitHub site.