小巧玲珑的动态返回类型 [英] Dapper dynamic return types

查看:240
本文介绍了小巧玲珑的动态返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用dapper.net因为虽然现在它的一个非常好的ORM映射伟大的工程与.net动态类型,但我注意到,当从数据库中检索短小精悍的数据返回为小巧玲珑行类型是有什么办法我可以在任何其他类型的像System.Dynamic.ExpandoObject退货吗?

I have been using dapper.net for while now and its a very good ORM mapper which works great with .Net dynamic types but I noticed that when dapper retrieves data from database it returns as Dapper row type is there are any way that i can return it in any other type Like System.Dynamic.ExpandoObject ?

推荐答案

DapperRow 对象旨在共享行之间有很多的状态。例如,如果您取40行,列名等只存储的一次。如果我们用 ExpandoObject ,这将需要每行进行配置。因此,使用 DapperRow 作为的背后 - 中 - 场景的实现细节是故意效率的事情。

The DapperRow object is designed to share a lot of state between rows. For example, if you fetch 40 rows, the column names etc are only stored once. If we used ExpandoObject, this would need to be configured per row. Hence, the use of DapperRow as the behind - the - scenes implementation detail is a deliberate efficiency thing.

请注意该物体从动态的API也可以转换为返回的IDictionary<字符串对象>

Note that the object returned from the dynamic APIs can also be cast as IDictionary<string,object>.

我会,但是,开放给支持的其他的支持这本词典使用类型 - 其中 ExpandoObject 就是其中之一。所以,是的,它的可能的改变使得:

I would, however, be open to supporting other types that support this dictionary usage - of which ExpandoObject is one. So yes, it could be changed such that:

var rows = conn.Query<ExpandoObject>(...);

工作。它只是需要代码的支持的它,该代码目前不存在。因此,没有,但也许在未来的版本。

works. It simply requires code to support it, and that code does not currently exist. So "no, but perhaps in a future build".

还请注意,你不需要使用 DapperRow 在所有...越多的有望的方案是使用通用的API来兑现自己的类型。

Note also that you don't need to use DapperRow at all... The more expected scenario is to use the generic API to materialize your own types.

这篇关于小巧玲珑的动态返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆