AutoEntityUnionGenerator.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 IAutoEntityUnionTable
  27. {
  28. Type Entity { get; }
  29. IFilter? Filter { get; }
  30. AutoEntityUnionConstant[] Constants { get; }
  31. }
  32. public class AutoEntityUnionTable<TInterface,TEntity> : IAutoEntityUnionTable
  33. {
  34. public Type Entity => typeof(TEntity);
  35. public IFilter? Filter { get; }
  36. private List<AutoEntityUnionConstant> _constants = new List<AutoEntityUnionConstant>();
  37. public AutoEntityUnionConstant[] Constants => _constants.ToArray();
  38. public AutoEntityUnionTable(Filter<TEntity>? filter)
  39. {
  40. Filter = filter;
  41. }
  42. public AutoEntityUnionTable<TInterface, TEntity> AddConstant<TType>(Expression<Func<TInterface, object?>> mapping, TType constant)
  43. {
  44. _constants.Add(new AutoEntityUnionConstant(constant, new Column<TInterface>(mapping)));
  45. return this;
  46. }
  47. }
  48. public abstract class AutoEntityUnionGenerator<TInterface> : IAutoEntityUnionGenerator
  49. {
  50. public AutoEntityUnionGenerator()
  51. {
  52. Configure();
  53. }
  54. private List<IAutoEntityUnionTable> _tables = new List<IAutoEntityUnionTable>();
  55. public IAutoEntityUnionTable[] Tables => _tables.ToArray();
  56. public AutoEntityUnionTable<TInterface, TType> AddTable<TType>(Filter<TType>? filter = null)
  57. {
  58. var table = new AutoEntityUnionTable<TInterface, TType>(filter);
  59. _tables.Add(table);
  60. return table;
  61. }
  62. protected abstract void Configure();
  63. public Type Definition => typeof(TInterface);
  64. public abstract bool Distinct { get; }
  65. public abstract Column<TInterface>[] IDColumns { get; }
  66. IColumn[] IAutoEntityGenerator.IDColumns => IDColumns;
  67. }
  68. }