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
QueryableExtensions
are part of the package and are inAutoMapper.QueryableExtensions
namespace - 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, ' ...
随机推荐
- [转]Apache 监听端口失败,selinux惹的祸
原文在此 CentOS 下启动Httpd 失败,报 (13)Permission denied: make_sock: could not bind to address [::]:8000 因为 小 ...
- MyEclipse如何恢复删掉的文件
今天一不小心删了项目里的两个包,心里那个痛啊,一想MyEclipse这么强大,应该会有恢复文件的功能吧,要不就太坑了啊. 果不其然让我找到了方法: 如图:右击项目选择 然后在弹出的页面勾选需要恢复的文 ...
- 如何使用 URLOpenStream 函数
URLOpenStream 和 URLDownloadToFile 类似, 都是下载文件的 COM 函数; 前者是下载到 IStream 流, 后者是直接下载到指定路径; 不如后者使用方便. 它们都声 ...
- linux环境中iostat命令的安装,解决-bash: iostat: command not found问题
需求说明: 今天在测试环境的主机上,准备通过iostat来查看系统的io情况,发现没有该命令 [root@testvm Packages]# iostat -bash: iostat: command ...
- getActionBar().setDisplayHomeAsUpEnabled(true)报空指针(已解决)
今天捣鼓了一下午.getActionBar().setDisplayHomeAsUpEnabled(true)总是报空指针.在我的还有一个Android4.4.2的项目中就没有一点问题.我还以为是我自 ...
- SpringMVC由浅入深day02_1课程安排_2包装类型pojo参数绑定_3集合类型绑定
springmvc第二天 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器: ...
- mysql asyn 示例
这篇文章摘自mysql asyn作者的博客,博客地址 开头有一个简单示例,然后是一个在play上的应用.例子我并没有跑过,但是仍能学到不少东西. object BasicExample { def m ...
- 【Android】录音暂停和继续
https://www.2cto.com/kf/201410/347839.html http://blog.csdn.net/wanli_smile/article/details/7715030 ...
- yarn基础架构
Yarn的基本架构 Yarn是Hadoop2.0中的资源管理系统,它的基本设计思想是将MRv1中的JobTracker拆分成两个独立的服务:一个全局的资源管理器ResourceManager和每个应用 ...
- Discuz!X 3.4 任意文件删除漏洞复现过程(附python脚本)
今天看下群里在讨论Discuz!X 3.4 任意文件删除漏洞,自己做了一些测试,记录一下过程.结尾附上自己编写的python脚本,自动化实现任意文件删除. 具体漏洞,请查看 https://paper ...