简体   繁体   English

使用Dapper映射超过5种类型

[英]Using Dapper to map more than 5 types

I am currently building a SELECT query that joins 12 tables together. 我目前正在构建一个将12个表连接在一起的SELECT查询。 I've been using Dapper for all my other queries and it works great. 我一直在使用Dapper进行所有其他查询,效果很好。 Problem is, the generic methods only have to five generic parameters. 问题是,泛型方法只需要五个通用参数。

I've previously modified the code to support up to 6 for another query, but now I really don't think I should be hacking 6 more levels of generics. 我之前修改过代码以支持最多6个用于另一个查询,但现在我真的不认为我应该破解6个更多级别的泛型。

Is there a way to pass dapper an array of types, and it returns the results as an array of objects, which I can cast manually if I have to? 有没有办法将dapper传递给一个类型数组,并将结果作为一个对象数组返回,如果必须的话我可以手动编译?

I also might be approaching the problem the wrong way! 我也可能以错误的方式接近问题! Any help will be appreciated! 任何帮助将不胜感激!

In a project I worked on I saw something like this to get more than 7 types mapped. 在我工作的一个项目中,我看到这样的东西可以映射超过7种类型。 We used Dapper 1.38: 我们使用了Dapper 1.38:

connection.Query<TypeOfYourResult>
(
   queryString,
   new[]
   {
      typeof(TypeOfArgument1),
      typeof(TypeOfArgument2),
      ...,
      typeof(TypeOfArgumentN)
   },
   objects =>
   {
      TypeOfArgument1 arg1 = objects[0] as TypeOfArgument1;
      TypeOfArgument2 arg2 = objects[1] as TypeOfArgument2;
      ...
      TypeOfArgumentN argN = objects[N] as TypeOfArgumentN;

     // do your processing here, e.g. arg1.SomeField = arg2, etc.
     // also initialize your result

     var result = new TypeOfYourResult(...)

     return result;
   },
   parameters,
   splitOn: "arg1_ID,arg2_ID, ... ,argN_ID"
);

The queryString is self-explanatory. queryString是不言自明的。 The splitOn parameter says how Dapper should split the columns from the SELECT statement so that everything can be mapped properly to the objects, you can read about it here . splitOn参数说明Dapper应如何从SELECT语句中拆分列,以便可以将所有内容正确映射到对象, 您可以在此处阅读

You could use a dynamic query and map it afterwards. 您可以使用动态查询并在之后映射它。 Something like this 像这样的东西

var result = conn.Query<dynamic>(query).Select(x => new Tuple<Type1, Type2, Type3, Type4, Type5>( 
// type initialization here 
    new Type1(x.Property1,x.Property2),
    new Type2(x.Property3,x.Property4),
    new Type3(x.Property5,x.Property6) etc....));

Edit: With a rather huge result set, another option might be to use multiple querys and then use a Grid Reader. 编辑:使用相当大的结果集,另一个选项可能是使用多个查询,然后使用网格阅读器。 That might work for you. 这可能对你有用。

There's the example taken from the dapper age: 这个例子取自小巧玲珑的时代:

var sql = 
@"
select * from Customers where CustomerId = @id
select * from Orders where CustomerId = @id
select * from Returns where CustomerId = @id";

using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
   var customer = multi.Read<Customer>().Single();
   var orders = multi.Read<Order>().ToList();
   var returns = multi.Read<Return>().ToList();
   ...
} 

This has been answered long time ago, but I would like to add my two cents here. 这已经很久以前得到了解答,但我想在这里补充两分钱。 Instead of manually modify Dapper's source code, why don't you just create a poco class with those fields and use your query like a table? 而不是手动修改Dapper的源代码,为什么不用这些字段创建一个poco类并像表一样使用您的查询?

The mapping would work fine, I know it is a pain also to do that class definition, but seems easier than dealing with later Dapper's updates. 映射可以正常工作,我知道做类定义也很痛苦,但似乎比处理后来的Dapper更新更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM