EntityLink.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 : Entity, 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. /// <summary>
  60. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  61. /// </summary>
  62. /// <param name="other">The link to copy data from.</param>
  63. public void CopyFrom(T other)
  64. {
  65. ID = other.ID;
  66. Synchronise(other);
  67. }
  68. public virtual bool Synchronise(object Entity)
  69. {
  70. var result = false;
  71. foreach (var prop in DatabaseSchema.Properties(GetType()))
  72. {
  73. if (prop.Name == "ID" || !(prop is StandardProperty stdProp) || stdProp.Property.DeclaringType.Equals(typeof(BaseObject))) continue;
  74. var entityProp = DatabaseSchema.Property(Entity.GetType(), prop.Name);
  75. if(entityProp != null)
  76. {
  77. prop.Setter()(this, entityProp.Getter()(Entity));
  78. }
  79. }
  80. foreach (var link in LinkedProperties.Find(this, out var parent))
  81. {
  82. link.Update(this, parent);
  83. result = true;
  84. }
  85. return result;
  86. }
  87. public bool Clear()
  88. {
  89. var result = false;
  90. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
  91. foreach (var prop in DatabaseSchema.Properties(GetType()))
  92. {
  93. if (prop.Name == "ID" || !(prop is StandardProperty stdProp) || stdProp.Property.DeclaringType.Equals(typeof(BaseObject))) continue;
  94. var entityProp = DatabaseSchema.Property(typeof(T), prop.Name);
  95. if(entityProp != null && entityProp.PropertyType == stdProp.PropertyType)
  96. {
  97. var oldValue = prop.Getter()(this);
  98. var newValue = CoreUtils.GetDefault(prop.PropertyType);
  99. if(!object.Equals(oldValue, newValue))
  100. {
  101. result = true;
  102. prop.Setter()(this, newValue);
  103. }
  104. }
  105. }
  106. return result;
  107. }
  108. protected override void DoPropertyChanged(string name, object? before, object? after)
  109. {
  110. if (IsObserving())
  111. {
  112. if(LinkedProperties.Find(this, name, out var link, out var parent))
  113. {
  114. link.Update(this, parent);
  115. }
  116. }
  117. }
  118. public bool IsValid() => !ID.Equals(Guid.Empty) && Deleted.Equals(Guid.Empty);
  119. }
  120. }