的DTO和IQueryable的:组装和拆卸的DTODTO、IQueryable

2023-09-03 17:47:59 作者:情深当斩.

我工作的,它使用的汇编方式组装LinqToEntity实体到数据传输的服务级别,然后再向下传递到客户端层使用的对象的项目。该方法已经翻译entitie物体进入简化,提供特定于服务呼叫信息平面物体

I’m working on a project which uses an Assembler pattern to assemble LinqToEntity entities into Data Transfer Objects at a service level, which are then passed down to the client layer for use. The approach has been to translate the entitie objects into simplified, flat objects that provide information specific to the service call.

例如:

// Original Entity looks something like this
public class PersonEntity
{
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int MotherId { get; set; }  
   public PersonEntity Mother { get; set; }
   // lots of other properties
}

// Flattened DTO
public class PersonSummaryInfo
{
  public int Id { get; set; }
  public string FullName { get; set; }
  public string MothersFullName { get; set; }
}

在本实施例中,汇编器将创建一个PersonSummaryInfo,构建过程的FullNames部分

In this example, the assembler would create a PersonSummaryInfo, constructing the FullNames part of the process.

我现在面临的一些第三方控制(的Telerik ASP.NET MVC GridControl),其中控制设置(使用IQueryable的)的基础上它的模型的属性来过滤的问题。有想法似乎是你有一个单层的设计和泵直接将数据库的企业合并形成的观点,这是我无法忍受。

I now face an issue with some 3rd party control (Telerik ASP.NET MVC GridControl), where the control is set up to filter (using IQueryable) based on it’s Model’s properties. There idea seems to be that you have a single tier design and pump your database entities directly into the view, which I can’t stand.

试图将其纳入我的逻辑,GridControl绑定到我的DTO,而不是实体这是所有好,直到它试图整理东西。我把所有的IQueryable的东西进入我的服务在一个非常通用FASION使其reponsible这一点。排序企图排序,说MothersFullName的DTO(其行为是通过MothersFullName作为一个字符串到你的排序逻辑),这被推到了我的服务,通过反射尝试将实体进行排序,使得IQueryable的懒人使用装载,但在执行查询时当然会抛出一个异常的MothersFullName已经不是原来的实体的属性。

Trying to incorporate it into my logic, the GridControl binds to my DTO instead of the Entity which is all good until it tries to sort anything. I pushed all of the IQueryable stuff into my service in a very Generic fasion to make it reponsible for this. Sorting attempts to sort by, say MothersFullName on the DTO (its behaviour is to pass "MothersFullName" as a string to your sorting logic), this gets pushed up to my service which through reflection attempts to sort the Entities, making use of IQueryable lazy loading, but of course when the query is executed, an Exception is thrown as "MothersFullName" is not a property of the original Entity.

是否有处理这种实现一个好的策略?它是很好的做法,有效地拆解一个DTO回一旦其应用程序的服务层后面的ORM实体?或者是它更好地传递下去有什么,他们更多的知识更丰富的对象(如如何排序使用名字和姓氏全名)?

Is there a good strategy that handles this sort of implementation? Is it good practice to effectively "disassemble" a DTO back to its ORM entity once its back in the Service layer of an application? Or is it better to pass down richer objects that have more knowledge of what they are (such as how to sort a full name using FirstName and LastName)?

什么是关键,我的要求是:

What is key to my requirements is:

使用花哨的控制的Telerik因为他们喜欢它 在服务水平延迟加载的结果(即不带靠背2万条记录,只显示10) 支持过滤,分页等 在一个健全的体系结构实现

推荐答案

去与此解决方案。

创建包含网格特定的列SQL视图。然后做数据传输对象使用属性完全匹配的视野。不干净或实现这一坚决的方式,但至少它得到我的数据引用了我的项目并不需要它。

Create sql Views containing the Grid specific columns. Then made data transfer objects use properties that exactly match those of the View. Not the cleanest or strongest way to achieve this, but at least it gets my Data references out of my projects that don't need it.

没有使用动态LINQ,而不是一直在我的通用方法,并使用反射来获得列名进行匹配。没有去Telerik的开放存取只是因为我们已经有了一个整体的服务层实现,但它听起来像一个很好的解决方案。

Did not use dynamic linq, instead kept my generic approach and used reflection to get column names for matching. Did not go for Telerik's Open Access just because we already have a whole service layer implemented, but it sounds like a nice solution.

这一切都还是IQueryable的,所以有效的它工作得非常好(只依赖于开发人员,以确保GridView的匹配GridViewModels,财产属性)。

It's all still IQueryable, so efficiently it works very very well (just relies on developers to ensure the GridViews match the GridViewModels, property for property).

 
精彩推荐
图片推荐