Jelajahi Sumber

Added override existing parameter to FillObject. Added IEnumerable.ToColumns

Kenric Nugteren 1 tahun lalu
induk
melakukan
12231efe9d
2 mengubah file dengan 57 tambahan dan 15 penghapusan
  1. 22 0
      InABox.Core/Column.cs
  2. 35 15
      InABox.Core/CoreTable/CoreRow.cs

+ 22 - 0
InABox.Core/Column.cs

@@ -204,6 +204,19 @@ namespace InABox.Core
             columns = new List<Column<T>>();
         }
 
+        /// <summary>
+        /// Create a new <see cref="Columns{T}"/>, using <paramref name="columns"/> as the internal list.
+        /// </summary>
+        /// <remarks>
+        /// <paramref name="columns"/> is passed by reference and is stored as the internal list of the <see cref="Columns{T}"/>;
+        /// hence if one wishes <paramref name="columns"/> to not be modified, one should pass a copy of the list.
+        /// </remarks>
+        /// <param name="columns"></param>
+        public Columns(List<Column<T>> columns)
+        {
+            this.columns = columns;
+        }
+
         public Columns<TNew> Cast<TNew>()
             where TNew : T
         {
@@ -572,6 +585,15 @@ namespace InABox.Core
             return columns.GetEnumerator();
         }
     }
+
+    public static class ColumnsExtensions
+    {
+        public static Columns<T> ToColumns<T>(this IEnumerable<Column<T>> columns)
+        {
+            return new Columns<T>(columns.ToList());
+        }
+    }
+
     public static class ColumnSerialization
     {
         /// <summary>

+ 35 - 15
InABox.Core/CoreTable/CoreRow.cs

@@ -67,7 +67,17 @@ namespace InABox.Core
                 Set(columnName, value);
         }
 
-        public void FillObject(Type t, BaseObject obj)
+        /// <summary>
+        /// Fill an object with the data from this row.
+        /// </summary>
+        /// <remarks>
+        /// If <paramref name="overrideExisting"/> is <see langword="true"/>, then the data in the row will override the data in <paramref name="obj"/>,
+        /// even if that column has previously been loaded (i.e., it is in <see cref="BaseObject.LoadedColumns"/>).
+        /// </remarks>
+        /// <param name="t">The type of <paramref name="obj"/>.</param>
+        /// <param name="obj">The object to fill.</param>
+        /// <param name="overrideExisting">Override any data which already exists in <paramref name="obj"/>.</param>
+        public void FillObject(Type t, BaseObject obj, bool overrideExisting = false)
         {
             obj.SetObserving(false);
 
@@ -85,19 +95,20 @@ namespace InABox.Core
                 var value = this[column];
                 try
                 {
-                    if (bFirst)
+                    if (obj.LoadedColumns.Add(column) || overrideExisting)
                     {
-                        var prop = DatabaseSchema.Property(t, column);
-                        setters.Add(prop?.Setter());
+                        if (bFirst)
+                        {
+                            var prop = DatabaseSchema.Property(t, column);
+                            setters.Add(prop?.Setter());
+                        }
+
+                        var setter = setters[i];
+                        if (setter != null && value != null)
+                            setter.Invoke(obj, value);
+                        else
+                            CoreUtils.SetPropertyValue(obj, column, value);
                     }
-
-                    var setter = setters[i];
-                    if (setter != null && value != null)
-                        setter.Invoke(obj, value);
-                    else
-                        CoreUtils.SetPropertyValue(obj, column, value);
-
-                    obj.LoadedColumns.Add(column);
                 }
                 catch (Exception e)
                 {
@@ -109,15 +120,24 @@ namespace InABox.Core
             obj.SetObserving(true);
         }
 
-        public void FillObject<T>(T obj) where T : BaseObject
+        /// <summary>
+        /// Fill an object with the data from this row.
+        /// </summary>
+        /// <remarks>
+        /// If <paramref name="overrideExisting"/> is <see langword="true"/>, then the data in the row will override the data in <paramref name="obj"/>,
+        /// even if that column has previously been loaded (i.e., it is in <see cref="BaseObject.LoadedColumns"/>).
+        /// </remarks>
+        /// <param name="obj">The object to fill.</param>
+        /// <param name="overrideExisting">Override any data which already exists in <paramref name="obj"/>.</param>
+        public void FillObject<T>(T obj, bool overrideExisting = false) where T : BaseObject
         {
-            FillObject(typeof(T), obj);
+            FillObject(typeof(T), obj, overrideExisting: overrideExisting);
         }
 
         public BaseObject ToObject(Type t)
         {
             var entity = (Activator.CreateInstance(t) as BaseObject)!;
-            FillObject(t, entity);
+            FillObject(t, entity, overrideExisting: true);
             return entity;
         }