mercredi 29 juin 2022

Business Object is duplicating some properties for Presentation Layer

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:

  1. To retrieve all EmployeeWorkShift objects of an employee, it calls List<EmployeeWorkShift> GetEmployeeWorkShifts(long employeeId); method

  2. To retrieve a specific EmployeeWorkShift object, it calls EmployeeWorkShift GetEmployeeWorkShift(long employeeWorkShiftId); method

  3. To insert a specific EmployeeWorkShift object, it calls EmployeeWorkShift InsertEmployeeWorkShift(EmployeeWorkShift employeeWorkShift); method

  4. To update a specific EmployeeWorkShift object, it calls EmployeeWorkShift UpdateEmployeeWorkShift(EmployeeWorkShift employeeWorkShift); method

  5. To delete a specific EmployeeWorkShift object, it calls void 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?

Aucun commentaire:

Enregistrer un commentaire