Aggregate.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace InABox.Core
  6. {
  7. public static class AggregateUtils
  8. {
  9. private static string RemoveConvert(Expression expression)
  10. {
  11. // We were running a ToString on expression and removing the convert using string functions, however this failed when
  12. // .NET has a different string representation for .NET 6.0;
  13. // Compare "Login => Convert(Login.User.ID)" and "Login => Convert(Login.User.ID, Object)"
  14. if (expression is LambdaExpression lambda)
  15. {
  16. var body = lambda.Body;
  17. if (body is UnaryExpression unary && body.NodeType == ExpressionType.Convert)
  18. {
  19. var operand = unary.Operand;
  20. return operand.ToString();
  21. }
  22. return body.ToString();
  23. }
  24. // Probably not, but it is for now
  25. return expression.ToString();
  26. //String result = expression.ToString().Split(new String[] { "=>" }, StringSplitOptions.RemoveEmptyEntries).Last().Trim();
  27. //if (result.ToUpper().StartsWith("CONVERT("))
  28. // result = result.Split('(', ')')[1];
  29. //return result;
  30. }
  31. private static string ProcessConstantExpression(Expression expression)
  32. {
  33. var result = expression.ToString();
  34. return result;
  35. }
  36. public static string ProcessExpression(Expression expr)
  37. {
  38. if (expr.NodeType == ExpressionType.Convert)
  39. expr = ((UnaryExpression)expr).Operand;
  40. //if (expr.NodeType == ExpressionType.MemberAccess)
  41. //{
  42. // var result = Expression.Lambda(expr).Compile().DynamicInvoke();
  43. // return result == null ? "null" : result.ToString();
  44. //}
  45. if (expr is ConstantExpression) return ProcessConstantExpression(expr);
  46. if (expr is MemberExpression && ((MemberExpression)expr).Expression == null)
  47. {
  48. var result = Expression.Lambda(expr).Compile().DynamicInvoke();
  49. return expr.Type.IsDefault(result) ? "NULL" : expr.Type == typeof(string) ? string.Format("\"{0}\"", result) : result.ToString();
  50. }
  51. return string.Join(".", RemoveConvert(expr).Split('.').Skip(1));
  52. }
  53. }
  54. #region Aggregates
  55. public enum AggregateCalculation
  56. {
  57. None,
  58. Sum,
  59. Count,
  60. Maximum,
  61. Minimum,
  62. Average,
  63. Concat
  64. }
  65. public interface ICoreAggregate<TType, TProp>
  66. {
  67. Expression<Func<TType, TProp>> Aggregate { get; }
  68. AggregateCalculation Calculation { get; }
  69. }
  70. public abstract class CoreAggregate<TType, TProp> : ICoreAggregate<TType, TProp>
  71. {
  72. public abstract Expression<Func<TType, TProp>> Aggregate { get; }
  73. public abstract AggregateCalculation Calculation { get; }
  74. public string GetAggregate()
  75. {
  76. return string.Join(".", Aggregate.ToString().Split('.').Skip(1));
  77. }
  78. }
  79. public interface ICoreAggregate<TMaster, TDetail, TProp>
  80. {
  81. Expression<Func<TDetail, TProp>> Aggregate { get; }
  82. Filter<TDetail>? Filter { get; }
  83. Dictionary<Expression<Func<TDetail, object>>, Expression<Func<TMaster, object>>> Links { get; }
  84. AggregateCalculation Calculation { get; }
  85. Dictionary<string, string> GetLinks();
  86. }
  87. public abstract class CoreAggregate<TMaster, TDetail, TProp> : ICoreAggregate<TMaster, TDetail, TProp>
  88. {
  89. public abstract Expression<Func<TDetail, TProp>> Aggregate { get; }
  90. public virtual Filter<TDetail>? Filter => null;
  91. public abstract Dictionary<Expression<Func<TDetail, object>>, Expression<Func<TMaster, object>>> Links { get; }
  92. public Dictionary<string, string> GetLinks()
  93. {
  94. var result = new Dictionary<string, string>();
  95. foreach (var link in Links)
  96. {
  97. var childkey = AggregateUtils.ProcessExpression(link.Key); // String.Join(".", link.Key.ToString().Split('.').Skip(1));
  98. var parentkey = AggregateUtils.ProcessExpression(link.Value); // String.Join(".", link.Value.ToString().Split('.').Skip(1);
  99. result[childkey] = parentkey;
  100. }
  101. return result;
  102. }
  103. public abstract AggregateCalculation Calculation { get; }
  104. public string GetAggregate()
  105. {
  106. return string.Join(".", Aggregate.ToString().Split('.').Skip(1));
  107. }
  108. }
  109. public class AggregateAttribute : Attribute
  110. {
  111. public AggregateAttribute(Type calculator)
  112. {
  113. Calculator = Activator.CreateInstance(calculator);
  114. }
  115. public object Calculator { get; }
  116. public Type Source => GetSource();
  117. public AggregateCalculation Calculation => GetCalculation();
  118. public string Aggregate => GetAggregate();
  119. public Dictionary<string, string> Links => GetLinks();
  120. public IFilter? Filter => GetFilter();
  121. #region Internal (Reflection) functions
  122. private Type GetSource()
  123. {
  124. var intf = Calculator.GetType().GetInterfaces()
  125. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  126. if (intf != null)
  127. return intf.GenericTypeArguments[1];
  128. intf = Calculator.GetType().GetInterfaces()
  129. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,>));
  130. if (intf != null)
  131. return intf.GenericTypeArguments[0];
  132. throw new Exception("Unable to Locate Type Information for Aggregate");
  133. }
  134. private string GetAggregate()
  135. {
  136. var intf = Calculator.GetType().GetInterfaces()
  137. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  138. if (intf == null)
  139. {
  140. intf = Calculator.GetType().GetInterfaces()
  141. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,>));
  142. }
  143. if (intf != null)
  144. {
  145. var prop = intf.GetProperty("Aggregate");
  146. if (prop != null)
  147. {
  148. var obj = prop.GetValue(Calculator);
  149. if (obj != null)
  150. {
  151. var expr = obj as Expression;
  152. if (expr != null) return AggregateUtils.ProcessExpression(expr);
  153. }
  154. }
  155. }
  156. return "";
  157. }
  158. private Dictionary<string, string> GetLinks()
  159. {
  160. var intf = Calculator.GetType().GetInterfaces()
  161. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  162. if (intf != null)
  163. {
  164. var method = intf.GetMethod("GetLinks");
  165. if (method != null)
  166. {
  167. var dict = method.Invoke(Calculator, new object[] { });
  168. return (dict as Dictionary<string, string>)!;
  169. }
  170. }
  171. return new Dictionary<string, string>();
  172. }
  173. private AggregateCalculation GetCalculation()
  174. {
  175. var intf = Calculator.GetType().GetInterfaces()
  176. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  177. if (intf == null)
  178. intf = Calculator.GetType().GetInterfaces()
  179. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,>));
  180. if (intf != null)
  181. {
  182. var prop = intf.GetProperty("Calculation");
  183. if (prop != null)
  184. return (AggregateCalculation)prop.GetValue(Calculator);
  185. }
  186. return AggregateCalculation.None;
  187. }
  188. private IFilter? GetFilter()
  189. {
  190. var intf = Calculator.GetType().GetInterfaces()
  191. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  192. if (intf != null)
  193. {
  194. var prop = intf.GetProperty("Filter");
  195. if (prop != null)
  196. return prop.GetValue(Calculator) as IFilter;
  197. }
  198. return null;
  199. }
  200. #endregion
  201. }
  202. #endregion
  203. #region Formulas
  204. public enum FormulaType
  205. {
  206. Virtual,
  207. Permanent
  208. }
  209. public enum FormulaOperator
  210. {
  211. None,
  212. Add,
  213. Subtract,
  214. Multiply,
  215. Divide,
  216. Minumum,
  217. Maximum,
  218. Constant
  219. }
  220. public interface IFormula<TType, TProp>
  221. {
  222. Expression<Func<TType, TProp>> Value { get; }
  223. Expression<Func<TType, TProp>>[] Modifiers { get; }
  224. FormulaOperator Operator { get; }
  225. FormulaType Type { get; }
  226. }
  227. public interface IFormula
  228. {
  229. String Value { get; }
  230. String[] Modifiers { get; }
  231. FormulaOperator Operator { get; }
  232. FormulaType Type { get; }
  233. }
  234. public class FormulaAttribute : Attribute, IFormula
  235. {
  236. public FormulaAttribute(Type calculator)
  237. {
  238. Calculator = Activator.CreateInstance(calculator);
  239. }
  240. public object Calculator { get; }
  241. public string Value => GetExpressionName("Value");
  242. public string[] Modifiers => GetExpressionNames("Modifiers");
  243. public FormulaOperator Operator => GetFormulaOperator();
  244. public FormulaType Type => GetFormulaType();
  245. #region Internal (Reflection) functions
  246. private FormulaOperator GetFormulaOperator()
  247. {
  248. var intf = Calculator.GetType().GetInterfaces()
  249. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  250. if (intf != null)
  251. {
  252. var prop = intf.GetProperty("Operator");
  253. if (prop != null)
  254. return (FormulaOperator)prop.GetValue(Calculator);
  255. }
  256. return FormulaOperator.None;
  257. }
  258. private FormulaType GetFormulaType()
  259. {
  260. var intf = Calculator.GetType().GetInterfaces()
  261. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  262. if (intf != null)
  263. {
  264. var prop = intf.GetProperty("Type");
  265. if (prop != null)
  266. return (FormulaType)prop.GetValue(Calculator);
  267. }
  268. return FormulaType.Virtual;
  269. }
  270. private string GetExpressionName(string property)
  271. {
  272. var intf = Calculator.GetType().GetInterfaces()
  273. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  274. if (intf != null)
  275. {
  276. var prop = intf.GetProperty(property);
  277. if (prop != null)
  278. {
  279. if(prop.GetValue(Calculator) is LambdaExpression expr)
  280. {
  281. var result = AggregateUtils.ProcessExpression(expr.Body);
  282. return result;
  283. }
  284. }
  285. }
  286. return "";
  287. }
  288. private string[] GetExpressionNames(string property)
  289. {
  290. var intf = Calculator.GetType().GetInterfaces()
  291. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  292. if (intf != null)
  293. {
  294. var prop = intf.GetProperty(property);
  295. if (prop != null)
  296. {
  297. if(prop.GetValue(Calculator) is LambdaExpression[] expressions)
  298. {
  299. var result = new List<string>();
  300. foreach (var expression in expressions)
  301. result.Add(AggregateUtils.ProcessExpression(expression.Body));
  302. return result.ToArray();
  303. }
  304. //return expressions.Select(x => x.Body is ConstantExpression ? ProcessConstantExpression(x.Body) : String.Join(".", RemoveConvert(x.Body).Split('.').Skip(1))).ToArray();
  305. }
  306. }
  307. return new string[] { };
  308. }
  309. #endregion
  310. }
  311. #endregion
  312. #region Conditions
  313. public enum Condition
  314. {
  315. None,
  316. Equals,
  317. NotEqual,
  318. GreaterThan,
  319. GreaterThanOrEqualTo,
  320. LessThan,
  321. LessThanOrEqualTo
  322. }
  323. public enum ConditionType
  324. {
  325. Virtual,
  326. Permanent
  327. }
  328. public interface ICondition<TType, TProp, TValue>
  329. {
  330. Expression<Func<TType, TProp>> Left { get; }
  331. Condition Condition { get; }
  332. Expression<Func<TType, TProp>> Right { get; }
  333. Expression<Func<TType, TValue>> True { get; }
  334. Expression<Func<TType, TValue>> False { get; }
  335. ConditionType Type { get; }
  336. }
  337. public class ConditionAttribute : Attribute
  338. {
  339. public ConditionAttribute(Type calculator)
  340. {
  341. Calculator = Activator.CreateInstance(calculator);
  342. }
  343. public object Calculator { get; }
  344. public string Left => GetExpressionName("Left");
  345. public Condition Condition => GetCondition();
  346. public string Right => GetExpressionName("Right");
  347. public string True => GetExpressionName("True");
  348. public string False => GetExpressionName("False");
  349. public ConditionType Type => GetConditionType();
  350. #region Internal (Reflection) functions
  351. private Condition GetCondition()
  352. {
  353. var intf = Calculator.GetType().GetInterfaces()
  354. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  355. if (intf != null)
  356. {
  357. var prop = intf.GetProperty("Condition");
  358. if (prop != null)
  359. return (Condition)prop.GetValue(Calculator);
  360. }
  361. return Condition.None;
  362. }
  363. private ConditionType GetConditionType()
  364. {
  365. var intf = Calculator.GetType().GetInterfaces()
  366. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  367. if (intf != null)
  368. {
  369. var prop = intf.GetProperty("Type");
  370. if (prop != null)
  371. return (ConditionType)prop.GetValue(Calculator);
  372. }
  373. return ConditionType.Virtual;
  374. }
  375. private string GetExpressionName(string property)
  376. {
  377. var intf = Calculator.GetType().GetInterfaces()
  378. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  379. if (intf != null)
  380. {
  381. var prop = intf.GetProperty(property);
  382. if (prop?.GetValue(Calculator) is LambdaExpression expr)
  383. {
  384. return AggregateUtils.ProcessExpression(expr.Body);
  385. }
  386. }
  387. return "";
  388. }
  389. #endregion
  390. }
  391. #endregion
  392. #region ChildEntity
  393. public interface IChildEntityDefinition
  394. {
  395. string ParentColumn { get; }
  396. Type EntityType { get; }
  397. IFilter? Filter { get; }
  398. ISortOrder? Sort { get; }
  399. }
  400. public interface IChildEntityDefinition<TEntity> : IChildEntityDefinition
  401. where TEntity : Entity
  402. {
  403. new Filter<TEntity>? Filter { get; }
  404. new SortOrder<TEntity>? Sort { get; }
  405. Expression<Func<TEntity, Guid>> Parent { get; }
  406. Type IChildEntityDefinition.EntityType => typeof(TEntity);
  407. IFilter? IChildEntityDefinition.Filter => Filter;
  408. ISortOrder? IChildEntityDefinition.Sort => Sort;
  409. string IChildEntityDefinition.ParentColumn => CoreUtils.GetFullPropertyName(Parent, ".");
  410. }
  411. public class ChildEntityAttribute : Attribute
  412. {
  413. public IChildEntityDefinition Calculator { get; set; }
  414. public ChildEntityAttribute(Type definition)
  415. {
  416. Calculator = (Activator.CreateInstance(definition) as IChildEntityDefinition)
  417. ?? throw new Exception($"{definition} is not an {nameof(IChildEntityDefinition)}");
  418. }
  419. }
  420. #endregion
  421. }