zzzprojects / Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
https://dapper-plus.net/
380 stars 84 forks source link

Add Like Dapper TypeHandler #107

Closed znyet closed 2 years ago

znyet commented 2 years ago

hello! dapper can add TypeHandler like

public class MyTypeHandler<T> : SqlMapper.TypeHandler<T>
{
        public override T Parse(object value)
        {
            return JsonConvert.DeserializeObject<T>((string)value);  // Newtonsoft.Json
        }

        public override void SetValue(IDbDataParameter parameter, T value)
        {
            parameter.Value = (value == null) ? DBNull.Value : JsonConvert.SerializeObject(value);
            parameter.DbType = DbType.String;
        }
 }
public class Student
{
    public int Id { get; set; }

    public string Name { get; set; }

    [Column(0, "", "varchar(8000)")]
    public Child cc { get; set; } //is an varchar column in database
}

public class Child
{
    public string Name { get; set; }

    public int Sex { get; set; }
}

//map it 
SqlMapper.AddTypeHandler(new MyTypeHandler<Child>());

can dapper plus add like this?

JonathanMagnan commented 2 years ago

Hello @znyet ,

Thank you for reporting, we already discussed it recently.

At this moment, we currently have too many requests but we will look at it at the beginning of March.

Best regards,

Jon

znyet commented 2 years ago

Thank you! Jon. Dapper has two ways impl TypeHandler

//first way
public class MyTypeHandler : SqlMapper.ITypeHandler  //it is an ITypeHandler
{
    public object Parse(Type destinationType, object value) 
    {
        return JsonConvert.DeserializeObject((string)value, destinationType);
    }

    public void SetValue(IDbDataParameter parameter, object value)  //z.dapper.plus impl this
    {
        parameter.Value = (value == null) ? DBNull.Value : JsonConvert.SerializeObject(value);
        parameter.DbType = DbType.String;
    }
}

//use it 
SqlMapper.AddTypeHandler(typeof(Child), new MyTypeHandler());
//second way
public class MyTypeHandler<T> : SqlMapper.TypeHandler<T>  //it is an TypeHandler<T>
{
    public override T Parse(object value)
    {
        return JsonConvert.DeserializeObject<T>((string)value);
    }

    public override void SetValue(IDbDataParameter parameter, T value)  //z.dapper.plus impl this
    {
        parameter.Value = (value == null) ? DBNull.Value : JsonConvert.SerializeObject(value);
        parameter.DbType = DbType.String;
    }
}

//use it
SqlMapper.AddTypeHandler(new MyTypeHandler<Child>());

z.dapper.plus only need SetValue method. Because z.dapper.plus do not use query.Only insert、update、delete....

znyet

JonathanMagnan commented 2 years ago

Thank for the additional information ;)

znyet commented 2 years ago

Hello @JonathanMagnan , How many days will it take to complete ITypeHandler. I think this function is still very practical. Thank you for your hard work.

Best regards,

znyet

JonathanMagnan commented 2 years ago

Hello @znyet ,

I cannot give you an estimated date yet but we started to investigate this request this week.

At this moment, this is going really well as we success quickly to implement the "SetValue" part for some datasource but there is still some work to do. The parse should be really easy as well.

What might be hard would be to make sure and test all data sources we support.

I will be able to give you a better ETA next week.

Best Regards,

Jon

JonathanMagnan commented 2 years ago

Hello @znyet ,

Just to let you know, we hope to complete our test probably next week on that feature.

We currently target the date April 5 to release it if there is no problem

Best Regards,

Jon

znyet commented 2 years ago

Thank you for your hard work. I'm looking forward to it.

znyet commented 2 years ago

Hello @JonathanMagnan ,

Will release ITypeHandler tomorrow?

Best regards,

znyet

JonathanMagnan commented 2 years ago

Hello @znyet ,

Unfortunately, we didn't release anything this week.

However, this is in our merge list for next week. So this is coming really soon.

Best Regards,

Jon

znyet commented 2 years ago

Hello Jon, i test version 4.0.27 success, good job! thank you for your hard work.

internal class JsonNetTypeHandler : IBulkValueConverter
{
        public object ConvertFromProvider(Type destinationType, object value)
        {
            if (value == null || value == DBNull.Value)
            {
                return default;
            }
            try
            {
                var val = (string)value;
                if (val == "")
                {
                    return default;
                }
                return JsonConvert.DeserializeObject(val, destinationType);
            }
            catch
            {
                return default;
            }
        }

        public object ConvertToProvider(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }
            return JsonConvert.SerializeObject(value);
        }
}
DapperPlusManager.AddValueConverter(typeof(T), new JsonNetTypeHandler());

Best Regards,

znyet

JonathanMagnan commented 2 years ago

Hello @znyet ,

You have been faster than me ;)

The v4.0.27 has been released yesterday.

I guess I don't need to show you an example as you perfectly used it. You can either use the IBulkValueConverter or BulkValueConverter<T>.

Best Regards,

Jon