Closed znyet closed 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
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
Thank for the additional information ;)
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
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
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
Thank you for your hard work. I'm looking forward to it.
Hello @JonathanMagnan ,
Will release ITypeHandler tomorrow?
Best regards,
znyet
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
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
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
hello! dapper can add TypeHandler like
can dapper plus add like this?