AutoMapper queryable extensions 只找需要的字段

http://jahav.com/blog/automapper-queryable-extensions/

How to generate a LINQ query for your DTOs

AutoMapper is a really cool library that allows us to map one object to another, e.g. when passing objects through layers of our application, where we work with different objects in different layers of our app and we have to map them from one layer to another, e.g. from business object to viewmodel.

All is good and well for POCO, not so much for entity objects. The automapper tries to map everything using reflection, so properties like Project.Code can turn to ProjectCode, but that is troublesome with ORM, where querying an object means loading another entity from the database.

I am using a NHibernate linq provider that only gets columns we actually ask from the database, so it would be nice to have a DTO type, entity type and magically create a linq expression mapping from one to another that can be used by NHibernate LINQ provider.

Remember, such expression will require only necessary fiels, so Id or Created won’t be part of SQL query (see NHibernate Linq query evaluation process for more info).

Queryable Extensions

Automapper provides a solution to this proble: queryable extensions (QE). They allow us to create such expression and they even solve SELECT N+1 problem. It is no panacea, but it solves most of my trouble.

Notice the key difference, normal automapping will traverse object graph and return a mapped object, QE will only generate a mapping expression.

Example

I will provide an example using the entities above:

  1. NuGet package for AutoMapper, the QueryableExtensions are part of the package and are in AutoMapper.QueryableExtensions namespace
  2. Create a test
  3. Create a mapping
    Mapper.CreateMap<Post, PostDto>();
  4. Query by hand (see query above)
    var postDto =     session.Query<Post>().Where(post => post.Id == id)    .Project().To<PostDto>()    .Single();
  5. Observe the generated SQL:
    select     blog1_.Name as col_0_0_,     post0_.Title as col_1_0_,     post0_.Body as col_2_0_  from     Post post0_  left outer join     Blog blog1_          on post0_.Blog=blog1_.Id  where     post0_.Id=@p0; @p0 = 1 [Type: Int32 (0)] 

    It is no different that the SQL generated by the hand made query. It only queries what is necessary without boilerplate code.

  6. Remove boilerplate code from your app.

You can also do a more difficult transformations, although QE are slightly more limited than in-memory AutoMapper capabilities, go and read the wiki.

This is really cool extension that will remove quite a lot of boilerplate code, so give it a try!

时间: 2024-08-27 19:26:10

AutoMapper queryable extensions 只找需要的字段的相关文章

aufomaper Queryable Extensions ProjectTo

When using an ORM such as NHibernate or Entity Framework with AutoMapper's standard Mapper.Map functions, you may notice that the ORM will query all the fields of all the objects within a graph when AutoMapper is attempting to map the results to a de

通过id查询用户,但是只返回指定的字段

使用hibernate和spring MVC 通过id查询到一个用户,但是只返回指定的字段 方式一: 拼接hql /*** * 通过数据库ID查询用户,但是只返回指定的字段 * @param id * @param propertyNames : 指定的多个成员变量 * @return */ public Object[] getPropertiesById(int id,String[] propertyNames){ if(ValueWidget.isNullOrEmpty(property

Hibernate高效查询,只查询部分/指定字段

公司使用 [java] view plain copy print? DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PeBulletin.class); detachedCriteria.createAlias("enumConstByFlagIsvalid", "enumConstByFlagIsvalid"); detachedCriteria.createCriteria("

drf序列化高级、自定义只读只写、序列化覆盖字段、二次封装Response、数据库查询优化(断关联)、十大接口、视图家族、自动补全图片链接

目录 自定义只读 自定义只写 序列化覆盖字段 二次封装Response 数据库关系分析 断外键关联关系 ORM操作外键关系 ORM四种关联关系 基表 序列化类其他配置(了解) 十大接口 BaseSerializer初始化方法 十大接口序列化总结 单查群查 单增群增 单删群删 单整体改/群整体改 单局部改/群局部改 群增群改配置 十大接口小结 字段提供就校验,不提供拉到 DjangoORM内置优化机制:一次最多查21条数据 models.py response.py serializers.py

DTO学习系列之AutoMapper(四)

本篇目录: Mapping Inheritance-映射继承 Queryable Extensions (LINQ)-扩展查询表达式 Configuration-配置 Conditional Mapping-条件映射 AutoMapper版本变化点 类型映射优先级 后记 关于AutoMapper写到这基本的东西都差不多了,上一篇定义为灵活配置篇,本篇可以定义为扩展应用篇,加一些补充,关于AutoMapper的项目应用,网上找了几篇英文文章,虽然看不懂,但是代码是相通的,感觉很不错,主要是Enti

AutoMapper

AutoMapper 使用实践 一.   使用意图 常常在开发过程中,碰到一个实体上的属性值,要赋值给另外一个相类似实体属性时,且属性有很多的情况.一般不利用工具的话,就要实例化被赋值实体B,然后再将实体A的字段一个个赋值给B的属性,单单写这些没有技术含量的赋值语句,就要用很大的代码篇幅.假如做得好一点的话,一般就是利用反射的方式,将A属性赋值给B,当然用反射的话,要顺利将A的属性,赋值B的属性,这样确实能够减少代码篇幅,那就要有一些约束或者限制,例如属性名称要相同,属性的数据类型要相同,这样反

AutoMapper使用

AutoMapper使用 前言:前篇搭建了下WCF的代码,就提到了DTO的概念,对于为什么要有这么一个DTO的对象,上章可能对于这点不太详尽,在此不厌其烦再来提提它的作用: 从安全上面考虑,领域Model都带有领域业务,让Client端引用Domain Model就意味着Client端可以绕过应用层直接完成业务逻辑的调用,这样是一种不安全的机制. 从对象传递效率上面考虑,领域Model带有业务,而这些业务一般对于UI层是没有意义的,所以带有业务的model传递起来会加重网络负担. 网上还说了DT

AutoMapper 下面的 Mapper

一些orm框架,在用到Entity的时候有一些开源代码用到了automapper(如:nopcommence),将数据对象转成DTO.比如在ORM中,与数据库交互用的Model模型是具有很多属性变量方法神马的.而当我们与其它系统(或系统中的其它结构)进行数据交互时,出于耦合性考虑或者安全性考虑或者性能考虑(总之就是各种考虑),我们不希望直接将这个Model模型传递给它们,这时我们会创建一个贫血模型来保存数据并传递.神马是贫血模型?贫血模型(DTO,Data Transfer Object)就是说

AutoMapper官方文档-目录

前言 对于AutoMapper很久前就接触过. 当时忙着做了一个网站,在domain和ui之间传递数据,有时候需要处理一些页面显示数据,从Entity到页面Model,参照网上的“入门”配置下就用上了. 最近又开始要使用AutoMapper,在看以前的代码,我勒个去,先不说多写了很多没用的配置,对一些稍微复杂一点的配置本来AutoMapper可以完成的,因为不会配置然后就手撸了…… 百度和博客园上找了不少入门啊,介绍啊一类的文章,发现很多是转载或者是高阶应用,对于真正系统的基础文档少而又少.Au