Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I map a DTO to/from a domain entity on both client and server sides?
    primarykey
    data
    text
    <p>I've got a rich domain model, where most classes have some behaviour and some properties that are either calculated or expose the properties of member objects (which is to say that the values of these properties are never persisted).</p> <p>My client speaks to the server only via WCF.</p> <p>As such, for each domain entity, I have a corresponding DTO -- a simple representation that contains only data -- as well as a mapper class that implements <code>DtoMapper&lt;DTO,Entity&gt;</code> and can convert an entity to its DTO equivalent or vice-versa through a static gateway: </p> <pre><code>var employee = Map&lt;Employee&gt;.from_dto&lt;EmployeeDto&gt;(); </code></pre> <p>The server side of this application is mostly about persistence, where my DTOs come in from the WCF service, are deserialized, and then an arbitrary ORM persists them to the database, or a query request comes in from WCF and the ORM executes that query against the DB and returns objects to be serialized and sent back by WCF.</p> <p>Given this scenario, <strong>does it make any sense to map my persistence store to the domain entities, or should I just map directly to the DTOs?</strong></p> <p>If I use domain entities, the flow would be </p> <ol> <li>client requests object</li> <li>WCF transmits request to server</li> <li>ORM queries database and returns domain entities</li> <li>domain entities transformed into DTOs by mapper</li> <li>WCF serializes DTO and returns to client</li> <li>client deserializes DTO</li> <li>DTO transformed into domain entity by mapper</li> <li>viewmodels created, <em>etc.</em></li> </ol> <p>similar on the return trip</p> <p><strong>If I map straight to DTO, I can eliminate one mapping per object, per request. What do I lose by doing this?</strong></p> <p>The only thing that comes to mind is another opportunity to validate before insert/update, because I have no guarantee that the DTO was ever subject to validation or even existed as a domain entity before being sent across the wire, and I guess a chance to validate on select (if another process might have put invalid values in the database). Are there other reasons? Are these reasons sufficient to warrant the additional mapping steps?</p> <h3>edit:</h3> <p>I did say "arbitrary ORM" above, and I do want things to be as ORM-and-persistence-agnostic as possible, but if you have anything special to add that is specific to NHibernate, by all means do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload