فهرست منبع

Fix to synchronise function being dumb.

Kenric Nugteren 1 سال پیش
والد
کامیت
1374100800
3فایلهای تغییر یافته به همراه18 افزوده شده و 43 حذف شده
  1. 6 31
      InABox.Core/EntityLink.cs
  2. 2 2
      InABox.Core/LinkedProperties.cs
  3. 10 10
      InABox.Core/LinkedProperty.cs

+ 6 - 31
InABox.Core/EntityLink.cs

@@ -88,39 +88,14 @@ namespace InABox.Core
         {
             var result = false;
             var props = CoreUtils.PropertyList(GetType(), x => (!x.Name.Equals("ID") || Entity == null) && !x.DeclaringType.Equals(typeof(BaseObject)), false);
-            foreach (var key in props.Keys)
+            foreach (var prop in DatabaseSchema.Properties(GetType()))
             {
-                if (CoreUtils.HasProperty(Entity.GetType(), key))
+                if (prop.Name == "ID" || !(prop is StandardProperty stdProp) || stdProp.Property.DeclaringType.Equals(typeof(BaseObject))) continue;
+
+                var entityProp = DatabaseSchema.Property(Entity.GetType(), prop.Name);
+                if(entityProp != null)
                 {
-                    var prop = CoreUtils.GetProperty(typeof(T), key);
-                    if (prop != null && prop.PropertyType.Equals(props[key]))
-                        
-                    {
-                        if (prop.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
-                        {
-                            var tgtlink = CoreUtils.GetPropertyValue(this, key) as IEntityLink;
-                            var srclink = CoreUtils.GetPropertyValue(Entity, key) as IEntityLink;
-                            tgtlink.ID = srclink.ID;
-                            tgtlink.Synchronise(srclink);
-                        }
-                        else
-                        {
-                            var oldvalue = CoreUtils.GetPropertyValue(this, key);
-                            var newvalue = CoreUtils.GetPropertyValue(Entity, key);
-                            if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
-                            {
-                                result = true;
-                                try
-                                {
-                                    CoreUtils.SetPropertyValue(this, key, newvalue);
-                                }
-                                catch (Exception e)
-                                {
-                                    Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
-                                }
-                            }
-                        }
-                    }
+                    prop.Setter()(this, entityProp.Getter()(Entity));
                 }
             }
 

+ 2 - 2
InABox.Core/LinkedProperties.cs

@@ -16,7 +16,7 @@ namespace InABox.Core
 
         void SetLinkedPath(string path);
     }
-    
+
     // public interface IResult
     // {
     //     object? Value { get; set; }
@@ -48,7 +48,7 @@ namespace InABox.Core
         private static readonly Dictionary<Type, List<ILinkedProperty>> _LinkedProperties = new Dictionary<Type, List<ILinkedProperty>>();
         
         public static void Register<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
-            Expression<Func<TLinkedEntity?, TType>> target, Func<TLinkedEntity?,TType, Result<TType, string>>? func = null)
+            Expression<Func<TLinkedEntity, TType>> target, Func<TLinkedEntity,TType, Result<TType, string>>? func = null)
             where TLinkedEntity : class
             where TEntityLink : class
         {

+ 10 - 10
InABox.Core/LinkedProperty.cs

@@ -12,7 +12,7 @@ namespace InABox.Core
         String Path { get; }
         String Source { get; }
         String Target { get; }
-        void Update(object? from, object? to);
+        void Update(object from, object to);
 
         /// <summary>
         /// This equality comparer checks against type, path, source and target
@@ -29,11 +29,11 @@ namespace InABox.Core
         public Type Type => typeof(TLinkedEntity);
         public String Path { get; }
 
-        private Func<TEntityLink?, TType> _source;
+        private Func<TEntityLink, TType> _source;
         public String Source { get; }
         public String Target { get; }
 
-        private readonly Func<TLinkedEntity?, TType, Result<TType, string>>? _func;
+        private readonly Func<TLinkedEntity, TType, Result<TType, string>>? _func;
         
         public override string ToString()
         {
@@ -41,9 +41,9 @@ namespace InABox.Core
         }
         
         public LinkedProperty(Expression<Func<TLinkedEntity, TEntityLink>> path, 
-            Expression<Func<TEntityLink?, TType>> source,
-            Expression<Func<TLinkedEntity?, TType>> target,
-            Func<TLinkedEntity?,TType, Result<TType, string>>? func = null)
+            Expression<Func<TEntityLink, TType>> source,
+            Expression<Func<TLinkedEntity, TType>> target,
+            Func<TLinkedEntity,TType, Result<TType, string>>? func = null)
         {
             Path = CoreUtils.GetFullPropertyName(path, ".");
             Source = CoreUtils.GetFullPropertyName(source, ".");
@@ -52,15 +52,15 @@ namespace InABox.Core
             _source = source.Compile();
         }
 
-        public void Update(object? from, object? to)
+        public void Update(object from, object to)
         {
-            if (from != null && to != null)
+            if (from is TEntityLink fromLink && to is TLinkedEntity toEntity)
             {
-                var value = _source.Invoke(from as TEntityLink);
+                var value = _source.Invoke(fromLink);
                 //var value = CoreUtils.GetPropertyValue(from, Source);
                 if (_func != null)
                 {
-                    var result = _func(to as TLinkedEntity, value);
+                    var result = _func(toEntity, value);
                     if (!result.Get(out value, out var _)) return;
                 }
                 CoreUtils.SetPropertyValue(to, Target, value);