提交 8aa10a02 编写于 作者: H hwikene

Add SqlMapper.RemoveTypeMap() method

上级 a155ebb4
......@@ -2484,6 +2484,52 @@ public void Issue253_TestIEnumerableTypeHandlerSetParameterValue()
}
}
public class RecordingTypeHandler<T> : Dapper.SqlMapper.TypeHandler<T>
{
public override void SetValue(IDbDataParameter parameter, T value)
{
SetValueWasCalled = true;
parameter.Value = value;
}
public override T Parse(object value)
{
ParseWasCalled = true;
return (T)value;
}
public bool SetValueWasCalled { get; set; }
public bool ParseWasCalled { get; set; }
}
[Fact]
public void Test_RemoveTypeMap()
{
Dapper.SqlMapper.ResetTypeHandlers();
Dapper.SqlMapper.RemoveTypeMap(typeof(DateTime));
var dateTimeHandler = new RecordingTypeHandler<DateTime>();
Dapper.SqlMapper.AddTypeHandler(dateTimeHandler);
connection.Execute("CREATE TABLE #Test_RemoveTypeMap (x datetime NOT NULL);");
try
{
connection.Execute(@"INSERT INTO #Test_RemoveTypeMap VALUES (@Now)", new { DateTime.Now });
connection.Query<DateTime>("SELECT * FROM #Test_RemoveTypeMap");
dateTimeHandler.ParseWasCalled.IsTrue();
dateTimeHandler.SetValueWasCalled.IsTrue();
}
finally
{
connection.Execute("DROP TABLE #Test_RemoveTypeMap");
Dapper.SqlMapper.AddTypeMap(typeof(DateTime), DbType.DateTime); // or an option to reset type map?
}
}
[Fact]
public void Issue130_IConvertible()
{
......
......@@ -260,6 +260,22 @@ public static void AddTypeMap(Type type, DbType dbType)
typeMap = newCopy;
}
/// <summary>
/// Removes the specified type from the Type/DbType mapping table
/// </summary>
public static void RemoveTypeMap(Type type)
{
// use clone, mutate, replace to avoid threading issues
var snapshot = typeMap;
if (!snapshot.ContainsKey(type)) return; // nothing to do
var newCopy = new Dictionary<Type, DbType>(snapshot);
newCopy.Remove(type);
typeMap = newCopy;
}
/// <summary>
/// Configure the specified type to be processed by a custom handler
/// </summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册