博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AutoMapper queryable extensions 只找需要的字段
阅读量:6232 次
发布时间:2019-06-22

本文共 2405 字,大约阅读时间需要 8 分钟。

How to generate a LINQ query for your DTOs

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 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 for more info).

 

Queryable Extensions

Automapper provides a solution to this proble: (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
    ();
  4. Query by hand (see query above)
    var postDto =      session.Query
    ().Where(post => post.Id == id) .Project().To
    () .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 .

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

转载地址:http://ogqna.baihongyu.com/

你可能感兴趣的文章
jQuery 插件开发全解析
查看>>
DEDE中 field:rel 是什么意思,起一个什么样的作用效果
查看>>
【图片】批量获取几万张图片
查看>>
Spring经常使用属性的注入及属性编辑器
查看>>
FreeRTOS系列第13篇---FreeRTOS内核控制
查看>>
python入门小记
查看>>
将逻辑卷降为物理分区
查看>>
CMake 入门实战【转】
查看>>
软硬件之共生之道——一千零一夜的启发
查看>>
redis 性能建议
查看>>
Android MaoZhuaWeiBo开发Service抓取个人信息-2
查看>>
Codefoces 436 B. Om Nom and Spiders
查看>>
流程控制------if else分支语句
查看>>
禁用Clusterware在系统启动后自己主动启动
查看>>
Storm编程入门API系列之Storm的Topology默认Workers、默认executors和默认tasks数目
查看>>
Json转java对象和List集合
查看>>
PHP操作MongoDB数据库具体样例介绍(增、删、改、查) (六)
查看>>
关于Unity中的模型描边与Shader切换(专题二)
查看>>
《淘宝技术这十年》读后感
查看>>
程序员经常加班的真正原因
查看>>