StandardProperty.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 IsPersistable(StandardProperty? prop)
  126. {
  127. if (prop == null)
  128. return true;
  129. if (prop.Property.GetCustomAttribute<DoNotPersist>() != null)
  130. return false;
  131. return IsPersistable(prop.Parent as StandardProperty);
  132. }
  133. private bool? _dbColumn;
  134. public bool IsDBColumn
  135. {
  136. get
  137. {
  138. if (!_dbColumn.HasValue)
  139. {
  140. _dbColumn = !IsCalculated
  141. && IsPersistable(this)
  142. && Property.CanWrite;
  143. }
  144. return _dbColumn.Value;
  145. }
  146. }
  147. public TAttribute? GetAttribute<TAttribute>() where TAttribute : Attribute
  148. {
  149. return Property.GetCustomAttribute<TAttribute>();
  150. }
  151. public Expression Expression()
  152. {
  153. return CoreUtils.CreateMemberExpression(_class, Name);
  154. }
  155. public Func<object, object> Getter()
  156. {
  157. if (_getter is null) CheckExpressions();
  158. return _getter;
  159. }
  160. public Action<object, object?> Setter()
  161. {
  162. if (_setter is null) CheckExpressions();
  163. return _setter;
  164. }
  165. public decimal PropertySequence() => _propertySequence;
  166. private decimal CalculatePropertySequence()
  167. {
  168. var sequence = 0.0M;
  169. IProperty? property = this;
  170. while(property != null)
  171. {
  172. sequence = property.Sequence + sequence / 1000.0M;
  173. property = property.Parent;
  174. }
  175. return sequence;
  176. }
  177. public override string ToString()
  178. {
  179. return string.Format("{0}.{1}", Class, Name);
  180. }
  181. private void ClearExpressions()
  182. {
  183. _checkedExpressions = false;
  184. _getter = null;
  185. _setter = null;
  186. }
  187. private void CheckExpressions()
  188. {
  189. if(_checkedExpressions) return;
  190. if (_class == null)
  191. return;
  192. if (string.IsNullOrEmpty(_name))
  193. return;
  194. try
  195. {
  196. _getter = Expressions.Getter(_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. try
  203. {
  204. _setter = Expressions.Setter(_class, _name);
  205. }
  206. catch (Exception e)
  207. {
  208. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  209. }
  210. _checkedExpressions = true;
  211. }
  212. }
  213. }