Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslating Entity Framework model navigation properties into DTOs
    primarykey
    data
    text
    <p>I’m currently working on an n-tier web project. After researching into Data Transfer Objects and their benefits we decided to give this pattern a go. Our ASP.NET MVC website does not have direct access to the EF DbContext but instead will use DTOs to send and receive entity data. There will be a service/mapping layer that will convert between DTOs and entity models.</p> <p>My question is, what is the best way to translate entity model navigation properties into its DTO?</p> <p>Below is an example of a entity model and its DTO from the project:</p> <p><strong>Entity Model:</strong></p> <pre><code>public class Payment { public int ID { get; set; } public DateTime? PaidOn { get; set; } public decimal Amount { get; set; } public string Reference { get; set; } //Navigation Properties public virtual PaymentMechanism PaymentMechanism { get; set; } public virtual ICollection&lt;Order&gt; Orders { get; set; } } </code></pre> <p><strong>DTO:</strong></p> <pre><code>public class PaymentDto { public int ID { get; set; } public DateTime? PaidOn { get; set; } public decimal Amount { get; set; } public string Reference { get; set; } //--------Navigation Properties - Object Ids-------- public int PaymentMechanismId { get; set; } public ICollection&lt;int&gt; OrderIds { get; set; } } </code></pre> <p>As can be seen they are very similar except for the navigation properties. I have changed them to hold integer Ids (of the entities) instead of the entity models. Therefore if the navigation property entities need to be obtained, their Id’s can passed into a service/mapping layer function which will retrieve the entities from then database, map them to DTOs and return the collection. Is this an acceptable way of doing things? </p> <p>I am new to this area so some of my terminology might not be totally correct but hopefully you’ll understand what I'm getting at. If you need me to clarify or provide additional detail on anything, please let me know.</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.
    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