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:
- NuGet package for AutoMapper, the
QueryableExtensionsare part of the package and are inAutoMapper.QueryableExtensionsnamespace - Create a test
- Create a mapping
Mapper.CreateMap<Post, PostDto>(); - Query by hand (see query above)
var postDto =
session.Query<Post>().Where(post => post.Id == id)
.Project().To<PostDto>()
.Single(); - 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.
- 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 只找需要的字段的更多相关文章
- 16.AutoMapper 之可查询扩展(Queryable Extensions)
https://www.jianshu.com/p/4b23e94a7825 可查询扩展(Queryable Extensions) 当在像NHibernate或者Entity Framework之类 ...
- aufomaper Queryable Extensions ProjectTo
When using an ORM such as NHibernate or Entity Framework with AutoMapper's standard Mapper.Map funct ...
- thinkphp 找数据库某个字段为空的数据,PHP 数据调取 空数据
$arr['dingwei'] = array('EXP','is null');
- EF只更新变化的字段
摘要 在使用EF的时候,由于表字段较多,所以在更新的时候,想要只更新变化的字段,有没有办法呢? 解决办法 代码片段 public async Task<int> UpdateAsync(T ...
- 转 A 、B两张表,找出ID字段中,存在A表,但是不存在B表的数据
A.B两张表,找出ID字段中,存在A表,但是不存在B表的数据,A表总共13W数据,去重后大约3万条数据,B表有2W条数据,且B表的ID有索引. 方法一 使用not in,容易理解,效率低. selec ...
- PDF.NET+EasyUI实现只更新修改的字段
PDF.NET 在我看来是目前最简单易用而且高效的orm框架之一,感谢作者深蓝医生 实现的功能是easyui的行内编辑,用到了爱看书不识字的datagrid仿extjs的行内编辑 都是牛人啊. 201 ...
- asp.net mvc 不找其他view模板,只找cshtml
asp.net mvc 默认找view文件时,依次找后辍名为aspx.ascx.cshtml.vbhtml的view文件.但是项目住住用C#+Razor开发,这样找,岂不有性能损失. 添加以下代码: ...
- Hibernate高效查询,只查询部分/指定字段
公司使用 DetachedCriteria detachedCriteria = DetachedCriteria.forClass(PeBulletin.class); detachedCriter ...
- 如何只更新datetime类型字段中的日期
UPDATE [dbo].[Order] SET CreateDate = STUFF(CONVERT(VARCHAR(50),CreateDate,126) ,1, 10, ' ...
随机推荐
- WPF TextBox属性IsReadOnlyCaretVisible
纠结了半天WPF下只读的TextBox怎么显示输入焦点提示,发现wpf 4中已有新属性“IsReadOnlyCaretVisible”,大善^_^
- pytest集成Allure Report
https://blog.csdn.net/liuchunming033/article/details/79624474#commentBox https://blog.csdn.net/lihua ...
- Android:AS与Unity3D之间打包的各种坑及解决方案
作者:DrkCore (http://blog.csdn.net/DrkCore) 原文链接:(http://blog.csdn.net/drkcore/article/details/5207937 ...
- 查询SQL阻塞语句
SELECT SPID=p.spid, DBName = convert(CHAR(),d.name), ProgramName = program_name, LoginName = convert ...
- windows7 安装IIS没有default web site 解决方法
因当时是第一次在Windows 7上使用IIS,不小心把default web site删除了,后来无论怎么重装IIS都不行,最后想到能不能直接把别人安装好后的IIS相关文件夹拷过来使用,用宿舍一哥们 ...
- 使用editorconfig配置你的编辑器
摘要: 在团队开发中,统一的代码格式是必要的.但是不同开发人员使用的编辑工具可能不同,这样就造成代码的differ.今天给大家分享一个很好的方法来使不同的编辑器保持一样的风格. 不同的编辑器也有设置代 ...
- 自动构建工具Grunt
摘要: 大部分项目在部署之前都需要做的就是js.css文件的压缩.合并,以及一些文件的错误检查,甚至是将LESS文件转换成css文件,coffeescript文件转化成js文件等等.但是项目开发是分迭 ...
- ios开发之--搜索框的使用(PYSearchViewController的使用)
最近需要开发搜索框,比较了以前的各个版本的搜索框,UISearchDisplayController和UISearchController的使用,以后再做记录,随着ios11的更新,一些控件发生了,改 ...
- HttpClientUtil请求http地址的工具类
直接贴代码: import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache ...
- 设置js同源
1)设置 document.domain 成一样的就行了(前提是都是同一个顶级域名) 2)例如,域名1:news.xxx.com ,域名2:member.xxx.com,这时可以设置它们的 docum ...