Commit 1fff7b97 authored by Marc Gravell's avatar Marc Gravell

Fixed: Issue 165

parent 71bac1da
...@@ -587,7 +587,7 @@ public static int GetCachedSQLCount() ...@@ -587,7 +587,7 @@ public static int GetCachedSQLCount()
#endif #endif
static readonly Dictionary<Type, DbType> typeMap; static Dictionary<Type, DbType> typeMap;
static SqlMapper() static SqlMapper()
{ {
...@@ -630,34 +630,59 @@ static SqlMapper() ...@@ -630,34 +630,59 @@ static SqlMapper()
typeMap[typeof(TimeSpan?)] = DbType.Time; typeMap[typeof(TimeSpan?)] = DbType.Time;
typeMap[typeof(object)] = DbType.Object; typeMap[typeof(object)] = DbType.Object;
AddTypeHandler(typeof(DataTable), new DataTableHandler()); AddTypeHandlerImpl(typeof(DataTable), new DataTableHandler(), false);
} }
/// <summary> /// <summary>
/// Configire the specified type to be mapped to a given db-type /// Configire the specified type to be mapped to a given db-type
/// </summary> /// </summary>
public static void AddTypeMap(Type type, DbType dbType) public static void AddTypeMap(Type type, DbType dbType)
{ {
typeMap[type] = dbType; // use clone, mutate, replace to avoid threading issues
var snapshot = typeMap;
DbType oldValue;
if (snapshot.TryGetValue(type, out oldValue) && oldValue == dbType) return; // nothing to do
var newCopy = new Dictionary<Type, DbType>(snapshot);
newCopy[type] = dbType;
typeMap = newCopy;
} }
/// <summary> /// <summary>
/// Configire the specified type to be processed by a custom handler /// Configire the specified type to be processed by a custom handler
/// </summary> /// </summary>
public static void AddTypeHandler(Type type, ITypeHandler handler) public static void AddTypeHandler(Type type, ITypeHandler handler)
{
AddTypeHandlerImpl(type, handler, true);
}
/// <summary>
/// Configire the specified type to be processed by a custom handler
/// </summary>
public static void AddTypeHandlerImpl(Type type, ITypeHandler handler, bool clone)
{ {
if (type == null) throw new ArgumentNullException("type"); if (type == null) throw new ArgumentNullException("type");
var snapshot = typeHandlers;
ITypeHandler oldValue;
if (snapshot.TryGetValue(type, out oldValue) && handler == oldValue) return; // nothing to do
var newCopy = clone ? new Dictionary<Type, ITypeHandler>(snapshot) : snapshot;
#pragma warning disable 618 #pragma warning disable 618
typeof(TypeHandlerCache<>).MakeGenericType(type).GetMethod("SetHandler", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new object[] { handler }); typeof(TypeHandlerCache<>).MakeGenericType(type).GetMethod("SetHandler", BindingFlags.Static | BindingFlags.NonPublic).Invoke(null, new object[] { handler });
#pragma warning restore 618 #pragma warning restore 618
if (handler == null) typeHandlers.Remove(type); if (handler == null) newCopy.Remove(type);
else typeHandlers[type] = handler; else newCopy[type] = handler;
typeHandlers = newCopy;
} }
/// <summary> /// <summary>
/// Configire the specified type to be processed by a custom handler /// Configire the specified type to be processed by a custom handler
/// </summary> /// </summary>
public static void AddTypeHandler<T>(TypeHandler<T> handler) public static void AddTypeHandler<T>(TypeHandler<T> handler)
{ {
AddTypeHandler(typeof(T), handler); AddTypeHandlerImpl(typeof(T), handler, true);
} }
/// <summary> /// <summary>
...@@ -696,7 +721,7 @@ internal static void SetHandler(ITypeHandler handler) ...@@ -696,7 +721,7 @@ internal static void SetHandler(ITypeHandler handler)
private static ITypeHandler handler; private static ITypeHandler handler;
} }
private static readonly Dictionary<Type, ITypeHandler> typeHandlers = new Dictionary<Type, ITypeHandler>(); private static Dictionary<Type, ITypeHandler> typeHandlers = new Dictionary<Type, ITypeHandler>();
internal const string LinqBinary = "System.Data.Linq.Binary"; internal const string LinqBinary = "System.Data.Linq.Binary";
internal static DbType LookupDbType(Type type, string name, out ITypeHandler handler) internal static DbType LookupDbType(Type type, string name, out ITypeHandler handler)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment