EntityLink.cs 4.0 KB

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