EntityLoader_CustomLoader_Test.cs 3.4 KB

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