Browse Source

Fody Autoporoperties stuff

frogsoftware 10 months ago
parent
commit
538ed9051e

+ 63 - 25
InABox.Core/BaseObject.cs

@@ -8,6 +8,7 @@ using System.Linq.Expressions;
 using System.Reflection;
 using System.Runtime.CompilerServices;
 using System.Runtime.Serialization;
+using AutoProperties;
 
 //using PropertyChanged;
 
@@ -31,11 +32,45 @@ namespace InABox.Core
     /// </summary>
     public abstract class BaseObject : INotifyPropertyChanged, IBaseObject
     {
+        [GetInterceptor]
+        protected T GetValue<T>(string name, Type propertyType, PropertyInfo propertyInfo, FieldInfo fieldInfo, object fieldValue, T genricFieldValue, ref T refToBackingField)
+        {
+            if (propertyType.IsSubclassOf(typeof(BaseObject)) && !propertyType.IsAbstract && refToBackingField == null)
+            {
+                if (this.GetType().IsAbstract)
+                    return refToBackingField;
+                if (propertyType.IsGenericType)
+                    return refToBackingField;
+                try
+                {
+                    refToBackingField ??= Activator.CreateInstance<T>();
+                    if (refToBackingField is ISubObject subObject)
+                    {
+                        subObject.SetLinkedParent(this);
+                        subObject.SetLinkedPath(name);
+                    }
+                }
+                catch (Exception e)
+                {
+
+                }
+            }
+
+            return refToBackingField;
+        }
+        
+        [SetInterceptor]
+        protected void SetValue<T>(string name, Type propertyType, PropertyInfo propertyInfo, FieldInfo fieldInfo, object newValue, T genricNewValue, out T refToBackingField)
+        {
+            refToBackingField = genricNewValue;
+        }
+        
+        
         public BaseObject()
         {
             SetObserving(false);
 
-            DatabaseSchema.InitializeSubObjects(this);
+            //DatabaseSchema.InitializeSubObjects(this);
 
             Init();
             SetObserving(true);
@@ -57,11 +92,11 @@ namespace InABox.Core
 
         protected virtual void Init()
         {
-            CheckOriginalValues();
+            //CheckOriginalValues();
 
-            LoadedColumns = new HashSet<string>();
+            //LoadedColumns = new HashSet<string>();
 
-            UserProperties = new UserProperties();
+            //UserProperties = new UserProperties();
             //UserProperties.ParentType = this.GetType();
             DatabaseSchema.InitializeObject(this);
             UserProperties.OnPropertyChanged += (o, n, b, a) =>
@@ -110,12 +145,15 @@ namespace InABox.Core
 
         private bool bChanged;
 
-        [DoNotPersist]
-        public ConcurrentDictionary<string, object?> OriginalValues { get; set; }
-
-        [DoNotPersist]
-        [DoNotSerialize]
-        public HashSet<string> LoadedColumns { get; set; }
+        private readonly ConcurrentDictionary<string, object?> originalValues = new ConcurrentDictionary<string, object?> ();
+        private readonly HashSet<string> loadedColumns = new HashSet<string>();
+        
+        [DoNotPersist] 
+        public ConcurrentDictionary<string, object?> OriginalValues => originalValues;
+        
+        [DoNotPersist] 
+        [DoNotSerialize] 
+        public HashSet<string> LoadedColumns => loadedColumns;
 
         protected virtual void SetChanged(string name, object? before, object? after)
         {
@@ -143,7 +181,7 @@ namespace InABox.Core
 
         private bool QueryChanged()
         {
-            CheckOriginalValues();
+            //CheckOriginalValues();
             if (OriginalValues.Count > 0)
                 return true;
 
@@ -173,22 +211,22 @@ namespace InABox.Core
             if (!BaseObjectExtensions.HasChanged(before, after))
                 return;
 
-            CheckOriginalValues();
+            //CheckOriginalValues();
             if (!OriginalValues.ContainsKey(name))
                 OriginalValues[name] = before;
             SetChanged(name, before, after);
         }
 
-        private void CheckOriginalValues()
-        {
-            if (OriginalValues == null)
-            {
-                var bObserving = _observing;
-                _observing = false;
-                OriginalValues = new ConcurrentDictionary<string, object?>();
-                _observing = bObserving;
-            }
-        }
+        // private void CheckOriginalValues()
+        // {
+        //     if (OriginalValues == null)
+        //     {
+        //         var bObserving = _observing;
+        //         _observing = false;
+        //         OriginalValues = new ConcurrentDictionary<string, object?>();
+        //         _observing = bObserving;
+        //     }
+        // }
 
         public bool IsChanged()
         {
@@ -201,7 +239,7 @@ namespace InABox.Core
             var bObs = IsObserving();
             SetObserving(false);
 
-            CheckOriginalValues();
+            //CheckOriginalValues();
             foreach (var key in OriginalValues.Keys.ToArray())
             {
                 try
@@ -251,7 +289,7 @@ namespace InABox.Core
         {
             bApplyingChanges = true;
 
-            CheckOriginalValues();
+            //CheckOriginalValues();
             OriginalValues.Clear();
 
             bChanged = false;
@@ -268,7 +306,7 @@ namespace InABox.Core
             var type = GetType();
             try
             {
-                CheckOriginalValues();
+                //CheckOriginalValues();
                 foreach (var key in OriginalValues.Keys)
                     try
                     {

+ 6 - 4
InABox.Core/CoreTable/CoreRow.cs

@@ -107,10 +107,12 @@ namespace InABox.Core
                         }
 
                         var setter = setters[i];
-                        if (setter != null && value != null && !(value is System.DBNull))
-                            setter.Invoke(obj, value);
-                        else
-                            CoreUtils.SetPropertyValue(obj, column, value);
+                        if (setter != null)
+                            setter.Invoke(obj,value);
+                        // if (setter != null && value != null && !(value is System.DBNull))
+                        //     setter.Invoke(obj, value);
+                        // else
+                        //     CoreUtils.SetPropertyValue(obj, column, value);
                     }
                 }
                 catch (Exception e)

+ 1 - 0
InABox.Core/FodyWeavers.xml

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
+    <AutoProperties />
     <PropertyChanged/>
 </Weavers>

+ 1 - 0
InABox.Core/FodyWeavers.xsd

@@ -53,6 +53,7 @@
             </xs:attribute>
           </xs:complexType>
         </xs:element>
+        <xs:element name="AutoProperties" minOccurs="0" maxOccurs="1" type="xs:anyType" />
       </xs:all>
       <xs:attribute name="VerifyAssembly" type="xs:boolean">
         <xs:annotation>

+ 1 - 0
InABox.Core/InABox.Core.csproj

@@ -23,6 +23,7 @@
         <None Remove=".gitignore" />
     </ItemGroup>
     <ItemGroup>
+        <PackageReference Include="AutoProperties.Fody" Version="1.25.0" />
         <PackageReference Include="ExpressiveParser" Version="3.0.1" />
         <PackageReference Include="FluentResults" Version="3.15.2" />
         <PackageReference Include="Fody" Version="6.8.1">

+ 14 - 0
InABox.Database/DataUpdater.cs

@@ -265,4 +265,18 @@ public static class DataUpdater
             return false;
         }
     }
+    
+    public static bool DoSpecificMigration(VersionNumber from, VersionNumber to)
+    {
+        try
+        {
+            return MigrateDatabase(from, to, out _);
+        }
+        catch(Exception e)
+        {
+            Logger.Send(LogType.Error, "", $"Error while performing specific migration: {CoreUtils.FormatException(e)}");
+            return false;
+        }
+    }
+    
 }

+ 1 - 1
InABox.Database/DbFactory.cs

@@ -86,7 +86,7 @@ public static class DbFactory
             throw new Exception("Database migration failed. Aborting startup");
         }
 
-
+        DataUpdater.DoSpecificMigration(new VersionNumber(8, 13), new VersionNumber(8,14));
         //Load up your custom properties here!
         // Can't use clients (b/c we're inside the database layer already
         // but we can simply access the store directly :-)

+ 1 - 0
InABox.Poster.MYOB/FodyWeavers.xml

@@ -1,3 +1,4 @@
 <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
+  <AutoProperties />
   <PropertyChanged />
 </Weavers>

+ 1 - 0
InABox.Poster.MYOB/FodyWeavers.xsd

@@ -53,6 +53,7 @@
             </xs:attribute>
           </xs:complexType>
         </xs:element>
+        <xs:element name="AutoProperties" minOccurs="0" maxOccurs="1" type="xs:anyType" />
       </xs:all>
       <xs:attribute name="VerifyAssembly" type="xs:boolean">
         <xs:annotation>

+ 1 - 0
InABox.Poster.MYOB/InABox.Poster.MYOB.csproj

@@ -8,6 +8,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="AutoProperties.Fody" Version="1.25.0" />
     <PackageReference Include="Fody" Version="6.8.1">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

+ 1 - 1
inabox.wpf/DynamicGrid/DynamicItemsListGrid.cs

@@ -60,7 +60,7 @@ public class DynamicItemsListGrid<T> : DynamicGrid<T>, IDynamicItemsListGrid
 
     public override T LoadItem(CoreRow row)
     {
-        return Items[_recordmap[row].Index];
+        return _recordmap.TryGetValue(row, out var _value) ? Items[_value.Index] : null;
     }
 
     protected override void Reload(