Hi everyone.
I want create simple eCommerce app using ABP Core 2 and EF. Before start coding I need some tips about my project data architecture.
eCommerce apps needs to much related data so I think that need to many lines hard code. In ABP using ViewModels together DTO is possible? or Where I can find any documents about using ABP for enterprise or big data projects?
Hi @CS-beglnner,
While it is possible to use DTO and View Models as the same class (either directly or via inheritance) it is not advisable as you are ultimately leaking information from your domain layer to your presentation layer. While it may seem tedious to have DTO's and ViewModels in the long run it makes a lot of sense as you keep things separate and can then have different validations for view models vs DTO's, caching of the domain layer etc. etc.
Have a read of https://aspnetboilerplate.com/Pages/Documents/Data-Transfer-Objects#abstraction-of-the-domain-layer which goes into more detail on the subject. You should also check out object to object mapping which shows how easy it is to transfer values between objects using something like AutoMapper.
If you really insist on tightly coupling your view models to your DTO's then your ViewModels could be a sub class of the DTO. However I would strongly advise against it.
Hi,
@natiki, DTOs are not a part of the domain. So, it's actually not leaking the domain to the presentation. DTOs already makes this abstraction.
@CS-beglnner, as like any good answer: it depends. For me;
There is nothing wrong to use DTOs in your views (as model). But do it only if no change required (I mean if you use the DTO class as is).
In pratical, you will generally want to add view-specific properties or methods to the model you are using in the view. In such cases, never modify the DTO for your view requirements. Instead, create a viewmodel and map from your DTO to the viewmodel.
@hikalkan Very thanks for clear answer
Most helpful comment
Hi,
@natiki, DTOs are not a part of the domain. So, it's actually not leaking the domain to the presentation. DTOs already makes this abstraction.
@CS-beglnner, as like any good answer: it depends. For me;
There is nothing wrong to use DTOs in your views (as model). But do it only if no change required (I mean if you use the DTO class as is).
In pratical, you will generally want to add view-specific properties or methods to the model you are using in the view. In such cases, never modify the DTO for your view requirements. Instead, create a viewmodel and map from your DTO to the viewmodel.