瀏覽代碼

Refactored LinkedProperties into its own static class

Frank van den Bos 2 年之前
父節點
當前提交
70bd4af96e

+ 0 - 8
InABox.Core/Dimensions/Dimensions.cs

@@ -66,14 +66,6 @@ namespace InABox.Core
         {
         {
             base.Init();
             base.Init();
             Unit = Activator.CreateInstance(typeof(TLink), new object[] { new Func<Entity>(LinkedEntity) }) as TLink;
             Unit = Activator.CreateInstance(typeof(TLink), new object[] { new Func<Entity>(LinkedEntity) }) as TLink;
-            // Unit.PropertyChanged += (sender, args) =>
-            // {
-            //     var unit = sender as DimensionUnitLink<TUnit>;
-            //     DoPropertyChanged(
-            //         String.Format("Unit.{0}",args.PropertyName), 
-            //         unit.GetOriginalValue<DimensionUnitLink<TUnit>,object>(args.PropertyName), 
-            //         CoreUtils.GetPropertyValue(unit,args.PropertyName));
-            // };
         }
         }
         
         
         private bool bCalculating = false;
         private bool bCalculating = false;

+ 1 - 1
InABox.Core/EnclosedEntity.cs

@@ -31,7 +31,7 @@ namespace InABox.Core
             var le = LinkedEntity();
             var le = LinkedEntity();
             if (IsObserving() && le != null)
             if (IsObserving() && le != null)
             {
             {
-                var link = le.LinkedProperties(this).FirstOrDefault(x => x.Source.Equals(name));
+                var link = LinkedProperties.Find(le, this).FirstOrDefault(x => x.Source.Equals(name));
                 if (link != null)
                 if (link != null)
                     link.Update(this, le);
                     link.Update(this, le);
             }
             }

+ 2 - 23
InABox.Core/Entity.cs

@@ -289,8 +289,7 @@ namespace InABox.Core
                 LastUpdate = DateTime.Now;
                 LastUpdate = DateTime.Now;
 
 
             LastUpdateBy = ClientFactory.UserID;
             LastUpdateBy = ClientFactory.UserID;
-
-
+            
             // This doesn;t work - keeps being updated to current date
             // This doesn;t work - keeps being updated to current date
             // Created => null ::Set ID = guid.empty -> now :: any other change -> unchanged!
             // Created => null ::Set ID = guid.empty -> now :: any other change -> unchanged!
             // Moved to Create(), should not simply be overwritten on deserialise from json
             // Moved to Create(), should not simply be overwritten on deserialise from json
@@ -301,27 +300,7 @@ namespace InABox.Core
             //    CreatedBy = ClientFactory.UserID;
             //    CreatedBy = ClientFactory.UserID;
             //}
             //}
         }
         }
-
-        #region Linked Properties
-
-        // Why?
-        [DoNotSerialize]
-        private static readonly List<ILinkedProperty> _LinkedProperties = new List<ILinkedProperty>();
-
-        public virtual void LinkProperty<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
-            Expression<Func<TLinkedEntity, TType>> target)
-        {
-            var map = new LinkedProperty<TLinkedEntity, TEntityLink, TType>(path, source, target);
-            if (!_LinkedProperties.Any(x => x.Equals(map)))
-                _LinkedProperties.Add(map);
-        }
-
-        public IEnumerable<ILinkedProperty> LinkedProperties(object path)
-        {
-            return _LinkedProperties.Where(x => (x.Type == this.GetType()) && (CoreUtils.GetPropertyValue(this,x.Path) == path));
-        }
-
-        #endregion
+        
     }
     }
 
 
     public interface ILicense<TLicenseToken> where TLicenseToken : LicenseToken
     public interface ILicense<TLicenseToken> where TLicenseToken : LicenseToken

+ 2 - 2
InABox.Core/EntityLink.cs

@@ -76,7 +76,7 @@ namespace InABox.Core
 
 
             var le = LinkedEntity();
             var le = LinkedEntity();
             if (le != null)
             if (le != null)
-                foreach (var link in le.LinkedProperties(this))
+                foreach (var link in LinkedProperties.Find(le,this))
                 {
                 {
                     link.Update(this, le);
                     link.Update(this, le);
                     result = true;
                     result = true;
@@ -112,7 +112,7 @@ namespace InABox.Core
             var le = LinkedEntity();
             var le = LinkedEntity();
             if (IsObserving() && le != null)
             if (IsObserving() && le != null)
             {
             {
-                var link = le.LinkedProperties(this).FirstOrDefault(x => x.Source.Equals(name));
+                var link = LinkedProperties.Find(le, this).FirstOrDefault(x => x.Source.Equals(name));
                 if (link != null)
                 if (link != null)
                     link.Update(this, le);
                     link.Update(this, le);
             }
             }

+ 27 - 0
InABox.Core/LinkedProperties.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+
+namespace InABox.Core
+{
+    public static class LinkedProperties
+    {
+
+        private static readonly List<ILinkedProperty> _LinkedProperties = new List<ILinkedProperty>();
+
+        public static void Register<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
+            Expression<Func<TLinkedEntity, TType>> target)
+        {
+            var map = new LinkedProperty<TLinkedEntity, TEntityLink, TType>(path, source, target);
+            if (!_LinkedProperties.Any(x => x.Equals(map)))
+                _LinkedProperties.Add(map);
+        }
+
+        public static IEnumerable<ILinkedProperty> Find(object parent, object path)
+        {
+            return _LinkedProperties.Where(x => (x.Type == parent.GetType()) && (CoreUtils.GetPropertyValue(parent,x.Path) == path));
+        }
+        
+    }
+}