Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

CustomTypeHandler seems to doesn't work correctly

Open MNie opened this issue 7 years ago • 12 comments

I have an entity like this:

[Table("Entity", Schema = "dbo")]
internal class Entity
{
    [Key]
    public virtual int Id { get; set; }
    public virtual CustomType Filters { get; set; }
    public virtual string Name { get; set; }
    ...
}

and custom type like this:

[JsonObject]
public class CustomType
{
    [JsonProperty(PropertyName = "a")]public string a{ get; set; }
    [JsonProperty(PropertyName = "b")]public string b { get; set; }
    [JsonProperty(PropertyName = "d")]public IReadOnlyCollection<d> d{ get; set; }
}

custom handler like this:

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

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

and I register it in global.asax.cs like this:

SqlMapper.AddTypeHandler(new JsonTypeHandler<CustomType>());

The problem is that SetValue is not invoked when Execute is called. I'm using a Dapper.FastCrud, and my code to insert an entity to db looks like this:

var entity = new Entity
{
    Filters = new CustomType{bla bla},
    Name = "fafsd",
    ...
};

_dbConnection.Insert(dataExtractEntry, x => x.AttachToTransaction(tx));

Is there something more I have to do here?

MNie avatar Feb 02 '18 08:02 MNie

CC: @mgravell ?

MNie avatar Mar 01 '18 16:03 MNie

I have this issue too, seems it was fixed here, Which was added to the 1.50.5 release so it seems. But I can confirm that using 1.50.5 does not solve the issue.

ITemplate avatar Dec 06 '18 10:12 ITemplate

This is still an issue in 2.0.4

coshea avatar Sep 13 '19 16:09 coshea

any updates? Having the same issue

alexkitrum avatar May 05 '20 20:05 alexkitrum

Same issue in 2.0.123, Parse is called, SetValue isn't

ccornici avatar Feb 25 '22 08:02 ccornici

Can anyone advice how to check and debug for this error? Would like to know if something happen, how to debug and fix it. THanks.

Arios509 avatar May 11 '22 08:05 Arios509

How is this still an open bug after 4 years??? Is there any plan to fix this or is there at least a workaround?

scott-mac avatar Aug 18 '22 20:08 scott-mac

This SO answer may be relevant: https://stackoverflow.com/a/56786941/986720

alex-ht-work avatar Jan 09 '23 06:01 alex-ht-work

In version 2.1.24 this still happens. I have a custom TypeHandler for Ulid and SetValue doesn't get called, thus throwing the exception:

System.AggregateException : One or more errors occurred. (No mapping exists from object type System.Ulid to a known managed provider native type.) ---- System.InvalidOperationException : No mapping exists from object type System.Ulid to a known managed provider native type.

Are there any plans to fix this soon? Could I help in any way?

alex13lopez avatar Dec 11 '23 18:12 alex13lopez

How is this still an open bug after 4 years??? Is there any plan to fix this or is there at least a workaround?

Don't be so rude and ask politely, after all this is an open-source free of charge library that Dapper devs do on their free time. If you want it fixed soon, the code is there to clone so go ahead and fix it yourself.

And yes, there are workarounds, I'm also affected by this issue and I've found a quite simple workaround to implement in my code.

alex13lopez avatar Dec 12 '23 11:12 alex13lopez

How is this still an open bug after 4 years??? Is there any plan to fix this or is there at least a workaround?

Don't be so rude and ask politely, after all this is an open-source free of charge library that Dapper devs do on their free time. If you want it fixed soon, the code is there to clone so go ahead and fix it yourself.

And yes, there are workarounds, I'm also affected by this issue and I've found a quite simple workaround to implement in my code.

Careful there now, if you fall off that high horse of yours you're bound to get hurt.

scott-mac avatar Dec 12 '23 13:12 scott-mac

A library doesn't do exactly what you want. That doesn't necessarily make it a bug; there are complex interactions that make this surprisingly subtle to implement without breaking other scenarios. Our highest priority is: not breaking existing usage. To quote from the license (apache):

Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.

You are free to use it as-is, but the maintainers (mostly: me) do not owe you a fix, an apology, an explanation, or promises. That's not how this works. Anything - including this reply - is above and beyond any SLA that we have, because: we don't have an SLA with you.

Because of the complex interactions of this API and the lack of configurability (and the complexity of maintenance) in Dapper, a lot of active development is moving to Dapper.AOT - see https://github.com/DapperLib/Dapper/issues/1909 for more context/reasons. This is currently usable but incomplete. I can state that a revised and replacement API - with better design - in Dapper.AOT is more likely than a fix here.

You are also free to work on a fix that handles all scenarios, or fork and use a fix that handles just your scenarios.

mgravell avatar Dec 12 '23 14:12 mgravell