MVC Series Learning (ix) use of-dto

Source: Internet
Author: User

The database used for this study is as follows

1. What is Dto:datatransferobject

That is, the data transfer object, the client communication on the server, automatically defines a small entity class that contains only the attributes we need to transfer

2. What's wrong with a DTO?

A. When using JavaScriptSerializer, an error will be made.

Cause: The teacher entity has a foreign key entity Teachclass, and Teachclass has a teacher collection property, and then it loops the query, and there is a dead loop

B. Error when using JSON to return data

3. Using DTOs to solve problems

Now the question:

When serializing the teacher object, the foreign key entity Teachclass is found, and when the Teachclass object is found, the teacher is re-searched until the loop.

Start the solution:

A. Defining a DTO class to resolve a dead loop

We can define a new Teachclass class that does not contain properties that cause a dead loop, that is, no icollection<teacher> property. Here we also re-declare a class for teacher.

B. Defining partial classes for easy addition of additional methods

The death cycle problem is solved, this is the DTO, simple. Next, we may want to use serialization in many places, so we define a method in the inside of the class and serialize ourselves for convenience. But again, because the entity class is generated by EF, each time we click Save, EF initializes the class according to the TT template and the XML configuration information, and the method we wrote is written in White. This time, we think of a partial class, a method that serializes the class in a partial class.

There is an idea because EF is looking for a class by file name, so the class name here cannot be the same as the class name

C. Start using your own defined DTO class

found that the download program is working properly, did not report any exceptions, done

MVC Series Learning (ix) use of-dto

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.