Browse Source

EntityLoader

Lith 10 months ago
parent
commit
4757ecf203

+ 5 - 2
src/Vitorm/DbContext.cs

@@ -5,7 +5,7 @@ using System.Linq;
 using Vit.Linq.ExpressionTree;
 
 using Vitorm.Entity;
-using Vitorm.Entity.DataAnnotations;
+using Vitorm.Entity.Loader;
 
 namespace Vitorm
 {
@@ -46,8 +46,11 @@ namespace Vitorm
         }
         #endregion
 
+        public static DefaultEntityLoader defaultEntityLoader = new();
 
-        public virtual IEntityDescriptor GetEntityDescriptor(Type entityType) => EntityDescriptor.GetEntityDescriptor(entityType);
+        public IEntityLoader entityLoader = defaultEntityLoader;
+
+        public virtual IEntityDescriptor GetEntityDescriptor(Type entityType) => entityLoader.LoadDescriptor(entityType);
 
 
         public virtual ExpressionConvertService convertService => Environment.convertService;

+ 0 - 0
src/Vitorm/Entity/ColumnDescriptor.cs → src/Vitorm/Entity/DataAnnotations/ColumnDescriptor.cs


+ 0 - 19
src/Vitorm/Entity/DataAnnotations/EntityDescriptor.cs

@@ -6,25 +6,6 @@ namespace Vitorm.Entity.DataAnnotations
 {
     public partial class EntityDescriptor : IEntityDescriptor
     {
-
-        static ConcurrentDictionary<Type, EntityDescriptor> descMap = new();
-
-
-        public static IEntityDescriptor GetEntityDescriptor(Type entityType)
-        {
-            if (descMap.TryGetValue(entityType, out var entityDescriptor)) return entityDescriptor;
-
-            entityDescriptor = LoadFromType(entityType);
-            if (entityDescriptor != null) descMap[entityType] = entityDescriptor;
-
-            return entityDescriptor;
-        }
-
-        public static IEntityDescriptor GetEntityDescriptor<Entity>()
-        {
-            return GetEntityDescriptor(typeof(Entity));
-        }
-
         public EntityDescriptor(Type entityType, IColumnDescriptor[] allColumns, string tableName, string schema = null)
         {
             this.entityType = entityType;

+ 14 - 3
src/Vitorm/Entity/DataAnnotations/EntityDescriptor.FromType.cs → src/Vitorm/Entity/DataAnnotations/EntityLoader.cs

@@ -2,11 +2,21 @@
 using System.ComponentModel.DataAnnotations.Schema;
 using System.Linq;
 using System.Reflection;
+using Vitorm.Entity.Loader;
 
 namespace Vitorm.Entity.DataAnnotations
 {
-    public partial class EntityDescriptor  
+    public class EntityLoader : IEntityLoader
     {
+        public void CleanCache()
+        {
+        }
+
+        public IEntityDescriptor LoadDescriptor(Type entityType)
+        {
+            return LoadFromType(entityType);
+        }
+
         public static bool GetTableName(Type entityType, out string tableName, out string schema)
         {
             var attribute = entityType?.GetCustomAttribute<global::System.ComponentModel.DataAnnotations.Schema.TableAttribute>();
@@ -17,7 +27,7 @@ namespace Vitorm.Entity.DataAnnotations
         public static string GetTableName(Type entityType) => GetTableName(entityType, out var tableName, out _) ? tableName : null;
 
 
-        private static EntityDescriptor LoadFromType(Type entityType)
+        public static EntityDescriptor LoadFromType(Type entityType)
         {
             if (!GetTableName(entityType, out var tableName, out var schema)) return null;
 
@@ -50,11 +60,12 @@ namespace Vitorm.Entity.DataAnnotations
                      }
                  }
 
-                 return new ColumnDescriptor(propertyInfo, name: name, isKey: isKey, databaseGenerated: databaseGenerated, databaseType: databaseType, nullable: nullable); 
+                 return new ColumnDescriptor(propertyInfo, name: name, isKey: isKey, databaseGenerated: databaseGenerated, databaseType: databaseType, nullable: nullable);
              }).Where(column => column != null).ToArray();
 
             return new EntityDescriptor(entityType, allColumns, tableName, schema);
         }
 
+
     }
 }

+ 42 - 0
src/Vitorm/Entity/Loader/DefaultEntityLoader.cs

@@ -0,0 +1,42 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using Vitorm.Entity.LoaderAttribute;
+
+namespace Vitorm.Entity.Loader
+{
+    public class DefaultEntityLoader : IEntityLoader
+    {
+        public List<IEntityLoader> loaders = new();
+        public DefaultEntityLoader()
+        {
+            loaders.Add(new DataAnnotations.EntityLoader());
+
+            loaders.Add(new EntityLoaderFromAttribute());
+        }
+
+        ConcurrentDictionary<Type, IEntityDescriptor> descriptorCache = new();
+
+        public void CleanCache()
+        {
+            descriptorCache.Clear();
+            foreach (var loader in loaders) loader.CleanCache();
+        }
+
+        public IEntityDescriptor LoadDescriptor(Type entityType)
+        {
+            if (descriptorCache.TryGetValue(entityType, out var entityDescriptor)) return entityDescriptor;
+            foreach (var loader in loaders)
+            {
+                entityDescriptor = loader.LoadDescriptor(entityType);
+                if (entityDescriptor != null)
+                {
+                    descriptorCache[entityType] = entityDescriptor;
+                    return entityDescriptor;
+                }
+            }
+            return null;
+        }
+
+    }
+}

+ 10 - 0
src/Vitorm/Entity/Loader/IEntityLoader.cs

@@ -0,0 +1,10 @@
+using System;
+
+namespace Vitorm.Entity.Loader
+{
+    public interface IEntityLoader
+    {
+        void CleanCache();
+        IEntityDescriptor LoadDescriptor(Type entityType);
+    }
+}

+ 10 - 0
src/Vitorm/Entity/LoaderAttribute/EntityLoaderAttribute.cs

@@ -0,0 +1,10 @@
+using System;
+
+namespace Vitorm.Entity.LoaderAttribute
+{
+    [AttributeUsage(AttributeTargets.Class)]
+    public class EntityLoaderAttribute : Attribute
+    {
+        public Type Loader { get; set; }
+    }
+}

+ 21 - 0
src/Vitorm/Entity/LoaderAttribute/EntityLoaderFromAttribute.cs

@@ -0,0 +1,21 @@
+using System;
+using System.Reflection;
+using Vitorm.Entity.Loader;
+
+namespace Vitorm.Entity.LoaderAttribute
+{
+    public class EntityLoaderFromAttribute : IEntityLoader
+    {
+        public void CleanCache()
+        {
+        }
+
+        public IEntityDescriptor LoadDescriptor(Type entityType)
+        {
+            var loaderType = entityType.GetCustomAttribute<EntityLoaderAttribute>()?.Loader;
+            if (loaderType == null || !typeof(IEntityLoader).IsAssignableFrom(loaderType)) return null;
+            var loader = Activator.CreateInstance(loaderType) as IEntityLoader;
+            return loader?.LoadDescriptor(entityType);
+        }
+    }
+}