My project is layered as follows:-
DAL (Entity) --> BLL (DTO) --> ApplicationComponent (ViewModel).
There will be multiple components of application (ApplicationComponent) which will access BLL. Components include windows services, web services, web API and MVC controller.
I am transforming NHibernate Entity objects to DTO objects while passing them from DAL to BLL. While passing this state to ApplicationComponent, BLL again converts it to ViewModel.
This helps me separate the concerns and how data is handled in each layer. I am not in favor of returning NHibernate Entity object to view for following reasons: -
- Data get exposed to
UIthat I want to hide (or only expose if needed) like passwords, user type, permission etc. - On references/joins,
NHibernateexecutes additional queries when property is accessed which nullify the use of lazy loading. - Unnecessary data exposed to user (of
Entity) creates confusion and gap for bugs. - Persistence implementations leaking into
BLL/UI.Entityis not designed forUI. It cannot serveUIin all cases. - We use attributes on
DTOproperties for user input validation which looks odd withEntity.
I am facing following problems with this approach: -
- Biggest and obvious problem is redundant objects with similar members and functionality.
- I have to write mapper methods in each layer to transform object. This could be minimized by using
AutoMapperor something similar; but it does not fully resolve problem.
Questions:-
- Is this an over separation and should be avoided (at least minimized)?
- If this approach is correct, I do not see any simple way to fully bypass two problems I stated above. Please suggest.
- If this approach is incorrect, please suggest corrections.
References:-
- Link1 suggests to transfer
Entityobject to view which in my understanding not a good idea. - Link2 suggests to map
EntitywithDTOthat I am already doing. - Link3 does not help.
- Link4 suggests using something like auto mapper tools which is OK. But it still does not solve the problem completely.
- Link5 is great post. It explains why those should be separate which I agree. It does not comment on how to minimize the overhead caused by it.
- Link6 is not helpful again.
- Link7 is an excellent answer which suggests use
Entityas is inUIif possible. It still does not apply to most of my project. - Linl8 is another excellent resource that suggest to go on mapping two way as I am doing now. It still does not suggest a way to minimize overhead.
