Browse Source

Improvements to PropertySequence

Kenric Nugteren 1 year ago
parent
commit
58db3f6df0

+ 3 - 1
InABox.Core/Column.cs

@@ -432,7 +432,9 @@ namespace InABox.Core
         {
             columns.Clear();
             
-            var props = DatabaseSchema.Properties(typeof(T)).Where(x=>x.Setter() != null).OrderBy(x => CoreUtils.GetPropertySequence(typeof(T), x.Name)).ToList();
+            var props = DatabaseSchema.Properties(typeof(T))
+                .Where(x => x.Setter() != null)
+                .OrderBy(x => x.PropertySequence()).ToList();
 
             if (types.Contains(ColumnType.All))
             {

+ 1 - 41
InABox.Core/CoreUtils.cs

@@ -1712,49 +1712,9 @@ namespace InABox.Core
             return sanitisedNamePart;
         }
 
-        private static Dictionary<(Type type, string column), decimal> _columnsequences = new Dictionary<(Type, string), decimal>();
-
-        private static decimal CalculatePropertySequence(IProperty? property)
-        {
-            var sequence = 0.0M;
-            if(property is null)
-            {
-                sequence = 999;
-            }
-            else if (property is CustomProperty)
-            {
-                sequence = property.Sequence;
-            }
-            else
-            {
-                while(property != null)
-                {
-                    sequence = property.Sequence + sequence / 1000.0M;
-
-                    property = property.Parent;
-                }
-            }
-            return sequence;
-        }
-
-        public static decimal GetPropertySequence(IProperty property)
-        {
-            if (!_columnsequences.TryGetValue((property.ClassType!, property.Name), out var sequence))
-            {
-                sequence = CalculatePropertySequence(property);
-                _columnsequences.Add((property.ClassType!, property.Name), sequence);
-            }
-            return sequence;
-        }
-
         public static decimal GetPropertySequence(Type type, string column)
         {
-            if(!_columnsequences.TryGetValue((type, column), out var sequence))
-            {
-                sequence = CalculatePropertySequence(DatabaseSchema.Property(type, column));
-                _columnsequences.Add((type, column), sequence);
-            }
-            return sequence;
+            return DatabaseSchema.Property(type, column)?.PropertySequence() ?? 999;
         }
 
         /// <summary>

+ 2 - 0
InABox.Core/DatabaseSchema/CustomProperty.cs

@@ -274,6 +274,8 @@ namespace InABox.Core
                 Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
             }
         }
+
+        public decimal PropertySequence() => Sequence;
     }
 
     public class CustomPropertyLookups : EntityLookup<CustomProperty>

+ 2 - 0
InABox.Core/DatabaseSchema/IProperty.cs

@@ -62,6 +62,8 @@ namespace InABox.Core
         Action<object, object?> Setter();
 
         TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute;
+
+        decimal PropertySequence();
     }
 
     public static class PropertyExtensions

+ 38 - 2
InABox.Core/DatabaseSchema/StandardProperty.cs

@@ -96,7 +96,18 @@ namespace InABox.Core
 
         public string Caption { get; set; }
 
-        public long Sequence { get; set; }
+        private decimal _propertySequence;
+
+        private long _sequence;
+        public long Sequence
+        {
+            get => _sequence;
+            set
+            {
+                _sequence = value;
+                _propertySequence = CalculatePropertySequence();
+            }
+        }
 
         [NullEditor]
         public string Page { get; set; }
@@ -105,7 +116,16 @@ namespace InABox.Core
 
         public LoggablePropertyAttribute? Loggable { get; set; }
 
-        public IProperty? Parent { get; set; }
+        private IProperty? _parent;
+        public IProperty? Parent
+        {
+            get => _parent;
+            set
+            {
+                _parent = value;
+                _propertySequence = CalculatePropertySequence();
+            }
+        }
 
         public bool IsEntityLink { get; set; }
 
@@ -146,6 +166,22 @@ namespace InABox.Core
             return _setter;
         }
 
+        public decimal PropertySequence() => _propertySequence;
+
+        private decimal CalculatePropertySequence()
+        {
+            var sequence = 0.0M;
+            IProperty? property = this;
+            while(property != null)
+            {
+                sequence = property.Sequence + sequence / 1000.0M;
+
+                property = property.Parent;
+            }
+            return sequence;
+        }
+
+
         public override string ToString()
         {
             return string.Format("{0}.{1}", Class, Name);

+ 1 - 1
InABox.Core/Imports/ImportFactory.cs

@@ -74,7 +74,7 @@ namespace InABox.Core
                     }
                     return result;
                 }))
-                .ThenBy(x => CoreUtils.GetPropertySequence(x));
+                .ThenBy(x => x.PropertySequence());
             
             //var keys = CoreUtils.PropertyList(type, x => true, true).Keys.ToArray();
             foreach (var prop in props)