Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DTOs vs Serializing Persisted Entities

I'm curious to know what the community feels on this subject. I've recently come into the question with a NHibernate/WCF scenario(entities persisted at the service layer) and realized I may be going the wrong direction here.

My question is plainly, when using a persistent object graph(NHibernate, LINQ to SQL, etc) behind a web service(WCF in this scenario), do you prefer to send those entities over the wire? Or would you create a set of lighter DTO's(sans cyclic references) across?

like image 967
Alexis Abril Avatar asked Nov 06 '09 15:11

Alexis Abril


2 Answers

DTOs. Use AutoMapper for object-to-object mapping

like image 81
epitka Avatar answered Sep 28 '22 02:09

epitka


I've been in this scenario multiple times before and can speak from experience on both sides. Originally I was just serializing my entities and sending them as is. This worked fine from a functional standpoint but the more I looked into it the more I realized that I was sending more data than I needed to and I was losing the ability to vary the implementation on either side. In subsequent service applications I've taken to created DTOs whose only purpose is to get data to and from the web service.

Outside of any interop, having to think about all the fields that are being sent over the wire is very helpful (to me) to make sure I'm not sending data that isn't needed or worse, should not get down to the client.

As others have mentioned, AutoMapper is a great tool for entity to DTO mapping.

like image 39
akmad Avatar answered Sep 28 '22 02:09

akmad