Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomTypeHandler seems to doesn't work correctly #934

Open
MNie opened this issue Feb 2, 2018 · 12 comments
Open

CustomTypeHandler seems to doesn't work correctly #934

MNie opened this issue Feb 2, 2018 · 12 comments

Comments

@MNie
Copy link

MNie commented Feb 2, 2018

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
Copy link
Author

MNie commented Mar 1, 2018

CC: @mgravell ?

@ITemplate
Copy link

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.

@coshea
Copy link

coshea commented Sep 13, 2019

This is still an issue in 2.0.4

@alexkitrum
Copy link

any updates? Having the same issue

@ccornici
Copy link

ccornici commented Feb 25, 2022

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

@Arios509
Copy link

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.

@scott-mac
Copy link

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

@alex-ht-work
Copy link

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

@alex13lopez
Copy link

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
Copy link

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.

@scott-mac
Copy link

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.

@mgravell
Copy link
Member

mgravell commented Dec 12, 2023

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 #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests