DTOs: bookkeeping information.

48 views
Skip to first unread message

tpdi

unread,
Jan 3, 2012, 5:51:06 PM1/3/12
to ServiceStack .NET Open Source REST Web Services Framework
So I have a service, for example, that registers (creates) a user. I
want my service to get the user properties, validate them, and persist
them. In my business layer I have a User class, with properties Name,
Password, Email.

In my service, I want to collect those three properties, plus an
additional ConfirmPassword property. I won't persist this, I'll just
check in my validation that Password == ConfirmPassword.

Should I just wrap:

public class UserDTO {
public User user {get; set; }
public string ConfirmPassword { get; set ;}
// other non-persisted book-keeping info
}

or is there a better idiom to use?

Thanks,
Tom

Demis Bellot

unread,
Jan 3, 2012, 6:05:19 PM1/3/12
to servic...@googlegroups.com
If your DTO's doesn't match your data model which is a frequent occurrence than I prefer to have 2 separate models, i.e. 1 for DTO and 1 for Data/Domain model.
ServiceStack has inbuilt support for mapping between POCOs and I have an example of how I like to do this here:


P.S. As ServiceStack is DTO/model-first it really pains me to see 'DTO' suffixes to what I think is the most important models in your services :)
I instead like to name it to how I want the outside world to see it, i.e. Drop the 'DTO' suffix. For times when you have collisions, e.g. DTO and Data models with the same name, I just use a 'using namespace alias' to distinguish between the 2, i.e.

using Dto=MyServiceModels.Types;

then you can do stuff like:

var user = dbUser.TranslateTo<Dto.User>();

etc. 

Hope this helps!

Cheers,
Reply all
Reply to author
Forward
0 new messages