none
Code First Objects and DTO objects - differences ? RRS feed

  • Question

  • Hello!

    I am looking to build a WPF app from scratch and weighing whether I should use the entity framework code first approach for the first time. My confusion is if you code the classes using Code First to generate your database in the data layer, then what becomes your data transfer objects (DTO's) if I have a WCF service layer in the middle grabbing entities, converting to DTO's to the client and then back again to entities to update the database? In other words, would I need to code both the code first objects and separately code the DTO's ?

    Sincerely and thank you,
    Harry


    • Edited by doug4jj Tuesday, March 14, 2017 7:03 PM
    Tuesday, March 14, 2017 7:02 PM

Answers

  • You do not want to expose your entities. So yes, you need to create entities and DTOs or in your case DataContracts.

    So after running an EF query, map the result to a DataContract and return that.


    william xifaras

    • Proposed as answer by William Xifaras Thursday, March 16, 2017 8:28 PM
    • Marked as answer by doug4jj Friday, March 17, 2017 2:18 PM
    Thursday, March 16, 2017 2:51 PM

All replies

  • Hi doug4jj,

    if you want to expose the database entities to the client. The client receives data that maps directly to your database tables. However, that's not always a good idea. Sometimes you want to change the shape of the data that you send to client. For example, you might want to:

    • Remove circular references.
    • Hide particular properties that clients are not supposed to view (such as password field).
    • Omit some properties in order to reduce payload size.
    • Flatten object graphs that contain nested objects, to make them more convenient for clients.
    • Avoid "over-posting" vulnerabilities. (See Model Validation for a discussion of over-posting.)
    • Decouple your service layer from your database layer.

    To accomplish this, you could define a data transfer object (DTO). A DTO is an object that defines how the data will be sent over the network.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 15, 2017 5:02 AM
    Moderator
  • Hi, Cole Wu.

    Thank you for the response and information!  So, it sounds then like I will need to code my entity objects AND DTO's regardless if they are the same or different ?  The main reason I ask is I wanted to know if this was the assumption when using the Code First approach with Entity Framework.

    Harry

    Wednesday, March 15, 2017 1:09 PM
  • You do not want to expose your entities. So yes, you need to create entities and DTOs or in your case DataContracts.

    So after running an EF query, map the result to a DataContract and return that.


    william xifaras

    • Proposed as answer by William Xifaras Thursday, March 16, 2017 8:28 PM
    • Marked as answer by doug4jj Friday, March 17, 2017 2:18 PM
    Thursday, March 16, 2017 2:51 PM
  • Yes you should create both entities and DTOs. If you are using WCF, then you will create DataContracts.

    It's not an assumption, but a recommended approach.


    william xifaras

    Thursday, March 16, 2017 5:04 PM
  • Hi doug4jj,

    I think it is a design pattern issue, if your DTO's and entity are the same, you don't need to transfer the object.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 17, 2017 7:57 AM
    Moderator
  • Thanks, William Cole!

    Friday, March 17, 2017 2:17 PM