ICSVPoster.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using CsvHelper.Configuration;
  2. using InABox.Core;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq.Expressions;
  6. using System.Text;
  7. namespace InABox.Poster.CSV
  8. {
  9. public interface ICSVClassMap
  10. {
  11. ClassMap ClassMap { get; }
  12. }
  13. public interface ICSVClassMap<T> : ICSVClassMap
  14. {
  15. new ClassMap<T> ClassMap { get; }
  16. ClassMap ICSVClassMap.ClassMap => ClassMap;
  17. void Map(string name, Expression<Func<T, object>> expr);
  18. }
  19. public class CSVClassMap<T> : ClassMap<T>, ICSVClassMap<T>
  20. {
  21. public ClassMap<T> ClassMap => this;
  22. public void Map(string name, Expression<Func<T, object>> expr)
  23. {
  24. Map(expr).Name(name);
  25. }
  26. }
  27. public interface ICSVExport<TPostable> : IPostResult<TPostable>
  28. where TPostable : IPostable
  29. {
  30. Type Type { get; }
  31. ICSVClassMap ClassMap { get; }
  32. IEnumerable<object> Records { get; }
  33. }
  34. public interface ICSVExport<TExport, TPostable> : ICSVExport<TPostable>
  35. where TExport : class
  36. where TPostable : IPostable
  37. {
  38. new Type Type => typeof(TExport);
  39. new CSVClassMap<TExport> ClassMap { get; }
  40. new IEnumerable<TExport> Records { get; }
  41. Type ICSVExport<TPostable>.Type => Type;
  42. ICSVClassMap ICSVExport<TPostable>.ClassMap => ClassMap;
  43. IEnumerable<object> ICSVExport<TPostable>.Records => Records;
  44. }
  45. public class CSVExport<TExport, TPostable> : ICSVExport<TExport, TPostable>
  46. where TExport : class
  47. where TPostable : IPostable
  48. {
  49. public CSVClassMap<TExport> ClassMap { get; } = new CSVClassMap<TExport>();
  50. public IEnumerable<TExport> Records => items.Where(x => x.Item2.PostedStatus == PostedStatus.Posted).Select(x => x.Item1);
  51. private List<Tuple<TExport, TPostable>> items = new List<Tuple<TExport, TPostable>>();
  52. private Dictionary<Type, List<IPostableFragment<TPostable>>> fragments = new Dictionary<Type, List<IPostableFragment<TPostable>>>();
  53. public IEnumerable<TPostable> PostedEntities => items.Select(x => x.Item2);
  54. public IEnumerable<KeyValuePair<Type, IEnumerable<IPostableFragment<TPostable>>>> Fragments =>
  55. fragments.Select(x => new KeyValuePair<Type, IEnumerable<IPostableFragment<TPostable>>>(x.Key, x.Value));
  56. public void DefineMapping(List<Tuple<string, Expression<Func<TExport, object>>>> mappings)
  57. {
  58. foreach(var (name, expr) in mappings)
  59. {
  60. ClassMap.Map(name, expr);
  61. }
  62. }
  63. public void Map(string name, Expression<Func<TExport, object>> expr)
  64. {
  65. ClassMap.Map(name, expr);
  66. }
  67. public void AddSuccess(TExport export, TPostable postable)
  68. {
  69. postable.Post();
  70. items.Add(new(export, postable));
  71. }
  72. }
  73. /// <summary>
  74. /// Defines an interface for posters that can export to CSV.
  75. /// </summary>
  76. /// <typeparam name="TEntity"></typeparam>
  77. [Caption("CSV")]
  78. public interface ICSVPoster<TEntity> : IPoster<TEntity, CSVPosterSettings>
  79. where TEntity : Entity, IPostable, IRemotable, IPersistent, new()
  80. {
  81. bool BeforePost(IDataModel<TEntity> model);
  82. ICSVExport<TEntity> Process(IDataModel<TEntity> model);
  83. void AfterPost(IDataModel<TEntity> model, IPostResult<TEntity> result);
  84. }
  85. }