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, ' ...
随机推荐
- Single Pattern(单例模式)
单例模式是一种常用的软件设计模式.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源.如果希望在系统中某个类的实例只能存在一个,单例模式是最好的 ...
- BarTender表单的人性化设计—分组框
BarTender 2016已经支持用户输入信息.从相同位置查询数据库和筛选数据记录,那就是数据输入表单了.这个功能相信迎合了很多用户的需求,主要作用体现在打印时数据输入. 对于表单的设计,不同的客户 ...
- BarTender怎么打印公式化的三列标签
有小伙伴在业务上有这样的需求:使用BarTender打印一行三列的标签,如下A,B,C三个并排.第一行 A1=a B1=a*2-1 C1=a*2:第二行 A2=a+1 B2=(a+1)*2-1 C2= ...
- Linux系统安装workerman,启动wss 服务
安装workerman其实很简单,只要会简单的linux口令就可以搞定, 这里我给大家演示一下如何安装workerman 进入终端的过程就不用演示了吧... 输入root及密码进入终端后找到站点根目录 ...
- springboot+swagger2案例
1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- linux添加PYTHONPATH环境变量
1.添加环境变量到pythonpath export PYTHONPATH=$PYTHONPATH:/home/myproject 查看pythonpathecho $PYTHONPATH 可以进入p ...
- Log4net用法(.config文件)
1.引用log4net.dll 2.在AssemblyInfo.cs中添加初始化: [assembly: log4net.Config.XmlConfigurator(ConfigFile = &qu ...
- nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
当你执行sudo nginx -s reload时出现nginx: [error] open() "/run/nginx.pid" failed (2: No such file ...
- 【安全开发】PHP安全编码规范
申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...
- 代码审计之DocCms漏洞分析
0x01 前言 DocCms[音译:稻壳Cms] ,定位于为企业.站长.开发者.网络公司.VI策划设计公司.SEO推广营销公司.网站初学者等用户 量身打造的一款全新企业建站.内容管理系统,服务于企业品 ...