3

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?

satyanarayana
  • 265
  • 4
  • 14
  • Btw I see you are new to Stack Overflow and have asked a few questions already. Welcome to the site. If you feel an answer covers your question fully and satisfactorily it's custom to "Accept" it and/or upvote it, to reward the people who've contributed useful answers. – Brian May 28 '14 at 10:16

1 Answers1

5

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).

Brian
  • 6,391
  • 3
  • 33
  • 49
  • Thanks for your quick reply. In one class, can I create a DTO which uses 10 attributes so that rest 20 attributes will be null? – satyanarayana May 28 '14 at 09:45
  • Absolutely. As long as your attributes do conceptually belong in a single class, that's fine. You certainly won't have any technical barriers to doing so. But, although I do assure you it's OK to do this, please listen to my caveat carefully, e.g. don't do something like handle all a web site's forms in a single FormDataDto or something :-) – Brian May 28 '14 at 09:53
  • Consider a case for registering/updating user profile. In my DB, 3 tables contains details of User like SIGN_ON, PROFILE, ADDRESS. When a new user registers, I need to insert data into these 3 tables. Can I create one DTO for this or 3 DTO for each table? If required, I need to update only address or only profile. How many DTOs are required for this? – satyanarayana May 28 '14 at 10:34
  • Generally, Stack Overflow encourages one question at a time, rather than asking new ones in comments. In this case, I'd be inclined to align my DTOs with the DB: a User object which perhaps consists of 3 sub types, Profile, SignOn and Address – Brian May 28 '14 at 10:45
  • But be aware this isn't about right and wrong, it's a style and a judgement choice. Often in coding, you will only learn what works "best" by trying other approaches. So good for you for trying to find out what's best but take time to try your own ideas and see why *you* think they are good or bad – Brian May 28 '14 at 10:46