AutoEntityCartesianGenerator.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. namespace InABox.Core
  5. {
  6. public interface IAutoEntityCartesianMapping
  7. {
  8. IColumn Column { get;}
  9. IColumn Mapping { get; }
  10. }
  11. public struct AutoEntityCartesianMapping : IAutoEntityCartesianMapping
  12. {
  13. public IColumn Column { get; private set; }
  14. public IColumn Mapping { get; private set; }
  15. public AutoEntityCartesianMapping(IColumn column, IColumn mapping)
  16. {
  17. Column = column;
  18. Mapping = mapping;
  19. }
  20. }
  21. public interface IAutoEntityCartesianConstant
  22. {
  23. object Constant { get;}
  24. IColumn Mapping { get; }
  25. }
  26. public struct AutoEntityCartesianConstant: IAutoEntityCartesianConstant
  27. {
  28. public object Constant { get; private set; }
  29. public IColumn Mapping { get; private set; }
  30. public AutoEntityCartesianConstant(object constant, IColumn mapping)
  31. {
  32. Constant = constant;
  33. Mapping = mapping;
  34. }
  35. }
  36. public interface IAutoEntityCartesianTable
  37. {
  38. Type Type { get; }
  39. IFilter? Filter { get; }
  40. IColumns? Columns { get; }
  41. IAutoEntityCartesianMapping[] Mappings { get; }
  42. }
  43. public class AutoEntityCartesianTable<T, TInterface> : IAutoEntityCartesianTable
  44. {
  45. public Type Type => typeof(T);
  46. public IFilter? Filter { get; private set; }
  47. private List<IAutoEntityCartesianMapping> _mappings = new List<IAutoEntityCartesianMapping>();
  48. public IAutoEntityCartesianMapping[] Mappings => _mappings.ToArray();
  49. public AutoEntityCartesianTable(Filter<T> filter)
  50. {
  51. Filter = filter;
  52. }
  53. public IColumns Columns
  54. {
  55. get
  56. {
  57. var columns = new Columns<T>();
  58. foreach (var mapping in _mappings)
  59. {
  60. columns.Add((mapping.Column as IColumn).Property);
  61. }
  62. return columns;
  63. }
  64. }
  65. public AutoEntityCartesianTable<T, TInterface> AddMapping(Expression<Func<T, object?>> source, Expression<Func<TInterface, object?>> mapping)
  66. {
  67. _mappings.Add(new AutoEntityCartesianMapping(new Column<T>(source), new Column<TInterface>(mapping)));
  68. return this;
  69. }
  70. }
  71. public interface IAutoEntityCartesianGenerator : IAutoEntityGenerator
  72. {
  73. IAutoEntityCartesianTable[] Tables { get; }
  74. IAutoEntityCartesianConstant[] Constants { get; }
  75. void Configure();
  76. }
  77. public abstract class AutoEntityCartesianGenerator<TInterface>: IAutoEntityCartesianGenerator
  78. {
  79. public AutoEntityCartesianGenerator()
  80. {
  81. Configure();
  82. }
  83. private List<IAutoEntityCartesianTable> _tables = new List<IAutoEntityCartesianTable>();
  84. public IAutoEntityCartesianTable[] Tables => _tables.ToArray();
  85. private List<IAutoEntityCartesianConstant> _constants = new List<IAutoEntityCartesianConstant>();
  86. public IAutoEntityCartesianConstant[] Constants => _constants.ToArray();
  87. public abstract void Configure();
  88. protected AutoEntityCartesianTable<T,TInterface> AddTable<T>(Filter<T> filter)
  89. {
  90. var table = new AutoEntityCartesianTable<T,TInterface>(filter);
  91. _tables.Add(table);
  92. return table;
  93. }
  94. public void AddConstant<TType>(TType constant, Expression<Func<TInterface, object?>> mapping)
  95. {
  96. _constants.Add(new AutoEntityCartesianConstant(constant, new Column<TInterface>(mapping)));
  97. }
  98. public Type Definition => typeof(TInterface);
  99. public abstract bool Distinct { get; }
  100. }
  101. }