EntityLink.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using Expressive;
  2. using System;
  3. using System.Dynamic;
  4. using System.Linq;
  5. namespace InABox.Core
  6. {
  7. public interface IEntityLink : ISubObject
  8. {
  9. Guid ID { get; set; }
  10. Guid Deleted { get; set; }
  11. bool Synchronise(object entity);
  12. bool Clear();
  13. bool IsValid();
  14. }
  15. public interface IEntityLink<T> : IEntityLink
  16. {
  17. }
  18. public abstract class EntityLink<T> : BaseObject, IEntityLink<T> where T : BaseObject, new()
  19. {
  20. /*
  21. private Func<BaseObject>? _linkedentity;
  22. [DoNotSerialize]
  23. protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();*/
  24. private BaseObject _linkedParent;
  25. private string _linkedPath;
  26. public void SetLinkedParent(BaseObject parent)
  27. {
  28. _linkedParent = parent;
  29. }
  30. public void SetLinkedPath(string path)
  31. {
  32. _linkedPath = path;
  33. }
  34. public BaseObject GetLinkedParent() => _linkedParent;
  35. public string GetLinkedPath() => _linkedPath;
  36. /*
  37. [Obsolete("Please supply linked Entity")]
  38. public EntityLink()
  39. {
  40. }
  41. public EntityLink(Func<BaseObject>? entity, string name)
  42. {
  43. _linkedentity = entity;
  44. }*/
  45. [NullEditor]
  46. public abstract Guid ID { get; set; }
  47. [NullEditor]
  48. [Obsolete]
  49. public Guid Deleted { get; set; }
  50. /// <summary>
  51. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  52. /// </summary>
  53. /// <param name="other">The link to copy data from.</param>
  54. public void CopyFrom(IEntityLink<T> other)
  55. {
  56. ID = other.ID;
  57. Synchronise(other);
  58. }
  59. public virtual bool Synchronise(object Entity)
  60. {
  61. var result = false;
  62. var props = CoreUtils.PropertyList(GetType(), x => (!x.Name.Equals("ID") || Entity == null) && !x.DeclaringType.Equals(typeof(BaseObject)), false);
  63. foreach (var key in props.Keys)
  64. {
  65. if (CoreUtils.HasProperty(Entity.GetType(), key))
  66. {
  67. var prop = CoreUtils.GetProperty(typeof(T), key);
  68. if (prop != null && prop.PropertyType.Equals(props[key]))
  69. {
  70. if (prop.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
  71. {
  72. var tgtlink = CoreUtils.GetPropertyValue(this, key) as IEntityLink;
  73. var srclink = CoreUtils.GetPropertyValue(Entity, key) as IEntityLink;
  74. tgtlink.ID = srclink.ID;
  75. tgtlink.Synchronise(srclink);
  76. }
  77. else
  78. {
  79. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  80. var newvalue = CoreUtils.GetPropertyValue(Entity, key);
  81. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  82. {
  83. result = true;
  84. try
  85. {
  86. CoreUtils.SetPropertyValue(this, key, newvalue);
  87. }
  88. catch (Exception e)
  89. {
  90. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. foreach (var link in LinkedProperties.Find(this, out var parent))
  98. {
  99. link.Update(this, parent);
  100. result = true;
  101. }
  102. return result;
  103. }
  104. public bool Clear()
  105. {
  106. var result = false;
  107. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
  108. foreach (var key in props.Keys)
  109. {
  110. var prop = CoreUtils.GetProperty(typeof(T), key);
  111. if (prop != null && prop.PropertyType.Equals(props[key]))
  112. {
  113. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  114. var newvalue = prop.PropertyType.Equals(typeof(string)) ? "" : Activator.CreateInstance(prop.PropertyType);
  115. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  116. {
  117. result = true;
  118. CoreUtils.SetPropertyValue(this, key, newvalue);
  119. }
  120. }
  121. }
  122. return result;
  123. }
  124. protected override void DoPropertyChanged(string name, object? before, object? after)
  125. {
  126. if (IsObserving())
  127. {
  128. if(LinkedProperties.Find(this, name, out var link, out var parent))
  129. {
  130. link.Update(this, parent);
  131. }
  132. }
  133. }
  134. public bool IsValid() => !ID.Equals(Guid.Empty) && Deleted.Equals(Guid.Empty);
  135. }
  136. }