EntityLoader_CustomLoader_Test.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Reflection;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Vitorm.Entity;
  4. using Vitorm.Entity.Loader.DataAnnotations;
  5. using Vitorm.Entity.PropertyType;
  6. using Vitorm.MsTest.CustomTest;
  7. using Vitorm.MsTest.Sqlite;
  8. namespace Vitorm.MsTest.Sqlite
  9. {
  10. [MyTable(table = "CustomUser2")]
  11. public class CustomUser
  12. {
  13. [MyKey]
  14. public int id { get; set; }
  15. public string name { get; set; }
  16. }
  17. }
  18. namespace Vitorm.MsTest.CustomTest
  19. {
  20. [TestClass]
  21. public class EntityLoader_CustomLoader_Test
  22. {
  23. [TestMethod]
  24. public void Test_EntityLoader()
  25. {
  26. {
  27. var name = Guid.NewGuid().ToString();
  28. Data.TryDropTable<CustomUser>();
  29. Data.TryCreateTable<CustomUser>();
  30. Data.Add(new CustomUser { id = 1, name = name });
  31. Assert.AreEqual(name, Data.Get<CustomUser>(1).name);
  32. }
  33. }
  34. }
  35. #region Custom EntityLoader Attribute
  36. public class MyTableAttribute : Attribute
  37. {
  38. public string table { get; set; }
  39. }
  40. public class MyKeyAttribute : Attribute { }
  41. public class CustomEntityLoader : IEntityLoader
  42. {
  43. public void CleanCache() { }
  44. public (bool success, IEntityDescriptor entityDescriptor) LoadDescriptor(Type entityType) => LoadFromType(entityType);
  45. public (bool success, IEntityDescriptor entityDescriptor) LoadDescriptorWithoutCache(Type entityType) => LoadFromType(entityType);
  46. public static bool GetTableName(Type entityType, out string tableName, out string schema)
  47. {
  48. tableName = entityType?.GetCustomAttribute<MyTableAttribute>()?.table;
  49. schema = null;
  50. return tableName != null;
  51. }
  52. public static (bool success, IEntityDescriptor EntityDescriptor) LoadFromType(Type entityType)
  53. {
  54. if (!GetTableName(entityType, out var tableName, out var schema)) return default;
  55. var propertyDescriptors = entityType?.GetProperties(BindingFlags.Public | BindingFlags.Instance)
  56. .Select(propertyInfo =>
  57. {
  58. // #1 isKey
  59. bool isKey = propertyInfo?.GetCustomAttribute<MyKeyAttribute>() != null;
  60. // #2 column name and type
  61. string columnName = propertyInfo.Name;
  62. string columnDbType = null;
  63. int? columnOrder = null;
  64. // #3 isIdentity
  65. var isIdentity = false;
  66. // #4 isNullable
  67. bool isNullable;
  68. {
  69. var type = propertyInfo.PropertyType;
  70. if (type == typeof(string)) isNullable = true;
  71. else
  72. {
  73. isNullable = TypeUtil.IsNullable(type);
  74. }
  75. }
  76. return new PropertyDescriptor(
  77. propertyInfo, propertyType: new PropertyValueType(propertyInfo.PropertyType),
  78. columnName: columnName,
  79. isKey: isKey, isIdentity: isIdentity, isNullable: isNullable,
  80. columnDbType: columnDbType,
  81. columnOrder: columnOrder
  82. );
  83. }).Where(column => column != null);
  84. var propertyType = new PropertyObjectType(entityType);
  85. propertyType.properties = propertyDescriptors.Select(m => (IPropertyDescriptor)m).ToArray();
  86. return (true, new EntityDescriptor(propertyType, tableName, schema));
  87. }
  88. }
  89. #endregion
  90. }