0

I have a web service with the methods GetUser and AddUser, and some DTOs (Data Transfer Objects)

  • GetUser: retrieves user information, as a DTO called UserGet
  • DTO AddUser: takes a DTO called UserAdd, and creates a new user

both DTOs inherit from the class User, but have different additional fields. outline:

class User
{}

class UserGet : User
{}

class UserAdd : User
{}

what are better class names?

8
  • 2
    GetUserResponse and AddUserResponse – yitzih Jan 21 at 20:13
  • @yitzih i'm assuming you would mean AddUserRequest – symbiont Jan 21 at 20:36
  • i'm also considering putting the classes in a separate namespace, from the methods – symbiont Jan 21 at 20:38
  • 4
    That inheritance should be removed. A DTO and their related domain object should not inherit from one another. The entire point of the DTO is to decouple the data from its domain, and by using inheritance you're undoing that purpose entirely. This is a case where you do not create shared ancestry just because two classes happen to have some similar properties. – Flater Jan 22 at 10:25
  • Questions about naming really depend on context. For example, the act of adding a user could be seen as an “enrollment” or registration” request, if it comes from an end-user registering for the first time. But it might be called something else if it administrator is adding the user through a backend tool, or is merely adding a user to a tenant or permission group. You might also have several different actions that add a user that need to be distinguished from one another. The naming really depends on you and what is clear to you in the context of your line of business. – John Wu Jan 23 at 19:31
2

A general approach is to add "DTO" to the end of the class name, as well as place all the DTOs in their own namespace. So, considering the above, you could name them GetUserDTO and AddUserDTO respectively.

3
  • that makes sense. what is your opinion on only changing the namespace and using the same name for the classes and methods? – symbiont Jan 25 at 10:50
  • In my opinion, then the distinction between class and method wouldn't be so clear to someone reading the code. It is a matter of promoting fast comprehension and development, and having different names for classes and methods helps achieve that. – Sebastian Sotto Jan 25 at 13:29
  • i see. thank you! – symbiont Jan 25 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.