10

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?

3
  • Could you add some more info about what your tables look like and also the query from your stored procedure? Jan 22, 2019 at 14:59
  • @Florian Thanks for having a look at my post. I stumbled on a fix to the problem. Apparently, the CustomTypeHandler is not called on QueryFirstAsync calls. When I used connection.QueryAsync with a map parameter, the handler Parse is called. I don't know if this is a bug, a feature, or I don't fully understand when the CustomTypeHandler is invoked. Can anyone explain the difference?
    – hcode
    Jan 22, 2019 at 20:15
  • I'm having a similar issue with a .Query via a simplecrud .GetList. Like hcode said, can anyone shed light on when custom type handlers are invoked?
    – Dave
    Mar 27, 2019 at 10:51

1 Answer 1

3

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);
                }
            }

        }
2
  • So what's the fix? can't spot a major difference between the two Sep 30, 2020 at 1:46
  • "no this error in Dapper 2.0.30". New dapper version don't have described bug Oct 8, 2020 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.