AutoEntityUnionGenerator.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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<TType>(Expression<Func<TInterface, object?>> target, IComplexFormulaNode<TEntity, TType> source)
  74. {
  75. var _tgt = new Column<TInterface>(target);
  76. var _src = new ComplexColumn<TEntity, TType>(_tgt.Property, source);
  77. _mappings.Add(new AutoEntityUnionMapping(_src, _tgt));
  78. return this;
  79. }
  80. public AutoEntityUnionTable<TInterface, TEntity> AliasField(Column<TInterface> target, Column<TEntity> source)
  81. {
  82. var _node = new ComplexFormulaFieldNode<TEntity, object?>(source.Property);
  83. var _src = new ComplexColumn<TEntity, object?>(target.Property, _node);
  84. _mappings.Add(new AutoEntityUnionMapping(_src, target));
  85. return this;
  86. }
  87. public AutoEntityUnionTable<TInterface, TEntity> AliasField(Expression<Func<TInterface, object?>> target, string formatString, params Expression<Func<TEntity, object?>>[] fields)
  88. {
  89. var _tgt = new Column<TInterface>(target);
  90. var _o = fields.Select(x => new ComplexFormulaFieldNode<TEntity, object?>(x));
  91. var _o2 = _o.OfType<IComplexFormulaNode<TEntity, object?>>().ToList()
  92. .Prepend(new ComplexFormulaConstantNode<TEntity, object>(formatString))
  93. .ToArray();
  94. var _n = new ComplexFormulaFormulaNode<TEntity, object?>(_o2, FormulaOperator.Format);
  95. //var _node = new ComplexFormulaFieldNode<TEntity, TType>(source);
  96. var _src = new ComplexColumn<TEntity, object?>(_tgt.Property, _n);
  97. _mappings.Add(new AutoEntityUnionMapping(_src, _tgt));
  98. return this;
  99. }
  100. }
  101. public abstract class AutoEntityUnionGenerator<TInterface> : IAutoEntityUnionGenerator
  102. {
  103. public AutoEntityUnionGenerator()
  104. {
  105. Configure();
  106. }
  107. private List<IAutoEntityUnionTable> _tables = new List<IAutoEntityUnionTable>();
  108. public IAutoEntityUnionTable[] Tables => _tables.ToArray();
  109. public AutoEntityUnionTable<TInterface, TType> AddTable<TType>(Filter<TType>? filter = null)
  110. {
  111. var table = new AutoEntityUnionTable<TInterface, TType>(filter);
  112. _tables.Add(table);
  113. return table;
  114. }
  115. protected abstract void Configure();
  116. public Type Definition => typeof(TInterface);
  117. public abstract bool Distinct { get; }
  118. public abstract Column<TInterface>[] IDColumns { get; }
  119. IColumn[] IAutoEntityGenerator.IDColumns => IDColumns;
  120. }
  121. }