AutoEntityUnionGenerator.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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; }
  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> AddConstant<TType>(Expression<Func<TInterface, object?>> mapping, TType constant)
  61. {
  62. _constants.Add(new AutoEntityUnionConstant(constant, new Column<TInterface>(mapping)));
  63. return this;
  64. }
  65. public AutoEntityUnionTable<TInterface, TEntity> AliasField<TType>(Expression<Func<TInterface, object?>> target, Expression<Func<TEntity, TType>> source)
  66. {
  67. var _tgt = new Column<TInterface>(target);
  68. var _node = new ComplexFormulaFieldNode<TEntity, TType>(source);
  69. var _src = new ComplexColumn<TEntity, TType>(_tgt.Property, _node);
  70. _mappings.Add(new AutoEntityUnionMapping(_src, _tgt));
  71. return this;
  72. }
  73. public AutoEntityUnionTable<TInterface, TEntity> AliasField(Expression<Func<TInterface, object?>> target, string formatString, params Expression<Func<TEntity, object?>>[] fields)
  74. {
  75. var _tgt = new Column<TInterface>(target);
  76. var _o = fields.Select(x => new ComplexFormulaFieldNode<TEntity, object?>(x));
  77. var _o2 = _o.OfType<IComplexFormulaNode<TEntity, object?>>().ToList()
  78. .Prepend(new ComplexFormulaConstantNode<TEntity, object>(formatString))
  79. .ToArray();
  80. var _n = new ComplexFormulaFormulaNode<TEntity, object?>(_o2, FormulaOperator.Format);
  81. //var _node = new ComplexFormulaFieldNode<TEntity, TType>(source);
  82. var _src = new ComplexColumn<TEntity, object?>(_tgt.Property, _n);
  83. _mappings.Add(new AutoEntityUnionMapping(_src, _tgt));
  84. return this;
  85. }
  86. }
  87. public abstract class AutoEntityUnionGenerator<TInterface> : IAutoEntityUnionGenerator
  88. {
  89. public AutoEntityUnionGenerator()
  90. {
  91. Configure();
  92. }
  93. private List<IAutoEntityUnionTable> _tables = new List<IAutoEntityUnionTable>();
  94. public IAutoEntityUnionTable[] Tables => _tables.ToArray();
  95. public AutoEntityUnionTable<TInterface, TType> AddTable<TType>(Filter<TType>? filter = null)
  96. {
  97. var table = new AutoEntityUnionTable<TInterface, TType>(filter);
  98. _tables.Add(table);
  99. return table;
  100. }
  101. protected abstract void Configure();
  102. public Type Definition => typeof(TInterface);
  103. public abstract bool Distinct { get; }
  104. public abstract Column<TInterface>[] IDColumns { get; }
  105. IColumn[] IAutoEntityGenerator.IDColumns => IDColumns;
  106. }
  107. }