EntityLink.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. protected override IOriginalValues CreateOriginalValues()
  37. {
  38. return new SubObjectOriginalValues(this);
  39. }
  40. protected override ILoadedColumns CreateLoadedColumns()
  41. {
  42. return new SubObjectLoadedColumns(this);
  43. }
  44. /*
  45. [Obsolete("Please supply linked Entity")]
  46. public EntityLink()
  47. {
  48. }
  49. public EntityLink(Func<BaseObject>? entity, string name)
  50. {
  51. _linkedentity = entity;
  52. }*/
  53. [NullEditor]
  54. public abstract Guid ID { get; set; }
  55. [NullEditor]
  56. [Obsolete]
  57. public Guid Deleted { get; set; }
  58. /// <summary>
  59. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  60. /// </summary>
  61. /// <param name="other">The link to copy data from.</param>
  62. public void CopyFrom(IEntityLink<T> other)
  63. {
  64. ID = other.ID;
  65. Synchronise(other);
  66. }
  67. /// <summary>
  68. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  69. /// </summary>
  70. /// <param name="other">The link to copy data from.</param>
  71. public void CopyFrom(T other)
  72. {
  73. ID = other.ID;
  74. Synchronise(other);
  75. }
  76. public virtual bool Synchronise(object Entity)
  77. {
  78. var result = false;
  79. foreach (var prop in DatabaseSchema.Properties(GetType()))
  80. {
  81. if (prop.Name == "ID" || !(prop is StandardProperty stdProp) || stdProp.Property.DeclaringType.Equals(typeof(BaseObject))) continue;
  82. var entityProp = DatabaseSchema.Property(Entity.GetType(), prop.Name);
  83. if(entityProp != null)
  84. {
  85. prop.Setter()(this, entityProp.Getter()(Entity));
  86. }
  87. }
  88. foreach (var link in LinkedProperties.Find(this, out var parent))
  89. {
  90. link.Update(this, parent);
  91. result = true;
  92. }
  93. return result;
  94. }
  95. public bool Clear()
  96. {
  97. var result = false;
  98. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);
  99. foreach (var prop in DatabaseSchema.Properties(GetType()))
  100. {
  101. if (prop.Name == "ID" || !(prop is StandardProperty stdProp) || stdProp.Property.DeclaringType.Equals(typeof(BaseObject))) continue;
  102. var entityProp = DatabaseSchema.Property(typeof(T), prop.Name);
  103. if(entityProp != null && entityProp.PropertyType == stdProp.PropertyType)
  104. {
  105. var oldValue = prop.Getter()(this);
  106. var newValue = CoreUtils.GetDefault(prop.PropertyType);
  107. if(!object.Equals(oldValue, newValue))
  108. {
  109. result = true;
  110. prop.Setter()(this, newValue);
  111. }
  112. }
  113. }
  114. return result;
  115. }
  116. protected override void DoPropertyChanged(string name, object? before, object? after)
  117. {
  118. LinkedProperties.GetParent(this)?.CascadePropertyChanged(LinkedProperties.GetPath(this) + "." + name, before, after);
  119. if (LinkedProperties.Find(this, name, out var link, out var parent))
  120. {
  121. link.Update(this, parent);
  122. }
  123. }
  124. public bool IsValid() => !ID.Equals(Guid.Empty);
  125. }
  126. }