Aggregate.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. }
  64. public interface ICoreAggregate<TType, TProp>
  65. {
  66. Expression<Func<TType, TProp>> Aggregate { get; }
  67. AggregateCalculation Calculation { get; }
  68. }
  69. public abstract class CoreAggregate<TType, TProp> : ICoreAggregate<TType, TProp>
  70. {
  71. public abstract Expression<Func<TType, TProp>> Aggregate { get; }
  72. public abstract AggregateCalculation Calculation { get; }
  73. public string GetAggregate()
  74. {
  75. return string.Join(".", Aggregate.ToString().Split('.').Skip(1));
  76. }
  77. }
  78. public interface ICoreAggregate<TMaster, TDetail, TProp>
  79. {
  80. Expression<Func<TDetail, TProp>> Aggregate { get; }
  81. Filter<TDetail>? Filter { get; }
  82. Dictionary<Expression<Func<TDetail, object>>, Expression<Func<TMaster, object>>> Links { get; }
  83. AggregateCalculation Calculation { get; }
  84. Dictionary<string, string> GetLinks();
  85. }
  86. public abstract class CoreAggregate<TMaster, TDetail, TProp> : ICoreAggregate<TMaster, TDetail, TProp>
  87. {
  88. public abstract Expression<Func<TDetail, TProp>> Aggregate { get; }
  89. public virtual Filter<TDetail>? Filter => null;
  90. public abstract Dictionary<Expression<Func<TDetail, object>>, Expression<Func<TMaster, object>>> Links { get; }
  91. public Dictionary<string, string> GetLinks()
  92. {
  93. var result = new Dictionary<string, string>();
  94. foreach (var link in Links)
  95. {
  96. var childkey = AggregateUtils.ProcessExpression(link.Key); // String.Join(".", link.Key.ToString().Split('.').Skip(1));
  97. var parentkey = AggregateUtils.ProcessExpression(link.Value); // String.Join(".", link.Value.ToString().Split('.').Skip(1);
  98. result[childkey] = parentkey;
  99. }
  100. return result;
  101. }
  102. public abstract AggregateCalculation Calculation { get; }
  103. public string GetAggregate()
  104. {
  105. return string.Join(".", Aggregate.ToString().Split('.').Skip(1));
  106. }
  107. }
  108. public class AggregateAttribute : Attribute
  109. {
  110. public AggregateAttribute(Type calculator)
  111. {
  112. Calculator = Activator.CreateInstance(calculator);
  113. }
  114. public object Calculator { get; }
  115. public Type Source => GetSource();
  116. public AggregateCalculation Calculation => GetCalculation();
  117. public string Aggregate => GetAggregate();
  118. public Dictionary<string, string> Links => GetLinks();
  119. public IFilter? Filter => GetFilter();
  120. #region Internal (Reflection) functions
  121. private Type GetSource()
  122. {
  123. var intf = Calculator.GetType().GetInterfaces()
  124. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  125. if (intf != null)
  126. return intf.GenericTypeArguments[1];
  127. intf = Calculator.GetType().GetInterfaces()
  128. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,>));
  129. if (intf != null)
  130. return intf.GenericTypeArguments[0];
  131. throw new Exception("Unable to Locate Type Information for Aggregate");
  132. }
  133. private string GetAggregate()
  134. {
  135. var intf = Calculator.GetType().GetInterfaces()
  136. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  137. if (intf == null)
  138. {
  139. intf = Calculator.GetType().GetInterfaces()
  140. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,>));
  141. }
  142. if (intf != null)
  143. {
  144. var prop = intf.GetProperty("Aggregate");
  145. if (prop != null)
  146. {
  147. var obj = prop.GetValue(Calculator);
  148. if (obj != null)
  149. {
  150. var expr = obj as Expression;
  151. if (expr != null) return AggregateUtils.ProcessExpression(expr);
  152. }
  153. }
  154. }
  155. return "";
  156. }
  157. private Dictionary<string, string> GetLinks()
  158. {
  159. var intf = Calculator.GetType().GetInterfaces()
  160. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  161. if (intf != null)
  162. {
  163. var method = intf.GetMethod("GetLinks");
  164. if (method != null)
  165. {
  166. var dict = method.Invoke(Calculator, new object[] { });
  167. return (dict as Dictionary<string, string>)!;
  168. }
  169. }
  170. return new Dictionary<string, string>();
  171. }
  172. private AggregateCalculation GetCalculation()
  173. {
  174. var intf = Calculator.GetType().GetInterfaces()
  175. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  176. if (intf == null)
  177. intf = Calculator.GetType().GetInterfaces()
  178. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,>));
  179. if (intf != null)
  180. {
  181. var prop = intf.GetProperty("Calculation");
  182. if (prop != null)
  183. return (AggregateCalculation)prop.GetValue(Calculator);
  184. }
  185. return AggregateCalculation.None;
  186. }
  187. private IFilter? GetFilter()
  188. {
  189. var intf = Calculator.GetType().GetInterfaces()
  190. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICoreAggregate<,,>));
  191. if (intf != null)
  192. {
  193. var prop = intf.GetProperty("Filter");
  194. if (prop != null)
  195. return prop.GetValue(Calculator) as IFilter;
  196. }
  197. return null;
  198. }
  199. #endregion
  200. }
  201. #endregion
  202. #region Formulas
  203. public enum FormulaType
  204. {
  205. Virtual,
  206. Permanent
  207. }
  208. public enum FormulaOperator
  209. {
  210. None,
  211. Add,
  212. Subtract,
  213. Multiply,
  214. Divide,
  215. Minumum,
  216. Maximum
  217. }
  218. public interface IFormula<TType, TProp>
  219. {
  220. Expression<Func<TType, TProp>> Value { get; }
  221. Expression<Func<TType, TProp>>[] Modifiers { get; }
  222. FormulaOperator Operator { get; }
  223. FormulaType Type { get; }
  224. }
  225. public interface IFormula
  226. {
  227. String Value { get; }
  228. String[] Modifiers { get; }
  229. FormulaOperator Operator { get; }
  230. FormulaType Type { get; }
  231. }
  232. public class FormulaAttribute : Attribute, IFormula
  233. {
  234. public FormulaAttribute(Type calculator)
  235. {
  236. Calculator = Activator.CreateInstance(calculator);
  237. }
  238. public object Calculator { get; }
  239. public string Value => GetExpressionName("Value");
  240. public string[] Modifiers => GetExpressionNames("Modifiers");
  241. public FormulaOperator Operator => GetFormulaOperator();
  242. public FormulaType Type => GetFormulaType();
  243. #region Internal (Reflection) functions
  244. private FormulaOperator GetFormulaOperator()
  245. {
  246. var intf = Calculator.GetType().GetInterfaces()
  247. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  248. if (intf != null)
  249. {
  250. var prop = intf.GetProperty("Operator");
  251. if (prop != null)
  252. return (FormulaOperator)prop.GetValue(Calculator);
  253. }
  254. return FormulaOperator.None;
  255. }
  256. private FormulaType GetFormulaType()
  257. {
  258. var intf = Calculator.GetType().GetInterfaces()
  259. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  260. if (intf != null)
  261. {
  262. var prop = intf.GetProperty("Type");
  263. if (prop != null)
  264. return (FormulaType)prop.GetValue(Calculator);
  265. }
  266. return FormulaType.Virtual;
  267. }
  268. private string GetExpressionName(string property)
  269. {
  270. var intf = Calculator.GetType().GetInterfaces()
  271. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  272. if (intf != null)
  273. {
  274. var prop = intf.GetProperty(property);
  275. if (prop != null)
  276. {
  277. if(prop.GetValue(Calculator) is LambdaExpression expr)
  278. {
  279. var result = AggregateUtils.ProcessExpression(expr.Body);
  280. return result;
  281. }
  282. }
  283. }
  284. return "";
  285. }
  286. private string[] GetExpressionNames(string property)
  287. {
  288. var intf = Calculator.GetType().GetInterfaces()
  289. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  290. if (intf != null)
  291. {
  292. var prop = intf.GetProperty(property);
  293. if (prop != null)
  294. {
  295. if(prop.GetValue(Calculator) is LambdaExpression[] expressions)
  296. {
  297. var result = new List<string>();
  298. foreach (var expression in expressions)
  299. result.Add(AggregateUtils.ProcessExpression(expression.Body));
  300. return result.ToArray();
  301. }
  302. //return expressions.Select(x => x.Body is ConstantExpression ? ProcessConstantExpression(x.Body) : String.Join(".", RemoveConvert(x.Body).Split('.').Skip(1))).ToArray();
  303. }
  304. }
  305. return new string[] { };
  306. }
  307. #endregion
  308. }
  309. #endregion
  310. #region Conditions
  311. public enum Condition
  312. {
  313. None,
  314. Equals,
  315. NotEqual,
  316. GreaterThan,
  317. GreaterThanOrEqualTo,
  318. LessThan,
  319. LessThanOrEqualTo
  320. }
  321. public enum ConditionType
  322. {
  323. Virtual,
  324. Permanent
  325. }
  326. public interface ICondition<TType, TProp, TValue>
  327. {
  328. Expression<Func<TType, TProp>> Left { get; }
  329. Condition Condition { get; }
  330. Expression<Func<TType, TProp>> Right { get; }
  331. Expression<Func<TType, TValue>> True { get; }
  332. Expression<Func<TType, TValue>> False { get; }
  333. ConditionType Type { get; }
  334. }
  335. public class ConditionAttribute : Attribute
  336. {
  337. public ConditionAttribute(Type calculator)
  338. {
  339. Calculator = Activator.CreateInstance(calculator);
  340. }
  341. public object Calculator { get; }
  342. public string Left => GetExpressionName("Left");
  343. public Condition Condition => GetCondition();
  344. public string Right => GetExpressionName("Right");
  345. public string True => GetExpressionName("True");
  346. public string False => GetExpressionName("False");
  347. public ConditionType Type => GetConditionType();
  348. #region Internal (Reflection) functions
  349. private Condition GetCondition()
  350. {
  351. var intf = Calculator.GetType().GetInterfaces()
  352. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  353. if (intf != null)
  354. {
  355. var prop = intf.GetProperty("Condition");
  356. if (prop != null)
  357. return (Condition)prop.GetValue(Calculator);
  358. }
  359. return Condition.None;
  360. }
  361. private ConditionType GetConditionType()
  362. {
  363. var intf = Calculator.GetType().GetInterfaces()
  364. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  365. if (intf != null)
  366. {
  367. var prop = intf.GetProperty("Type");
  368. if (prop != null)
  369. return (ConditionType)prop.GetValue(Calculator);
  370. }
  371. return ConditionType.Virtual;
  372. }
  373. private string GetExpressionName(string property)
  374. {
  375. var intf = Calculator.GetType().GetInterfaces()
  376. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  377. if (intf != null)
  378. {
  379. var prop = intf.GetProperty(property);
  380. if (prop?.GetValue(Calculator) is LambdaExpression expr)
  381. {
  382. return AggregateUtils.ProcessExpression(expr.Body);
  383. }
  384. }
  385. return "";
  386. }
  387. #endregion
  388. }
  389. #endregion
  390. }