123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System.Reflection;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Vitorm.Entity;
- using Vitorm.Entity.Loader.DataAnnotations;
- using Vitorm.Entity.PropertyType;
- using Vitorm.MsTest.CustomTest;
- using Vitorm.MsTest.Sqlite;
- namespace Vitorm.MsTest.Sqlite
- {
- [MyTable(table = "CustomUser2")]
- public class CustomUser
- {
- [MyKey]
- public int id { get; set; }
- public string name { get; set; }
- }
- }
- namespace Vitorm.MsTest.CustomTest
- {
- [TestClass]
- public class EntityLoader_CustomLoader_Test
- {
- [TestMethod]
- public void Test_EntityLoader()
- {
- {
- var name = Guid.NewGuid().ToString();
- Data.TryDropTable<CustomUser>();
- Data.TryCreateTable<CustomUser>();
- Data.Add(new CustomUser { id = 1, name = name });
- Assert.AreEqual(name, Data.Get<CustomUser>(1).name);
- }
- }
- }
- #region Custom EntityLoader Attribute
- public class MyTableAttribute : Attribute
- {
- public string table { get; set; }
- }
- public class MyKeyAttribute : Attribute { }
- public class CustomEntityLoader : IEntityLoader
- {
- public void CleanCache() { }
- public (bool success, IEntityDescriptor entityDescriptor) LoadDescriptor(Type entityType) => LoadFromType(entityType);
- public (bool success, IEntityDescriptor entityDescriptor) LoadDescriptorWithoutCache(Type entityType) => LoadFromType(entityType);
- public static bool GetTableName(Type entityType, out string tableName, out string schema)
- {
- tableName = entityType?.GetCustomAttribute<MyTableAttribute>()?.table;
- schema = null;
- return tableName != null;
- }
- public static (bool success, IEntityDescriptor EntityDescriptor) LoadFromType(Type entityType)
- {
- if (!GetTableName(entityType, out var tableName, out var schema)) return default;
- var propertyDescriptors = entityType?.GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Select(propertyInfo =>
- {
- // #1 isKey
- bool isKey = propertyInfo?.GetCustomAttribute<MyKeyAttribute>() != null;
- // #2 column name and type
- string columnName = propertyInfo.Name;
- string columnDbType = null;
- int? columnOrder = null;
- // #3 isIdentity
- var isIdentity = false;
- // #4 isNullable
- bool isNullable;
- {
- var type = propertyInfo.PropertyType;
- if (type == typeof(string)) isNullable = true;
- else
- {
- isNullable = TypeUtil.IsNullable(type);
- }
- }
- return new PropertyDescriptor(
- propertyInfo, propertyType: new PropertyValueType(propertyInfo.PropertyType),
- columnName: columnName,
- isKey: isKey, isIdentity: isIdentity, isNullable: isNullable,
- columnDbType: columnDbType,
- columnOrder: columnOrder
- );
- }).Where(column => column != null);
- var propertyType = new PropertyObjectType(entityType);
- propertyType.properties = propertyDescriptors.Select(m => (IPropertyDescriptor)m).ToArray();
- return (true, new EntityDescriptor(propertyType, tableName, schema));
- }
- }
- #endregion
- }
|