123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using Expressive;
- using System;
- using System.Dynamic;
- using System.Linq;
- namespace InABox.Core
- {
- public interface IEntityLink : ISubObject
- {
- Guid ID { get; set; }
-
- Guid Deleted { get; set; }
- bool Synchronise(object entity);
- bool Clear();
- bool IsValid();
- }
- public interface IEntityLink<T> : IEntityLink
- {
- }
- public abstract class EntityLink<T> : BaseObject, IEntityLink<T> where T : Entity, new()
- {
- /*
- private Func<BaseObject>? _linkedentity;
-
- [DoNotSerialize]
- protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();*/
- private BaseObject _linkedParent;
- private string _linkedPath;
- public void SetLinkedParent(BaseObject parent)
- {
- _linkedParent = parent;
- }
- public void SetLinkedPath(string path)
- {
- _linkedPath = path;
- }
- public BaseObject GetLinkedParent() => _linkedParent;
- public string GetLinkedPath() => _linkedPath;
- /*
- [Obsolete("Please supply linked Entity")]
- public EntityLink()
- {
- }
- public EntityLink(Func<BaseObject>? entity, string name)
- {
- _linkedentity = entity;
- }*/
- [NullEditor]
- public abstract Guid ID { get; set; }
-
- [NullEditor]
- [Obsolete]
- public Guid Deleted { get; set; }
- /// <summary>
- /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
- /// </summary>
- /// <param name="other">The link to copy data from.</param>
- public void CopyFrom(IEntityLink<T> other)
- {
- ID = other.ID;
- Synchronise(other);
- }
- /// <summary>
- /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
- /// </summary>
- /// <param name="other">The link to copy data from.</param>
- public void CopyFrom(T other)
- {
- ID = other.ID;
- Synchronise(other);
- }
- public virtual bool Synchronise(object Entity)
- {
- 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)
- {
- if (CoreUtils.HasProperty(Entity.GetType(), key))
- {
- 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));
- }
- }
- }
- }
- }
- }
- foreach (var link in LinkedProperties.Find(this, out var parent))
- {
- link.Update(this, parent);
- result = true;
- }
- return result;
- }
- public bool Clear()
- {
- var result = false;
- var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
- foreach (var key in props.Keys)
- {
- var prop = CoreUtils.GetProperty(typeof(T), key);
- if (prop != null && prop.PropertyType.Equals(props[key]))
- {
- var oldvalue = CoreUtils.GetPropertyValue(this, key);
- var newvalue = prop.PropertyType.Equals(typeof(string)) ? "" : Activator.CreateInstance(prop.PropertyType);
- if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
- {
- result = true;
- CoreUtils.SetPropertyValue(this, key, newvalue);
- }
- }
- }
- return result;
- }
- protected override void DoPropertyChanged(string name, object? before, object? after)
- {
- if (IsObserving())
- {
- if(LinkedProperties.Find(this, name, out var link, out var parent))
- {
- link.Update(this, parent);
- }
- }
- }
- public bool IsValid() => !ID.Equals(Guid.Empty) && Deleted.Equals(Guid.Empty);
- }
- }
|