Guest User

Untitled

a guest
Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. var type = typeof(T);
  2. var cacheType = typeof(List<T>);
  3.  
  4. if (!GetQueries.TryGetValue(cacheType.TypeHandle, out string sql))
  5. {
  6. GetSingleKey<T>(nameof(GetAll));
  7. var name = GetTableName(type); <--- key thing
  8.  
  9. sql = "select * from " + name;
  10. GetQueries[cacheType.TypeHandle] = sql;
  11. }
  12.  
  13. private static string GetTableName(Type type)
  14. {
  15. if (TypeTableName.TryGetValue(type.TypeHandle, out string name)) return name;
  16.  
  17. if (TableNameMapper != null)
  18. {
  19. name = TableNameMapper(type); <-- key thing
  20. }
  21. else
  22. {
  23. //NOTE: This as dynamic trick should be able to handle both our own Table-attribute as well as the one in EntityFramework
  24. var tableAttr = type
  25. #if NETSTANDARD1_3
  26. .GetTypeInfo()
  27. #endif
  28. .GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic;
  29. if (tableAttr != null)
  30. {
  31. name = tableAttr.Name;
  32. }
  33. else
  34. {
  35. name = type.Name + "s";
  36. if (type.IsInterface() && name.StartsWith("I"))
  37. name = name.Substring(1);
  38. }
  39. }
  40.  
  41. TypeTableName[type.TypeHandle] = name;
  42. return name;
  43. }
  44.  
  45. SqlMapperExtensions.TableNameMapper = DapperMapper.TableNameMapper();
  46.  
  47. public static SqlMapperExtensions.TableNameMapperDelegate TableNameMapper()
  48. {
  49. return (type) =>
  50. {
  51. var has = Attribute.GetCustomAttribute(type, typeof(CastleTableAttribute));
  52. if (has != null)
  53. {
  54. return $"{ConfigurationProvider.Schema}.{type.Name}";
  55. }
  56. else
  57. {
  58. return type.Name;
  59. }
  60. };
  61. }
  62.  
  63. [CastleTable]
  64. class CubeTimestamps
  65. {
  66. [ExplicitKey]
  67. public int cube_id { get; set; }
  68. public DateTime cube_timestamp { get; set; }
  69. }
Add Comment
Please, Sign In to add comment