AutoEntityUnionGenerator.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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>(TType constant, Expression<Func<TInterface, object?>> mapping)
  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. }
  66. }