I am working on mvc4 application using EF code first approach. I have finished writing the models of the application and came across data transfer objects DTOs which I have never used before. The basics that I have in my mind for models is that they are taxi drivers that take data from database as passengers and drop wherever there is requirement. So, in what cases should we go for DTOs?
Share
The problem of using model as the only vehicle is that sometimes it carries too much data. For example, there may need to show all user information but not the SSN. Another concern is over-posting. For example, if your model is used in WEB API to deserialize JSON object, someone can easy stick extra info into it. And if your model will carry this info all the way to DB.
To limit these problems, you can create View Models, DTOs or both, which will limit amount of fields “visible” to the client. Your DTO hydrator will simply skip non-existing fields during hydration.
Check this auto-mapper http://www.dnrtv.com/default.aspx?showNum=155