Aggregate.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. Constant
  218. }
  219. public interface IFormula<TType, TProp>
  220. {
  221. Expression<Func<TType, TProp>> Value { get; }
  222. Expression<Func<TType, TProp>>[] Modifiers { get; }
  223. FormulaOperator Operator { get; }
  224. FormulaType Type { get; }
  225. }
  226. public interface IFormula
  227. {
  228. String Value { get; }
  229. String[] Modifiers { get; }
  230. FormulaOperator Operator { get; }
  231. FormulaType Type { get; }
  232. }
  233. public class FormulaAttribute : Attribute, IFormula
  234. {
  235. public FormulaAttribute(Type calculator)
  236. {
  237. Calculator = Activator.CreateInstance(calculator);
  238. }
  239. public object Calculator { get; }
  240. public string Value => GetExpressionName("Value");
  241. public string[] Modifiers => GetExpressionNames("Modifiers");
  242. public FormulaOperator Operator => GetFormulaOperator();
  243. public FormulaType Type => GetFormulaType();
  244. #region Internal (Reflection) functions
  245. private FormulaOperator GetFormulaOperator()
  246. {
  247. var intf = Calculator.GetType().GetInterfaces()
  248. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  249. if (intf != null)
  250. {
  251. var prop = intf.GetProperty("Operator");
  252. if (prop != null)
  253. return (FormulaOperator)prop.GetValue(Calculator);
  254. }
  255. return FormulaOperator.None;
  256. }
  257. private FormulaType GetFormulaType()
  258. {
  259. var intf = Calculator.GetType().GetInterfaces()
  260. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  261. if (intf != null)
  262. {
  263. var prop = intf.GetProperty("Type");
  264. if (prop != null)
  265. return (FormulaType)prop.GetValue(Calculator);
  266. }
  267. return FormulaType.Virtual;
  268. }
  269. private string GetExpressionName(string property)
  270. {
  271. var intf = Calculator.GetType().GetInterfaces()
  272. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  273. if (intf != null)
  274. {
  275. var prop = intf.GetProperty(property);
  276. if (prop != null)
  277. {
  278. if(prop.GetValue(Calculator) is LambdaExpression expr)
  279. {
  280. var result = AggregateUtils.ProcessExpression(expr.Body);
  281. return result;
  282. }
  283. }
  284. }
  285. return "";
  286. }
  287. private string[] GetExpressionNames(string property)
  288. {
  289. var intf = Calculator.GetType().GetInterfaces()
  290. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IFormula<,>));
  291. if (intf != null)
  292. {
  293. var prop = intf.GetProperty(property);
  294. if (prop != null)
  295. {
  296. if(prop.GetValue(Calculator) is LambdaExpression[] expressions)
  297. {
  298. var result = new List<string>();
  299. foreach (var expression in expressions)
  300. result.Add(AggregateUtils.ProcessExpression(expression.Body));
  301. return result.ToArray();
  302. }
  303. //return expressions.Select(x => x.Body is ConstantExpression ? ProcessConstantExpression(x.Body) : String.Join(".", RemoveConvert(x.Body).Split('.').Skip(1))).ToArray();
  304. }
  305. }
  306. return new string[] { };
  307. }
  308. #endregion
  309. }
  310. #endregion
  311. #region Conditions
  312. public enum Condition
  313. {
  314. None,
  315. Equals,
  316. NotEqual,
  317. GreaterThan,
  318. GreaterThanOrEqualTo,
  319. LessThan,
  320. LessThanOrEqualTo
  321. }
  322. public enum ConditionType
  323. {
  324. Virtual,
  325. Permanent
  326. }
  327. public interface ICondition<TType, TProp, TValue>
  328. {
  329. Expression<Func<TType, TProp>> Left { get; }
  330. Condition Condition { get; }
  331. Expression<Func<TType, TProp>> Right { get; }
  332. Expression<Func<TType, TValue>> True { get; }
  333. Expression<Func<TType, TValue>> False { get; }
  334. ConditionType Type { get; }
  335. }
  336. public class ConditionAttribute : Attribute
  337. {
  338. public ConditionAttribute(Type calculator)
  339. {
  340. Calculator = Activator.CreateInstance(calculator);
  341. }
  342. public object Calculator { get; }
  343. public string Left => GetExpressionName("Left");
  344. public Condition Condition => GetCondition();
  345. public string Right => GetExpressionName("Right");
  346. public string True => GetExpressionName("True");
  347. public string False => GetExpressionName("False");
  348. public ConditionType Type => GetConditionType();
  349. #region Internal (Reflection) functions
  350. private Condition GetCondition()
  351. {
  352. var intf = Calculator.GetType().GetInterfaces()
  353. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  354. if (intf != null)
  355. {
  356. var prop = intf.GetProperty("Condition");
  357. if (prop != null)
  358. return (Condition)prop.GetValue(Calculator);
  359. }
  360. return Condition.None;
  361. }
  362. private ConditionType GetConditionType()
  363. {
  364. var intf = Calculator.GetType().GetInterfaces()
  365. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  366. if (intf != null)
  367. {
  368. var prop = intf.GetProperty("Type");
  369. if (prop != null)
  370. return (ConditionType)prop.GetValue(Calculator);
  371. }
  372. return ConditionType.Virtual;
  373. }
  374. private string GetExpressionName(string property)
  375. {
  376. var intf = Calculator.GetType().GetInterfaces()
  377. .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICondition<,,>));
  378. if (intf != null)
  379. {
  380. var prop = intf.GetProperty(property);
  381. if (prop?.GetValue(Calculator) is LambdaExpression expr)
  382. {
  383. return AggregateUtils.ProcessExpression(expr.Body);
  384. }
  385. }
  386. return "";
  387. }
  388. #endregion
  389. }
  390. #endregion
  391. }