I have a Business Object (Domain Object) representing an employee's shift timings. Its name is EmployeeWorkShift.
using System;
namespace BusinessObjects
{
public class EmployeeWorkShift
{
public long EmployeeWorkShiftId { get; set; }
public long EmployeeId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
}
}
I have a Repository to create, read, update and delete this Business Object in database. Its name is IEmployeeWorkShiftRepository.
I have a Service which has methods to perform operations with this Business Object. Its name is IEmployeeWorkShiftService.
The User Interface call the Service methods for different events:
To retrieve all
EmployeeWorkShiftobjects of an employee, it callsList<EmployeeWorkShift> GetEmployeeWorkShifts(long employeeId);methodTo retrieve a specific
EmployeeWorkShiftobject, it callsEmployeeWorkShift GetEmployeeWorkShift(long employeeWorkShiftId);methodTo insert a specific
EmployeeWorkShiftobject, it callsEmployeeWorkShift InsertEmployeeWorkShift(EmployeeWorkShift employeeWorkShift);methodTo update a specific
EmployeeWorkShiftobject, it callsEmployeeWorkShift UpdateEmployeeWorkShift(EmployeeWorkShift employeeWorkShift);methodTo delete a specific
EmployeeWorkShiftobject, it callsvoid DeleteEmployeeWorkShift(EmployeeWorkShift employeeWorkShift);method
Now in the User Interface, for retrieve/insert/update, the user wants to use some specific formats for dates and times of EmployeeWorkShift object.
One way to solve this issues, is to add 4 string properties in EmployeeWorkShift object which contains the dates and times in specific formats user desires:
using System;
namespace BusinessObjects
{
public class EmployeeWorkShift
{
public long EmployeeWorkShiftId { get; set; }
public long EmployeeId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
public string StartDateString { get; set; }
public string EndDateString { get; set; }
public string StartTimeString { get; set; }
public string EndTimeString { get; set; }
}
}
So in User Interface I don't use the original 4 properties of dates and times and instead use the new 4 string properties.
In Service method for retrieve, once I get data from Repository, I translate the original 4 properties of dates and times retrieved from database into specific formats and populate the new 4 string properties.
In Service method for insert/update, I translate the new 4 string properties into original 4 properties of dates and times before calling Repository.
This looks a crude solution to me. Is there a better way to solve this issue?
In my view, formatting Dates for display purposes is a presentation concern, not a business logic concern. I would imagine also that the formatting for dates affects all dates that the user sees, not only the dates related EmployeeWorkShift, so your approach would require extending every single entity which contains dates with the string property and applying the logic everywhere.
What I would do is have the business objects working with DateTime only both for reads and writes. Then in the presentation layer I would have a DateTime
formatterwhich would accept the DateTime and a Format parameter. The Format parameter could be retrieved from User settings or obtained from the selected Culture for example.So, you'd have the concerns separated into 3 parts:
Business logic works with DateTimes always. This will simplify the business logic layer and avoid mistakes
A single Formatter function to format any DateTime for display regardless of the business object it belongs to (you'll need a Parser function too).
A single way to retrieve the format, decoupled from all business objects and dates presented in the UI, so you can easily replace how you obtain it (combo box on the page, from the Culture in the browser or system, from user settings, etc).