Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var type = typeof(T);
- var cacheType = typeof(List<T>);
- if (!GetQueries.TryGetValue(cacheType.TypeHandle, out string sql))
- {
- GetSingleKey<T>(nameof(GetAll));
- var name = GetTableName(type); <--- key thing
- sql = "select * from " + name;
- GetQueries[cacheType.TypeHandle] = sql;
- }
- private static string GetTableName(Type type)
- {
- if (TypeTableName.TryGetValue(type.TypeHandle, out string name)) return name;
- if (TableNameMapper != null)
- {
- name = TableNameMapper(type); <-- key thing
- }
- else
- {
- //NOTE: This as dynamic trick should be able to handle both our own Table-attribute as well as the one in EntityFramework
- var tableAttr = type
- #if NETSTANDARD1_3
- .GetTypeInfo()
- #endif
- .GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic;
- if (tableAttr != null)
- {
- name = tableAttr.Name;
- }
- else
- {
- name = type.Name + "s";
- if (type.IsInterface() && name.StartsWith("I"))
- name = name.Substring(1);
- }
- }
- TypeTableName[type.TypeHandle] = name;
- return name;
- }
- SqlMapperExtensions.TableNameMapper = DapperMapper.TableNameMapper();
- public static SqlMapperExtensions.TableNameMapperDelegate TableNameMapper()
- {
- return (type) =>
- {
- var has = Attribute.GetCustomAttribute(type, typeof(CastleTableAttribute));
- if (has != null)
- {
- return $"{ConfigurationProvider.Schema}.{type.Name}";
- }
- else
- {
- return type.Name;
- }
- };
- }
- [CastleTable]
- class CubeTimestamps
- {
- [ExplicitKey]
- public int cube_id { get; set; }
- public DateTime cube_timestamp { get; set; }
- }
Add Comment
Please, Sign In to add comment