EntityLink.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 Clear();
  12. bool IsValid();
  13. }
  14. public interface IEntityLink<T> : IEntityLink
  15. {
  16. }
  17. public abstract class EntityLink<T> : BaseObject, IEntityLink<T> where T : Entity, new()
  18. {
  19. /*
  20. private Func<BaseObject>? _linkedentity;
  21. [DoNotSerialize]
  22. protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();*/
  23. private BaseObject? _linkedParent;
  24. private string? _linkedPath;
  25. public void SetLinkedParent(BaseObject parent)
  26. {
  27. _linkedParent = parent;
  28. }
  29. public void SetLinkedPath(string path)
  30. {
  31. _linkedPath = path;
  32. }
  33. public BaseObject? GetLinkedParent() => _linkedParent;
  34. public string? GetLinkedPath() => _linkedPath;
  35. protected override IOriginalValues CreateOriginalValues()
  36. {
  37. return new SubObjectOriginalValues(this);
  38. }
  39. protected override ILoadedColumns CreateLoadedColumns()
  40. {
  41. return new SubObjectLoadedColumns(this);
  42. }
  43. /*
  44. [Obsolete("Please supply linked Entity")]
  45. public EntityLink()
  46. {
  47. }
  48. public EntityLink(Func<BaseObject>? entity, string name)
  49. {
  50. _linkedentity = entity;
  51. }*/
  52. [NullEditor]
  53. public abstract Guid ID { get; set; }
  54. [NullEditor]
  55. [Obsolete]
  56. public Guid Deleted { get; set; }
  57. /// <summary>
  58. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  59. /// </summary>
  60. /// <param name="other">The link to copy data from.</param>
  61. public void CopyFrom(IEntityLink<T> other)
  62. {
  63. ID = other.ID;
  64. this.Synchronise(other);
  65. }
  66. /// <summary>
  67. /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
  68. /// </summary>
  69. /// <param name="other">The link to copy data from.</param>
  70. public void CopyFrom(T other)
  71. {
  72. ID = other.ID;
  73. this.Synchronise(other);
  74. }
  75. public bool Clear()
  76. {
  77. var result = false;
  78. var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), 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(typeof(T), prop.Name);
  83. if(entityProp != null && entityProp.PropertyType == stdProp.PropertyType)
  84. {
  85. var oldValue = prop.Getter()(this);
  86. var newValue = CoreUtils.GetDefault(prop.PropertyType);
  87. if(!object.Equals(oldValue, newValue))
  88. {
  89. result = true;
  90. prop.Setter()(this, newValue);
  91. }
  92. }
  93. }
  94. return result;
  95. }
  96. protected override void DoPropertyChanged(string name, object? before, object? after)
  97. {
  98. LinkedProperties.GetParent(this)?.CascadePropertyChanged(LinkedProperties.GetPath(this) + "." + name, before, after);
  99. if (LinkedProperties.Find(this, name, out var link, out var parent))
  100. {
  101. link.Update(this, parent);
  102. }
  103. }
  104. public bool IsValid() => !ID.Equals(Guid.Empty);
  105. }
  106. }