EntityLink.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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<Entity>? _linkedentity;
  20. [DoNotSerialize]
  21. protected Entity? LinkedEntity() => _linkedentity?.Invoke();
  22. //[Obsolete("Please supply linked Entity")]
  23. public EntityLink()
  24. {
  25. }
  26. public EntityLink(Func<Entity>? 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(),
  38. x => (!x.Name.Equals("ID") || Entity == null) && !x.DeclaringType.Equals(typeof(BaseObject)), false);
  39. foreach (var key in props.Keys)
  40. {
  41. if (CoreUtils.HasProperty(Entity.GetType(), key))
  42. {
  43. var prop = CoreUtils.GetProperty(typeof(T), key);
  44. if (prop != null && prop.PropertyType.Equals(props[key]) && !prop.PropertyType.GetInterfaces().Any(x => x == typeof(IEntityLink)))
  45. {
  46. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  47. var newvalue = CoreUtils.GetPropertyValue(Entity, key);
  48. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  49. {
  50. result = true;
  51. try
  52. {
  53. CoreUtils.SetPropertyValue(this, key, newvalue);
  54. }
  55. catch (Exception e)
  56. {
  57. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  58. }
  59. }
  60. }
  61. }
  62. }
  63. var le = LinkedEntity();
  64. if (le != null)
  65. foreach (var link in LinkedProperties.Find(le,this))
  66. {
  67. link.Update(this, le);
  68. result = true;
  69. }
  70. return result;
  71. }
  72. public bool Clear()
  73. {
  74. var result = false;
  75. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
  76. foreach (var key in props.Keys)
  77. {
  78. var prop = CoreUtils.GetProperty(typeof(T), key);
  79. if (prop != null && prop.PropertyType.Equals(props[key]))
  80. {
  81. var oldvalue = CoreUtils.GetPropertyValue(this, key);
  82. var newvalue = prop.PropertyType.Equals(typeof(string)) ? "" : Activator.CreateInstance(prop.PropertyType);
  83. if ((oldvalue == null && newvalue != null) || (oldvalue != null && !oldvalue.Equals(newvalue)))
  84. {
  85. result = true;
  86. CoreUtils.SetPropertyValue(this, key, newvalue);
  87. }
  88. }
  89. }
  90. return result;
  91. }
  92. protected override void DoPropertyChanged(string name, object before, object after)
  93. {
  94. var le = LinkedEntity();
  95. if (IsObserving() && le != null)
  96. {
  97. var link = LinkedProperties.Find(le, this).FirstOrDefault(x => x.Source.Equals(name));
  98. if (link != null)
  99. link.Update(this, le);
  100. }
  101. }
  102. public bool IsValid() => !ID.Equals(Guid.Empty) && Deleted.Equals(Guid.Empty);
  103. }
  104. }