简体   繁体   English

为什么我们需要DTO / VIewModels而不是在MVC Core中使用部分类?

[英]why do we need DTO/VIewModels rather than using partial classes in MVC Core?

I have an application in MVC CORE and in this application, i am using DTO/ViewModels. 我在MVC CORE中有一个应用程序,在此应用程序中,我正在使用DTO / ViewModels。 First I get from database and fill into the viewModels/DTOs. 首先,我从数据库获取并填写viewModels / DTO。 DTOs are more helpful because we can customize it in our own way rather than Microsoft Entity Framework pure generated poco classes. DTO更有帮助,因为我们可以用自己的方式自定义它,而不是Microsoft Entity Framework纯生成的poco类。 We can place data annotations, make more properties and can utilize them for more customization. 我们可以放置数据注释,创建更多属性,并可以利用它们进行更多自定义。

But now i am facing the performance issues from this layer/DTO/ViewModel. 但是现在我面临着来自此层/ DTO / ViewModel的性能问题。

Because For such large data, c# loops are much slowing down my application. 因为对于如此大的数据,c#循环大大降低了我的应用程序的速度。

Can't/Shouldn't i use the Partial Classes and write data annotations and write our own properties there. 我不能/应该使用Partial Classes并编写数据批注并在其中编写我们自己的属性。

What will be the issue in it ? 里面有什么问题?

Because For such large data, c# loops are much slowing down my application 因为对于如此大的数据,c#循环大大降低了我的应用程序的速度

Use restrictions in queries. 在查询中使用限制。 I dont know why you need 1,000,000 records at once. 我不知道为什么您一次需要1,000,000条记录。 if you need the 1,000,000 records do paging. 如果需要1,000,000条记录,请进行分页。 Is not the fact that you are using EF objects directly that is going to give you performance. 直接使用EF对象并不是要获得性能的事实。

You can use the partial classes in EF objects and add new properties and add them Annotations and all that, but my advice is don't use EF objects across your application. 您可以在EF对象中使用部分类,并添加新属性,并为其添加注释和所有其他内容,但我的建议是不要在整个应用程序中使用EF对象。 You loose abstraction, low coupling, your Domain model shouldn't be aware of infrastructure concerns(EF). 您松散的抽象,低耦合,您的Domain模型应该不了解基础结构问题(EF)。 Model should be focus on Model. 模型应集中在模型上。 Look at Uncle Bob, Clean Architecure https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html for some insight. 看看Bob叔叔,Clean Architecure https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html了解更多信息。 After reading you figure it out, why using EF objects could not be such a great idea. 阅读完之后,您会发现为什么使用EF对象并不是一个好主意。

This is a very vast topic to talk about. 这是一个非常广泛的话题。 Hope help you with my 5 cents 希望能帮我5美分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM