Update 2018-02-08
Dapper code has changed a lot since this answer was written almost 5 years ago. Since Mark Gravell commented on this question, it should not have been necessary when the question was asked, so it probably will not make much sense today either.
The code may or may not work. Even if it still works, it is not optimal, so I cannot recommend it in good faith. Use at your own risk.
Line 227 Database.cs shows:
static ConcurrentDictionary<Type, string> tableNameMap = new ConcurrentDictionary<Type, string>(); static ConcurrentDictionary<Type, List<string>> paramNameCache = new ConcurrentDictionary<Type, List<string>>();
which means that it is private. I'm not even sure if you can access it using Reflection (although that would be nice). Itβs best to add the ClearCache method to the source (as it is open source) and submit it for review.
Perhaps Sam Shaffron or Mark Gravell can work out.
I do not use Dapper, but I think the following extension method should work with the version in Repo:
public static class DapperExtensions { public static void ClearTableCache<TDatabase>(this Database<TDatabase> dapperDb) { var fld = dapperDb.GetType().GetField("tableNameMap", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); if (fld == null) throw new NotSupportedException("Unable to locate Private field tableNameMap"); var obj = fld.GetValue(null); if (obj == null) throw new NotSupportedException("Unable to get value from tableNameMap"); var clear = obj.GetType().GetMethod("Clear"); if (clear == null) throw new NotSupportedException("Unable to locate ConcurrentDictionary<T, U>.Clear"); clear.Invoke(obj, null); } public static void ClearParamCache<TDatabase>(this Database<TDatabase> dapperDb) { var fld = dapperDb.GetType().GetField("paramNameCache", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); if (fld == null) throw new NotSupportedException("Unable to locate Private field paramNameMap"); var obj = fld.GetValue(null); if (obj == null) throw new NotSupportedException("Unable to get value from paramNameMap"); var clear = obj.GetType().GetMethod("Clear"); if (clear == null) throw new NotSupportedException("Unable to locate ConcurrentDictionary<T, U>.Clear"); clear.Invoke(obj, null); } }
It has not been tested with Dapper, but I tested the principle using POCO. Access to a private API is dangerous (at best), but the reflection used in this code example should work with the current version.
M.Babcock
source share