We currently use DTO for Web API request and response
and use ViewModel to pass data to the View in MVC
Currently, we have:
Only difference I see in our case is that DTO has to be passed over the wire and sent as JSON while ViewModel has to be passed inside the view to be render as HTML.
Should we mantain separate transfer objects for MVC applications and our App/other clients OR do both of these jobs through DTOs only i.e. passing DTO inside the View instead of ViewModel? What could be possible downside of it?
I read many responses here but there's no example and convincing reason to keep separate ViewModel and DTO in our case.
ViewModel in ASP.NET MVC practice is the same as the DTO, however ViewModel in MVVM pattern is different from DTO because ViewModel in MVVM has behaviors but DTO does not have.
First, it's important to remember that you don't have to use DTOs. They are a programming pattern, and your code will work just fine without them. If you want to keep the same data representation in both layers, you can just use your entities as DTOs.
DTOs (Data Transfer objects) are the objects or classes used to transfer data between layers through the service layer. The service layer only accepts data in the form of DTOs.
Difference between DTO & Entity: Entity is class mapped to table. Dto is class mapped to "view" layer mostly. What needed to store is entity & which needed to 'show' on web page is DTO.
Bob did that once. He had a web site that displayed a user's profile, with things like their name and phone number. And he also had an API call that allowed a client to retrieve the same profile with the same information. So he used the same class for the web site's ViewModel and the API's DTO.
All was well at first. In fact, the business grew so well that Bob was able to sell his stock options and retire. The software engineering department became largely outsourced and people didn't really talk to each other.
A year later, the product owner, responding to customer requests, added a requirement that the profile page display not just the user's first and last name but also the user name and email address. A developer picked up the task and implemented it by adding a Login member to the ViewModel and populating it using AutoMapper. He believed the ViewModel was server side code and didn't think about it too much.
The company pushed a production live and the user name feature is a success. There was just one problem. All the API clients were now receiving JSON that contained the user's login, email address, and everything else, including their hashed password.
Don't be Bob.
I can share my experience with this pattern as I have implemented it in a project. Consider the project as a simple CRUD application with distributed architecture and is divided into three solutions:
- Project.Web (MVC Application)
- Project.DTO (Plain C# classes)
- Project.API (Web API Application)
Project.Web contains UI logic of the application and depends upon Project.API for all the CRUD operations, since database related operations are performed in Project.API.
Now comes Project.DTO, it contains plain C# classes and is used to transfer data to and fro.
Project.Web <---> Project.API
To make it more distributed, I have created Project.DTO as a Nuget Package and hosted it in Artifactory( you can use Nuget or any other repository) and consuming it using Nuget Package Manager. The main advantage with this approach is DTO classes are always versioned and easy to consume.
Coming to your question about keeping View Model and DTO separate, I can see the following points in favor of it:
These are few points I can think of related to your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With