EntityLink.cs 4.6 KB

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