How to use generic and Nullable <T> types in Dapper to materialize?

I have this call:

public IObservable<T> Subscribe(object topic, string service, string connectionString, string query)
{
    try
    {
        this.connection.ConnectionString = connectionString;
        this.connection.Open();
        this.connection.Query<T>(query, new { transactionid = topic }).ToObservable().Subscribe(message => this.subject.OnNext(message));
        return this.subject;
    }
    catch (Exception e)
    {
        this.subject.OnError(e);
        return this.subject;
    }
    finally
    {
        this.subject.OnCompleted();
        this.connection.Close();
    }
}

This is my request:

with IDS as  (select L1_ID, L2_ID, L1_VALUE, L2_VALUE 
from MYDB where MYDB.ID = :transactionid) 
select * from 
(select L1_ID as ID, round(L1_VALUE, 28) as VALUE from IDS
union 
select L2_ID as ID, round(L2_VALUE, 28) as VALUE from IDS) UN

This will result in an error:

An arbitrary default constructor or one matching signature (System.String ID, System.Decimal VALUE) is required for System.Tuple 2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Nullable1 [[System.Decimal, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089]], mscorlib , Version = 4.0.0.0, Culture = Neutral, PublicKeyToken = b77a5c561934e089]] materialization

+3
source share
1 answer

The problem is not in Nullable<T>, but in Tuple<,>.

Dapper .

ID      varchar
VALUE   decimal

( )

T, ( , , 0 ID 1 is VALUE ..):

T row = new T() { ID = reader.GetInt32(0), VALUE = reader.GetDecimal(1) };

T row = new T(ID: reader.GetInt32(0), VALUE: reader.GetDecimal(1));

, , , , .

: Tuple<T1,T2> . :

public Tuple(T1 item1, T2 item2);

- dapper , , . , dapper , ( ) , , .

:

  • :

    public class SomeType { // could also be a struct, immutable, whatever
        public int Id {get;set;}
        public decimal Value {get;set;}
    }
    

    T === SomeType

  • API :

    Query(query, new { transactionid = topic }).Select(
        row => Tuple.Create((int)row.ID, (decimal)row.VALUE
    ).Whatever(...);
    
  • item1 item2 (yeuch!)

+4

Source: https://habr.com/ru/post/1651994/


All Articles