AutoEntityUnionGenerator.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace InABox.Core
  6. {
  7. public interface IAutoEntityUnionGenerator : IAutoEntityGenerator
  8. {
  9. IAutoEntityUnionTable[] Tables { get; }
  10. }
  11. public interface IAutoEntityUnionConstant
  12. {
  13. object Value { get; }
  14. IColumn Mapping { get; }
  15. }
  16. public class AutoEntityUnionConstant : IAutoEntityUnionConstant
  17. {
  18. public object Value { get; private set; }
  19. public IColumn Mapping { get; private set; }
  20. public AutoEntityUnionConstant(object value, IColumn mapping)
  21. {
  22. Value = value;
  23. Mapping = mapping;
  24. }
  25. }
  26. public interface IAutoEntityUnionMapping
  27. {
  28. IComplexColumn Source { get; }
  29. IColumn Target { get; }
  30. }
  31. public class AutoEntityUnionMapping : IAutoEntityUnionMapping
  32. {
  33. public IComplexColumn Source { get; private set; }
  34. public IColumn Target { get; private set; }
  35. public AutoEntityUnionMapping(IComplexColumn source, IColumn target)
  36. {
  37. Source = source;
  38. Target = target;
  39. }
  40. }
  41. public interface IAutoEntityUnionTable
  42. {
  43. Type Entity { get; }
  44. IFilter? Filter { get; }
  45. AutoEntityUnionConstant[] Constants { get; }
  46. AutoEntityUnionMapping[] Mappings { get; }
  47. }
  48. public class AutoEntityUnionTable<TInterface,TEntity> : IAutoEntityUnionTable
  49. {
  50. public Type Entity => typeof(TEntity);
  51. public IFilter? Filter { get; private set; }
  52. private List<AutoEntityUnionConstant> _constants = new List<AutoEntityUnionConstant>();
  53. public AutoEntityUnionConstant[] Constants => _constants.ToArray();
  54. private List<AutoEntityUnionMapping> _mappings = new List<AutoEntityUnionMapping>();
  55. public AutoEntityUnionMapping[] Mappings => _mappings.ToArray();
  56. public AutoEntityUnionTable(Filter<TEntity>? filter)
  57. {
  58. Filter = filter;
  59. }
  60. public AutoEntityUnionTable<TInterface,TEntity> WithFilter(Filter<TEntity>? filter)
  61. {
  62. Filter = filter;
  63. return this;
  64. }
  65. public AutoEntityUnionTable<TInterface, TEntity> AddConstant<TType>(Expression<Func<TInterface, object?>> mapping, TType constant)
  66. {
  67. _constants.Add(new AutoEntityUnionConstant(constant, new Column<TInterface>(mapping)));
  68. return this;
  69. }
  70. public AutoEntityUnionTable<TInterface, TEntity> AliasField<TType>(Expression<Func<TInterface, object?>> target, Expression<Func<TEntity, TType>> source)
  71. {
  72. var _tgt = new Column<TInterface>(target);
  73. var _node = new ComplexFormulaFieldNode<TEntity, TType>(source);
  74. var _src = new ComplexColumn<TEntity, TType>(_tgt.Property, _node);
  75. _mappings.Add(new AutoEntityUnionMapping(_src, _tgt));
  76. return this;
  77. }
  78. public AutoEntityUnionTable<TInterface, TEntity> AliasField<TType>(Expression<Func<TInterface, object?>> target, IComplexFormulaNode<TEntity, TType> source)
  79. {
  80. var _tgt = new Column<TInterface>(target);
  81. var _src = new ComplexColumn<TEntity, TType>(_tgt.Property, source);
  82. _mappings.Add(new AutoEntityUnionMapping(_src, _tgt));
  83. return this;
  84. }
  85. public AutoEntityUnionTable<TInterface, TEntity> AliasField(Column<TInterface> target, Column<TEntity> source)
  86. {
  87. var _node = new ComplexFormulaFieldNode<TEntity, object?>(source.Property);
  88. var _src = new ComplexColumn<TEntity, object?>(target.Property, _node);
  89. _mappings.Add(new AutoEntityUnionMapping(_src, target));
  90. return this;
  91. }
  92. public AutoEntityUnionTable<TInterface, TEntity> AliasField(Expression<Func<TInterface, object?>> target, string formatString, params Expression<Func<TEntity, object?>>[] fields)
  93. {
  94. var _tgt = new Column<TInterface>(target);
  95. var _o = fields.Select(x => new ComplexFormulaFieldNode<TEntity, object?>(x));
  96. var _o2 = _o.OfType<IComplexFormulaNode<TEntity, object?>>().ToList()
  97. .Prepend(new ComplexFormulaConstantNode<TEntity, object>(formatString))
  98. .ToArray();
  99. var _n = new ComplexFormulaFormulaNode<TEntity, object?>(_o2, FormulaOperator.Format);
  100. //var _node = new ComplexFormulaFieldNode<TEntity, TType>(source);
  101. var _src = new ComplexColumn<TEntity, object?>(_tgt.Property, _n);
  102. _mappings.Add(new AutoEntityUnionMapping(_src, _tgt));
  103. return this;
  104. }
  105. }
  106. public abstract class AutoEntityUnionGenerator<TInterface> : IAutoEntityUnionGenerator
  107. {
  108. public AutoEntityUnionGenerator()
  109. {
  110. Configure();
  111. }
  112. private List<IAutoEntityUnionTable> _tables = new List<IAutoEntityUnionTable>();
  113. public IAutoEntityUnionTable[] Tables => _tables.ToArray();
  114. public AutoEntityUnionTable<TInterface, TType> AddTable<TType>(Filter<TType>? filter = null)
  115. {
  116. var table = new AutoEntityUnionTable<TInterface, TType>(filter);
  117. _tables.Add(table);
  118. return table;
  119. }
  120. protected abstract void Configure();
  121. public Type Definition => typeof(TInterface);
  122. public abstract bool Distinct { get; }
  123. public abstract Column<TInterface>[] IDColumns { get; }
  124. IColumn[] IAutoEntityGenerator.IDColumns => IDColumns;
  125. }
  126. }