Using Dapper to map more than 5 types

后端 未结 3 777
野趣味
野趣味 2020-12-04 22:24

I am currently building a SELECT query that joins 12 tables together. I\'ve been using Dapper for all my other queries and it works great. Problem is, the generic methods on

3条回答
  •  一个人的身影
    2020-12-04 23:13

    In a project I worked on I saw something like this to get more than 7 types mapped. We used Dapper 1.38:

    connection.Query
    (
       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. 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.

提交回复
热议问题