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!

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

  1. 16.AutoMapper 之可查询扩展(Queryable Extensions)

    https://www.jianshu.com/p/4b23e94a7825 可查询扩展(Queryable Extensions) 当在像NHibernate或者Entity Framework之类 ...

  2. aufomaper Queryable Extensions ProjectTo

    When using an ORM such as NHibernate or Entity Framework with AutoMapper's standard Mapper.Map funct ...

  3. thinkphp 找数据库某个字段为空的数据,PHP 数据调取 空数据

    $arr['dingwei'] = array('EXP','is null');

  4. EF只更新变化的字段

    摘要 在使用EF的时候,由于表字段较多,所以在更新的时候,想要只更新变化的字段,有没有办法呢? 解决办法 代码片段 public async Task<int> UpdateAsync(T ...

  5. 转 A 、B两张表,找出ID字段中,存在A表,但是不存在B表的数据

    A.B两张表,找出ID字段中,存在A表,但是不存在B表的数据,A表总共13W数据,去重后大约3万条数据,B表有2W条数据,且B表的ID有索引. 方法一 使用not in,容易理解,效率低. selec ...

  6. PDF.NET+EasyUI实现只更新修改的字段

    PDF.NET 在我看来是目前最简单易用而且高效的orm框架之一,感谢作者深蓝医生 实现的功能是easyui的行内编辑,用到了爱看书不识字的datagrid仿extjs的行内编辑 都是牛人啊. 201 ...

  7. asp.net mvc 不找其他view模板,只找cshtml

    asp.net mvc 默认找view文件时,依次找后辍名为aspx.ascx.cshtml.vbhtml的view文件.但是项目住住用C#+Razor开发,这样找,岂不有性能损失. 添加以下代码: ...

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

    公司使用 DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PeBulletin.class); detachedCriter ...

  9. 如何只更新datetime类型字段中的日期

    UPDATE  [dbo].[Order]       SET     CreateDate = STUFF(CONVERT(VARCHAR(50),CreateDate,126) ,1, 10, ' ...

随机推荐

  1. 【发包工具】http多线程发包工具

    [发包工具]http多线程发包工具 使用方法:输入地址,发送的内容,线程数,等待时间,每个线程发送的次数,GET/POST请求. 源代码 package com.xmxkkk.httptest; im ...

  2. NetLimiter网速测试小坑

    在涉及到网络下载或者上传时,需要对各种不同的网络环境进行模拟验证,这时就需要一种可以随意限制指定进程网速的软件,经过同事推荐,发现NetLimiter这款软件很不错,界面直观,可任意设置上传下载速度, ...

  3. GridView通过RowDataBound事件获取字段值、数据源列值

    如果数据源有某一列,而在绑定到GridView时不显示该列,编程时要用到该列的值可以用以下方法: protected void GridView1_RowDataBound(object sender ...

  4. 【WP8】富文本功能实现

    2014年8月1日更新:修复如果有多个相同链接解析失败的Bug,谢谢 @Walsh 提供的问题 富文本在移动APP上应用的最多的就是表情了,类似微博,QQ,微信都有对提供对表情和链接的支持,富文本一般 ...

  5. node配置自动监测文件改变不重启

    方法一: nodemon npm install -g nodemon nodemon ./bin/www 或者在npm start命令里把node改为nodemon 方法二:supervisor n ...

  6. 关系型数据库 和 非关系型数据对比 以及 MySQL与Oracle对比

    一.关系型数据库 关系型数据库,是指采用了关系模型来组织数据的数据库.    关系模型1970年提出的,关系模型的概念得到了充分的发展并逐渐成为主流数据库结构的主流模型.    简单来说,关系模型指的 ...

  7. python操作mysql数据库实现增删改查

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: ...

  8. org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'parentId' in 'class java.lang.String'

    Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' ...

  9. 关于openssl的编译与使用

    关于openssl的编译与使用,可以参考这两往篇文章 http://blog.csdn.net/lazyclough/article/details/7456131 http://www.leaves ...

  10. javascript 以“年-月-日 时:分:秒”格式显示当前时间

    运行代码 /** * Created by shgbit on 2015/1/9. *js代码 */ function showNow(){ var t=new Date();    var mont ...