EntityLink.cs 3.6 KB

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