ICSVPoster.cs 3.3 KB

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