Browse Source

Added cloning objects for Copy-Paste

Kenric Nugteren 7 months ago
parent
commit
30cbb54524

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

@@ -292,6 +292,8 @@ namespace InABox.Core
             return _getter;
         }
 
+        Func<object, object> IProperty.NullSafeGetter() => Getter();
+
         public Action<object, object?> Setter()
         {
             return _setter;

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

@@ -74,6 +74,8 @@ namespace InABox.Core
 
         Func<object, object> Getter();
 
+        internal Func<object, object> NullSafeGetter();
+
         Action<object, object?> Setter();
 
         TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute;

+ 7 - 1
InABox.Core/DatabaseSchema/StandardProperty.cs

@@ -10,6 +10,7 @@ namespace InABox.Core
         public Type _class;
 
         private Func<object, object> _getter;
+        private Func<object, object> _nullSafeGetter;
 
         private string _name = "";
 
@@ -204,6 +205,12 @@ namespace InABox.Core
             return _getter;
         }
 
+        Func<object, object> IProperty.NullSafeGetter()
+        {
+            _nullSafeGetter ??= Expressions.Getter(_class, _name, propagateNulls: true);
+            return _nullSafeGetter;
+        }
+
         public Action<object, object?> Setter()
         {
             if (_setter is null) CheckExpressions();
@@ -226,7 +233,6 @@ namespace InABox.Core
             return sequence;
         }
 
-
         public override string ToString()
         {
             return string.Format("{0}.{1}", Class, Name);

+ 15 - 1
InABox.Core/Objects/BaseObject.cs

@@ -131,7 +131,7 @@ namespace InABox.Core
             SetObserving(true);
         }
 
-        private bool _disabledInterceptor;
+        internal bool _disabledInterceptor;
 
         protected T InitializeField<T>(ref T? field, string name)
             where T : BaseObject, ISubObject, new()
@@ -536,6 +536,20 @@ namespace InABox.Core
 
     public static class BaseObjectExtensions
     {
+        public static T Clone<T>(this T obj) where T : BaseObject, new()
+        {
+            var newObj = new T();
+            obj._disabledInterceptor = true;
+            foreach(var property in DatabaseSchema.Properties(obj.GetType()))
+            {
+                if (property.Parent != null && property.Parent.NullSafeGetter()(obj) is null) continue;
+
+                property.Setter()(newObj, property.Getter()(obj));
+            }
+            obj._disabledInterceptor = false;
+            return newObj;
+        }
+
         public static bool HasChanged(object? before, object? after)
         {
             if ((before == null || before.Equals("")) && (after == null || after.Equals("")))

+ 6 - 2
inabox.wpf/DynamicGrid/DynamicDataGrid.cs

@@ -645,8 +645,12 @@ public class DynamicDataGrid<TEntity> : DynamicGrid<TEntity>, IDynamicDataGrid w
     {
         if (!base.BeforeCopy(items)) return false;
 
-        foreach (var item in items)
-            item.ID = Guid.Empty;
+        for(int i = 0; i < items.Count; ++i)
+        {
+            var newItem = items[i].Clone();
+            newItem.ID = Guid.Empty;
+            items[i] = newItem;
+        }
         return true;
     }
 

+ 6 - 2
inabox.wpf/DynamicGrid/DynamicManyToManyGrid.cs

@@ -431,8 +431,12 @@ public class DynamicManyToManyGrid<TManyToMany, TThis> : DynamicGrid<TManyToMany
     {
         if (!base.BeforeCopy(items)) return false;
 
-        foreach (var item in items)
-            item.ID = Guid.Empty;
+        for(int i = 0; i < items.Count; ++i)
+        {
+            var newItem = items[i].Clone();
+            newItem.ID = Guid.Empty;
+            items[i] = newItem;
+        }
         return true;
     }
 }

+ 6 - 2
inabox.wpf/DynamicGrid/DynamicOneToManyGrid.cs

@@ -364,8 +364,12 @@ public class DynamicOneToManyGrid<TOne, TMany> : DynamicGrid<TMany>,
     {
         if (!base.BeforeCopy(items)) return false;
 
-        foreach (var item in items)
-            item.ID = Guid.Empty;
+        for(int i = 0; i < items.Count; ++i)
+        {
+            var newItem = items[i].Clone();
+            newItem.ID = Guid.Empty;
+            items[i] = newItem;
+        }
         return true;
     }