StandardProperty.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. namespace InABox.Core
  6. {
  7. public class StandardProperty : IProperty
  8. {
  9. public Type _class;
  10. private Func<object, object> _getter;
  11. private string _name = "";
  12. private Action<object, object?> _setter;
  13. private bool _checkedExpressions;
  14. public string _type = "";
  15. public Type _propertyType = typeof(object);
  16. public StandardProperty()
  17. {
  18. Editor = new NullEditor();
  19. HasEditor = false;
  20. }
  21. public string Class
  22. {
  23. get => _class.EntityName();
  24. set
  25. {
  26. _class = CoreUtils.GetEntity(value);
  27. ClearExpressions();
  28. }
  29. }
  30. public Type? ClassType
  31. {
  32. get => _class;
  33. set
  34. {
  35. _class = value ?? typeof(object);
  36. }
  37. }
  38. public string Name
  39. {
  40. get => _name;
  41. set
  42. {
  43. _name = value;
  44. ClearExpressions();
  45. }
  46. }
  47. public string Type
  48. {
  49. get => _type;
  50. set
  51. {
  52. Type propType;
  53. try
  54. {
  55. propType = CoreUtils.GetEntityOrNull(_type) ?? typeof(object);
  56. }
  57. catch
  58. {
  59. propType = typeof(object);
  60. }
  61. PropertyType = propType; // Invoke other setter to ensure consistency of functionality
  62. }
  63. }
  64. public Type PropertyType
  65. {
  66. get => _propertyType;
  67. set
  68. {
  69. _propertyType = value;
  70. _type = value.EntityName();
  71. IsEntityLink = _propertyType.HasInterface(typeof(IEntityLink));
  72. IsEnclosedEntity = _propertyType.HasInterface(typeof(IEnclosedEntity));
  73. IsParent = IsEnclosedEntity || IsEntityLink;
  74. }
  75. }
  76. public PropertyInfo Property { get; set; }
  77. //[ComboLookupEditor(typeof(PropertyEditorLookups))]
  78. public BaseEditor Editor { get; set; }
  79. public bool HasEditor { get; set; }
  80. public string Caption { get; set; }
  81. private decimal _propertySequence;
  82. private long _sequence;
  83. public long Sequence
  84. {
  85. get => _sequence;
  86. set
  87. {
  88. _sequence = value;
  89. _propertySequence = CalculatePropertySequence();
  90. }
  91. }
  92. [NullEditor]
  93. public string Page { get; set; }
  94. public bool Required { get; set; }
  95. public LoggablePropertyAttribute? Loggable { get; set; }
  96. private IProperty? _parent;
  97. public IProperty? Parent
  98. {
  99. get => _parent;
  100. set
  101. {
  102. _parent = value;
  103. _propertySequence = CalculatePropertySequence();
  104. }
  105. }
  106. public bool IsEntityLink { get; set; }
  107. public bool IsEnclosedEntity { get; set; }
  108. public bool IsParent { get; set; }
  109. private bool? _calculated;
  110. public bool IsCalculated
  111. {
  112. get
  113. {
  114. if (!_calculated.HasValue)
  115. {
  116. _calculated = Property.GetCustomAttribute<AggregateAttribute>() != null
  117. || Property.GetCustomAttribute<FormulaAttribute>() != null
  118. || Property.GetCustomAttribute<ConditionAttribute>() != null
  119. || Property.GetCustomAttribute<ChildEntityAttribute>() != null
  120. || this.GetParent(x => x.IsCalculated) != null;
  121. }
  122. return _calculated.Value;
  123. }
  124. }
  125. private bool? _dbColumn;
  126. public bool IsDBColumn
  127. {
  128. get
  129. {
  130. if (!_dbColumn.HasValue)
  131. {
  132. _dbColumn = !IsCalculated
  133. && Property.GetCustomAttribute<DoNotPersist>() == null
  134. && Property.CanWrite;
  135. }
  136. return _dbColumn.Value;
  137. }
  138. }
  139. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  140. {
  141. return Property.GetCustomAttribute<TAttribute>();
  142. }
  143. public Expression Expression()
  144. {
  145. return CoreUtils.CreateMemberExpression(_class, Name);
  146. }
  147. public Func<object, object> Getter()
  148. {
  149. if (_getter is null) CheckExpressions();
  150. return _getter;
  151. }
  152. public Action<object, object?> Setter()
  153. {
  154. if (_setter is null) CheckExpressions();
  155. return _setter;
  156. }
  157. public decimal PropertySequence() => _propertySequence;
  158. private decimal CalculatePropertySequence()
  159. {
  160. var sequence = 0.0M;
  161. IProperty? property = this;
  162. while(property != null)
  163. {
  164. sequence = property.Sequence + sequence / 1000.0M;
  165. property = property.Parent;
  166. }
  167. return sequence;
  168. }
  169. public override string ToString()
  170. {
  171. return string.Format("{0}.{1}", Class, Name);
  172. }
  173. private void ClearExpressions()
  174. {
  175. _checkedExpressions = false;
  176. _getter = null;
  177. _setter = null;
  178. }
  179. private void CheckExpressions()
  180. {
  181. if(_checkedExpressions) return;
  182. if (_class == null)
  183. return;
  184. if (string.IsNullOrEmpty(_name))
  185. return;
  186. try
  187. {
  188. _getter = Expressions.Getter(_class, _name);
  189. }
  190. catch (Exception e)
  191. {
  192. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  193. }
  194. try
  195. {
  196. _setter = Expressions.Setter(_class, _name);
  197. }
  198. catch (Exception e)
  199. {
  200. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  201. }
  202. _checkedExpressions = true;
  203. }
  204. }
  205. }