3

In the context of ASP.Net MVC 2.0, can anybody please explain why do we need to use DTO (Data Transfer Object) if there can already be Models? I have seen an example where a web service returns DTO to asp.net and then it is converted to Model using some factory class. This web service talks to database and returns data in the form of DTO.

In my previous projects, I used to communicate to DB using Data context and repository, which used to return model object to my controller. Then I used to pass this model to the corresponding view. Isn't this simpler? I cannot find out the exact use of DTO pattren.

1 Answer 1

5

Models represent the logical data model that your views are coded against. This may or may not map 1:1 with the source(s) of the data. In a situation where Model == DTO, I agree, the DTO is somewhat redundant.

In most situations where I've used MVC, it's been pretty rare to have a single source of data, or lack the desire to separate the logical view from the physical sources. For example I often make multiple service and database calls to build single logical model.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation! So in that case, when DTO is created from multiple sources, we can use this dto itself to bind our view? Or we just need to copy 1:1 data to our model for this purpose?
We found a few of clear patterns emerged - composite model structures with fields pointing to DTO instances, composite model pointing to wrappers around DTOs e.g. converting arrays to dictionaries, and complex factories that took multiple DTOs to produce objects to put into a model. We thought of the model as the rich programming API we wanted to expose and document for view developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.