且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DTO模式+延迟加载+实体框架+ ASP.Net MVC +自动映射

更新时间:2022-10-16 10:27:17

首先不使用延迟加载作为可能会导致在选择N + 1的问题或类似的。


  

选择N + 1是一个数据存取反模式,其中该数据库是
  在次优的方式进行访问。


块引用>

在换句话说,使用延迟加载无需预先加载的集合导致实体框架去到数据库,并在同一时间把结果返回一行。

使用jsRender为模板,因为它比JQuery的模板要快得多:
渲染Bemchmark
这里的如何使用它的一个很好的信息:Reducing JavaScript的code在HTML5应用程序中使用jsRender模板


  

一般情况下,我们有正常和精简版的DTO域,如客户,
  CustomerLite等(精简版的对象具有比普通几个属性)。


块引用>

您正常DTO可能是一个ViewModel作为的ViewModels可能会或可能不会映射一来一往的DTO,和的ViewModels通常包含的逻辑推从视图回来,或帮助与将数据推回模型对用户的响应。
DTO的没有任何行为和它们的目的是为了降低应用层之间的呼叫的数量。


  

有没有同时考虑可维护性,我们可以利用提高性能的机制?


块引用>

使用一个视图模型的一个视图,你会不会担心可维护性。我个人通常创建一个abtract类是基础,并为编辑,创建或列表我继承类,并添加了特定的视图属性。因此,对于一个例子创建视图并不需要属性ID(有人可以劫持您的文章,并张贴),因此只编辑和列表的ViewModels有属性ID属性公开。


  

是否有可能使用Automapper更多的地图视图基于映射功能相同的DTO?


块引用>

您可以使用AutoMapper来定义每个地图,但问题是,地图上会多么复杂。采用按次和你会将一个视图模型将是简单的编写和维护。我必须指出,我们不建议在数据访问code作为使用Automapper:


  

AutoMapper的一个缺点是从域对象投影
  还迫使整个域对象进行查询和下载。


块引用>

来源: Autoprojecting LINQ查询

您可以使用一组扩展被限制,截至目前,以加快数据访问code你的映射:的停在你的数据访问code 使用AutoMapper

问候

Firstly, Sorry For lengthy question but I have to give some underlying information.

We are creating an Application which uses ASP.net MVC, JQuery Templates, Entity Framework, WCF and we used POCO as our domain layer. In our application, there is a WCF Services Layer to exchange data with ASP.net MVC application and it uses Data Transfer Objects (DTOs) from WCF to MVC.

Furthermore, the application uses Lazy Loading in Entity Framework by using AutoMapper when converting Domain-TO-DTOs in our WCF Service Layer.

Our Backend Architecture as follows (WCF Services -> Managers -> Repository -> Entity Framework(POCO))

In our application, we don't use View Models as we don't want another mapping layer for MVC application and we use only DTOs as View Models.

Generally, we have Normal and Lite DTOs for domain such as Customer, CustomerLite, etc (Lite object is having few properties than Normal).

Now we are having some difficulties with DTOs because our DTO structure is becoming more complex and when we think maintainability (with general Hierarchical structure of DTOs) we lose Performance.

For example,

We have Customer View page and our DTO hierarchy as follows

 public class CustomerViewDetailsDTO
 {
   public CustomerLiteDto Customer{get;set;}
   public OrderLiteDto Order{get;set;}
   public AddressLiteDto Address{get;set;}
 }

In this case we don't want some fields of OrderLiteDto for this view. But some other view needs that fields, so in order to facilitates that it we use that structure.

When it comes to Auto Mapping, we map CustomerViewDetailsDTO and we will get additional data (which is not required for particular view) from Lazy Loading (Entity Framework).

My Questions:

  1. Is there any mechanism that we can use for improving performance while considering the maintainability?

  2. Is it possible to use Automapper with more map view based mapping functions for same DTO ?

First of don't use Lazy Loading as probably it will result in Select N+1 problems or similar.

Select N + 1 is a data access anti-pattern where the database is accessed in a suboptimal way.

In other words, using lazy loading without eager loading collections causes Entity Framework to go to the database and bring the results back one row at a time.

Use jsRender for templates as it's much faster than JQuery Templates: Render Bemchmark and here's an nice info for how to use it: Reducing JavaScript Code Using jsRender Templates in HTML5 Applications

Generally, we have Normal and Lite DTOs for domain such as Customer, CustomerLite, etc (Lite object is having few properties than Normal).

Your normal DTO is probably a ViewModel as ViewModels may or may not map one to one to DTOs, and ViewModels often contain logic pushed back from the view, or help out with pushing data back to the Model on a user's response. DTOs don't have any behavior and their purpose is to reduce the number of calls between tiers of the application.

Is there any mechanism that we can use for improving performance while considering the maintainability?

Use one ViewModel for one view and you won't have to worry about maintainability. Personally i usually create an abtract class that is a base, and for edit,create or list i inherit that class and add properties that are specific for a view. So for an example Create view doesn't need PropertyId (as someone can hijack your post and post it) so only Edit and List ViewModels have PropertyId property exposed.

Is it possible to use Automapper with more map view based mapping functions for same DTO ?

You can use AutoMapper to define every map, but the question is, how complicated the map will be. Using one ViewModel per view and your maps will be simple to write and maintain. I must point out that it is not recommended to use Automapper in the data access code as:

One downside of AutoMapper is that projection from domain objects still forces the entire domain object to be queried and loaded.

Source: Autoprojecting LINQ queries

You can use a set of extension that are limited as of now to speed up your mapping in data access code: Stop using AutoMapper in your Data Access Code

Regards