Use DTOs only in Responses or also in Requests?

1,902 views
Skip to first unread message

shapper

unread,
Nov 17, 2011, 7:34:22 AM11/17/11
to agath...@googlegroups.com
Hello,

I am returning DTOs inside the Responses, for example, "FindUserWithNameDto" in "FindUserWithNameResponse".

But when creating a user, CreateUserHandler, do you place the User properties directly on the request or also use a UserDto, for example, UserCreateDto?

--
 
Thank You,
Miguel

Ramon Smits

unread,
Nov 17, 2011, 9:52:22 AM11/17/11
to agath...@googlegroups.com

Personally I try to avoid  DTO's reuse. In short, I have requests/responses with their own DTO classes and often I don't even re-use a dto between similar requests/responses.

In your example I have :

public class CreateUserRequest : Request{

    public UserDto User;

    public class UserDto{
        public string Username;
        public string Email;
    }
--
Ramon

Steve Sheldon

unread,
Nov 17, 2011, 10:40:28 AM11/17/11
to agath...@googlegroups.com
I'd suggest FindUserWithNameResult rather than Dto.

I saw this pattern in the Amazon AWS SDK .NET services and I like it.

I'd also agree with Ramon in that it's best to have sub objects specific to a request/response.  Although there may be some global value objects like say an Address that might be shared, there won't be too many like that.  I wouldn't say User is, because there are so many different ways to represent that.

shapper

unread,
Nov 18, 2011, 5:11:55 PM11/18/11
to agath...@googlegroups.com
Ramon,

Sorry for the delay.
I was trying that approach on my code and I really like it!

It solves most of the problems. I have a few questions:

1 - On CreateUserResponse I return User's ID after being created.
     Would you include just this property on the Response?
     Or you would also add a UserDto with only the property ID?

2 - The same for DeleteUserRequest. I pass only the ID.
     Would you also create a model User with one property ID?

I am curious how you do it.

--
 
Thank You,
Miguel
     

shapper

unread,
Nov 18, 2011, 5:20:15 PM11/18/11
to agath...@googlegroups.com
Hello Steve,

I downloaded and installed the Amazon AWS SDK .NET.
It has a few things that is nicer to follow.

In this case I decide to define user as follows:


public class UserDto{
  public string Username;
  public string Email;
  public string[] Roles;
}

I am sending only the role names.
I think in sometimes I might have more DTOs inside the request.

For example:


public class CreateUserRequest : Request{

    public UserDto User;

    public class UserDto{
      public string Username;
      public string Email;
      public string[] Roles;
      public PaperDTO Paper;
      public AddressDTO Address;
    }

    public class PaperDto{
      public string Title;
      public byte[] Data;
      public bool Published;
    }

}

public class AddressDto {
  public string Sreet;
  public string PostalCode;
  public string City;
  public string Country;
}


So UserDto and PaperDto are in the request.

But AddressDto is global so used in many requests.

I think this makes sense. If I am missing something just let me know.

--
 
Thank You,
Miguel

Reply all
Reply to author
Forward
0 new messages