فهرست منبع

Fix to bug in AutoDataModel

Kenric Nugteren 1 سال پیش
والد
کامیت
648771bdfe
1فایلهای تغییر یافته به همراه19 افزوده شده و 9 حذف شده
  1. 19 9
      InABox.Core/DataModel/AutoDataModel.cs

+ 19 - 9
InABox.Core/DataModel/AutoDataModel.cs

@@ -7,10 +7,21 @@ using System.Threading.Tasks;
 
 namespace InABox.Core
 {
-    public class AutoDataModel<T> : DataModel<T> where T : Entity, IPersistent, IRemotable, new()
+    internal static class _AutoDataModel
     {
         private static Type[]? _allo2mtypes;
 
+        public static Type[] GetOneOrManyToManyTypes()
+        {
+            _allo2mtypes ??= CoreUtils.Entities.Where(x => x.HasInterface(typeof(IOneToMany<>)) || x.HasInterface(typeof(IManyToMany<,>))).ToArray();
+            return _allo2mtypes;
+        }
+    }
+
+    public class AutoDataModel<T> : DataModel<T> where T : Entity, IPersistent, IRemotable, new()
+    {
+        private static bool _loaded = false;
+
         // Method; Parent Column; Child Column; Columns expression; Table Name
         private static readonly List<Tuple<MethodInfo, Expression, Expression, IColumns, string>> children =
             new List<Tuple<MethodInfo, Expression, Expression, IColumns, string>>();
@@ -47,14 +58,11 @@ namespace InABox.Core
 
             var headName = TableName<T>();
 
-            if (_allo2mtypes == null)
+            if (!_loaded)
             {
-                _allo2mtypes = CoreUtils.Entities.Where(x => x.HasInterface(typeof(IOneToMany<>)) || x.HasInterface(typeof(IManyToMany<,>))).ToArray();
-
-                var maps = _allo2mtypes.Where(x =>
-                        x.GetInterfaceDefinition(typeof(IOneToMany<>))?.GenericTypeArguments[0] == typeof(T)
-                        && !x.HasInterface<ISkipLoad>())
-                    .OrderBy(x => x.EntityName());
+                var maps = _AutoDataModel.GetOneOrManyToManyTypes()
+                    .Where(x => x.GetInterfaces(typeof(IOneToMany<>)).Any(x => x.GenericTypeArguments[0] == typeof(T)) && !x.HasInterface<ISkipLoad>())
+                    .OrderBy(x => x.Name);
                 var childMethod = GetType().GetMethods().Where(x => string.Equals(x.Name, nameof(AddChildTable)) && x.IsGenericMethod).FirstOrDefault();
                 var lookupMethod = GetType().GetMethods().Where(x => string.Equals(x.Name, nameof(AddLookupTable)) && x.IsGenericMethod).FirstOrDefault();
                 foreach (var map in maps)
@@ -104,7 +112,8 @@ namespace InABox.Core
                     }
                 }
 
-                var mapsLookup = _allo2mtypes.Where(x => x.GetInterfaceDefinition(typeof(IManyToMany<,>))?.GenericTypeArguments.Contains(typeof(T)) == true)
+                var mapsLookup = _AutoDataModel.GetOneOrManyToManyTypes()
+                    .Where(x => x.GetInterfaces(typeof(IManyToMany<,>)).Any(x => x.GenericTypeArguments.Contains(typeof(T))))
                     .OrderBy(x => x.EntityName());
                 foreach (var map in mapsLookup)
                 {
@@ -146,6 +155,7 @@ namespace InABox.Core
                         //_childtables.Add(new Tuple<Type, String, bool>(map, prop.Name, false));
                     }
                 }
+                _loaded = true;
             }
 
             foreach (var child in children)