Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper custom SqlMapper.TypeHandler Parse method not called

I created a SqlMapper.TypeHandler to map a Customer object into a CreditAccount class as follows:

public class CustomerTypeHandler : SqlMapper.TypeHandler<Customer>
{
    public override Customer Parse(object value)
    {
        throw new NotImplementedException();
    }
    public override void SetValue(IDbDataParameter parameter, Customer 
    value)
    {
        throw new NotImplementedException();
    }
}

public class CreditAccount
{
    public int AccountId { get; set; }
    public Customer Customer{ get; set; }
}

public class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
}

When I connect to the DB and call a sproc, the CustomerTypeHandler Parse method is never called and my CreditAccount object is populated with the AccountId only. The Customer object is null.

I am calling it as follows:

    public async Task<CreditAccount> GetCreditAccount(int accountId)
    {
        var sql = "MY PROC NAME HERE";
        var parameters = new DynamicParameters();
        parameters.Add("@AccountId", accountId);

            SqlMapper.AddTypeHandler(new CustomerTypeHandler());

            using (IDbConnection connection = Connection)
            {
                connection.Open();
                var account = await connection.QueryFirstAsync<CreditAccount>(sql, parameters, commandType: CommandType.StoredProcedure);
                return account;
            }
        }
    }

I placed a breakpoint in the Parse method and it is never called. The database connection works, and I am getting the AccountId.

My environment; .NET Core 2.2
Dapper 1.50.5

The code is simple enough. I get not exceptions. Any ideas?

like image 875
hcode Avatar asked Jan 21 '19 14:01

hcode


1 Answers

A year has passed and now there is no this error in Dapper 2.0.30. I checked it on jsonb columns in Postgres.

using Dapper;
using Newtonsoft.Json;
using Npgsql;
using System;
using System.Data;

    public class CreditAccount
    {
        public int AccountId { get; set; }
        public Customer Customer { get; set; }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
    }

    public class CustomerJsonObjectTypeHandler : SqlMapper.TypeHandler<Customer>
    {
        public override void SetValue(IDbDataParameter parameter, Customer value)
        {
            parameter.Value = (value == null)
                ? (object)DBNull.Value
                : JsonConvert.SerializeObject(value);
            parameter.DbType = DbType.String;
        }

        public override Customer Parse(object value)
        {
            return JsonConvert.DeserializeObject<Customer>(value.ToString());
        }
    }

Example using this classes - all work fine.

        static void Main(string[] args)
        {

            using (var connection = GetDefaultConnection())
            {
                connection.Open();
                var customer = new Customer
                {
                    FirstName = "Gaday",
                    LastName = "Ivanova",
                    MiddleName = "Petrovich"
                };
                var jsonData = JsonConvert.SerializeObject(customer);
                var strQuery = $"SELECT 10500 as AccountId,'{jsonData}'::jsonb as Customer";

                SqlMapper.AddTypeHandler(new CustomerJsonObjectTypeHandler());

                try
                {
                    var data = connection.QueryFirst<CreditAccount>(strQuery);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

        }
like image 54
Костя Гришин Avatar answered Nov 02 '22 05:11

Костя Гришин