Do I need to split a large DTO?

class, dto, inheritance, ooad

Solution

There is a natural reluctance to consider a single DTO with 30 attributes, but it's by no means a "wrong" choice just for that reason. Think of a photograph file which has many "tags": camera type, lens type, aperture, mode, size, etc. There are dozens of these, and it's entirely OK to have them all in a PhotoDto.

Only split your DTO if there's good real design reasons to do so. Size is not enough and the split could introduce different difficulties.

Edit: it can be helpful to consider downstream usage too, e.g. if a DTO's attributes are all to be held in a single DB table, then it can be additionally advantageous to retaining that 1 DTO -> 1 DB table structure, both conceptually and in practical terms (ORM config, etc).

Problem

I have a DTO with 30 attributes, some attributes would be added to it. Many other classes use this DTO, some classes use 10 to 20 attributes, some other class uses all 30 attributes. In one class, can I create a DTO which uses 10 attributes and rest 20 attributes will be null? In this scenario, is it a good approach to split DTO into 2-3 DTOs by inheritance or some other way?

Original source