Search code examples
c#dtodata-transfer-objects

Design data transfer object depending on ui logic


I am in a situation with data transfer objects where I start thinking I am over-architecting or complicating things...

I dunno wether I am right or wrong.

All the properties down in the EditSchoolyearDTO are only needed when WeekType AB is selected in the client side UI.

When only WeekType A is selected then the A-properties are enough.

In the AB-Case I would create now a SchoolWeekADTO and SchoolWeekBDTO.

Well when I consider the DTO structure as json interface between my client and server then my DTO structure does not express when the A or AB data is needed. This feels bad for me.

Above all my server api has a fixed type so I can not at one time send a post with a A-DTO and at another time a AB-DTO.

What would you do?

public class EditSchoolyearDTO
    {
        public string Name { get; set; }
        public DateTime EndDate { get; set; }
        public WeekType StartWeek { get; set; }
        public enum WeekType : int
        {
            A = 0,
            AB = 1,
        }
        public int UserId { get; set; }

        public SchoolWeekADTO SchoolWeekA { get; set; }
        public SchoolWeekBDTO SchoolWeekB { get; set; }

        // When WeektType is A then only SchoolWeekA is or its A-properties are needed
        // SchoolyWeekADTO
        //public int MaxPeriodPerWeekA { get; set; }
        //public IEnumerable<int> VisibleWeekDayIndexA;
        //public DayOfWeek FirstDayOfWeekA { get; set; }

        // SchoolWeekBDTO
        //public int WeeklyRotation { get; set; }
        //public DayOfWeek FirstDayOfWeekB { get; set; }
        //public IEnumerable<int> VisibleWeekDayIndexB;
        //public int MaxPeriodPerWeekB { get; set; }

    }

Solution

  • In order to make it not necessary to send all objects and also mark the intention of the client better, you could separate the properties for type A and B into separate classes. This way you always select one of the values of the enum to say if the type is A or B. Base on that you create an instance of one of WeekTypeADTO and WeekTypeBDTO and set the other to null.

    The code also looks cleaner and shows your intention better.

    public class EditSchoolyearDTO
    {
        public string Name { get; set; }
        public DateTime EndDate { get; set; }
        public WeekType StartWeek { get; set; }
        public enum WeekType : int
        {
            A = 0,
            AB = 1,
        }
        public int UserId { get; set; }
    
        public WeekTypeADTO SchoolWeekA { get; set; }
        public WeekTypeBDTO SchoolWeekB { get; set; }
    
    }
    
    public class WeekTypeADTO
    {
        public int MaxPeriodPerWeekA { get; set; }
        public IEnumerable<int> VisibleWeekDayIndexA {get; set;}
        public DayOfWeek FirstDayOfWeekA { get; set; }
    }
    
    public class WeekTypeBDTO
    {
        public int WeeklyRotation { get; set; }
        public DayOfWeek FirstDayOfWeekB { get; set; }
        public IEnumerable<int> VisibleWeekDayIndexB {get; set;}
        public int MaxPeriodPerWeekB { get; set; }
    }