Microsoft’s DateTime Structure is an object that represents an instant in time, typically expressed as a date and time of day.
In Sage 300’s Web Screens, we use this object for dates and times in our C-Sharp code. However, we do not use its numerous properties and methods directly. Instead, we have encapsulated this functionality in our DateUtil class.
Let’s me explain why we came to the decision to encapsulate this functionality.
Encapsulation and Technical Debt
But, first let’s cover some concepts that were at the core of our decision.
What is encapsulation?
Encapsulation is the ability to provide users with a well-defined interface to a set of functions in a way which hides their internal workings.
Benefits of encapsulation
One of the principles of encapsulation is that the internal representation of an object should not concern its consumers. It also hides complex mechanisms and provides code reduction.
What is technical debt?
Technical debt is the set of problems in a development effort that make progress on customer value inefficient.
What does technical debt look like?
Technical debt are problems found through code analysis, duplicate code patterns, code complexity, spaghetti code, …
We analyzed the code and determined that individual usage of the DateTime properties numbered in excess of ten thousand lines of code. Yikes.
Sage 300 Web Screens can be localized and therefore these lines of code had to specify a locale along with the necessary conditional logic.
And, unfortunately we had different patterns being implemented in those individual lines of code.
So, the decision to encapsulate and to create a single design pattern for the developers to access a date and time was the best choice.
Date Utility Class
Thus, the DateUtil class was created and is available in the Sage.CA.SBS.ERP.Sage300.Common.Utilities assembly.
Why should I use this class?
Sage 300 Web Screen Date and Time Pattern
Let this class do the date validations, acquire a new date, perform conditional checks, etc. for you
It’s the standard for Sage 300 Web Screen developers
Minimum Date logic is centralized
The DateTime structure has different values for the minimum date value based upon how the object was instantiated
The DateUtil class has the ability to standardize on a single minimum date value and therefore the application will not have multiple minimum date possibilities
Formatting based upon Current Culture
The encapsulation allows for the DateUtil class to perform the culture specific logic
This eliminates the developer from having to specify this where required
It’s easy and does the work for you
Enough said on this!
Before and After Examples
In this section, I will show some examples where we replaced code in our application with usage of the DateUtil class.
Example 1: Before
Example 1: After
Example 2: Before
Example 2: After
Example 3: Before
Example 3: After
Example 4: Before
Example 4: After
This article presented the DateUtil class for dealing with dates and times in the Sage 300 Web Screens (C-Sharp code) along with the benefits of using a single pattern by developers.
By encapsulating the date and time functions, we are able to provide a consistent and simple pattern while reducing the potential for technical debt.
When this class was implemented, we removed over 6,500 lines of code from the application.
The DateUtil class is available in the Sage.CA.SBS.ERP.Sage300.Common.Utilities assembly which is referenced in most if not all projects.
As a standard disclaimer, any topic in this article is subject to review and doesn’t represent a commitment as to when it will be available.