|
@@ -0,0 +1,95 @@
|
|
|
+using CsvHelper.Configuration;
|
|
|
+using InABox.Core;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq.Expressions;
|
|
|
+using System.Text;
|
|
|
+
|
|
|
+namespace InABox.Poster.CSV
|
|
|
+{
|
|
|
+ public interface ICSVClassMap
|
|
|
+ {
|
|
|
+ ClassMap ClassMap { get; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface ICSVClassMap<T> : ICSVClassMap
|
|
|
+ {
|
|
|
+ new ClassMap<T> ClassMap { get; }
|
|
|
+
|
|
|
+ ClassMap ICSVClassMap.ClassMap => ClassMap;
|
|
|
+
|
|
|
+ void Map(string name, Expression<Func<T, object>> expr);
|
|
|
+ }
|
|
|
+
|
|
|
+ public class CSVClassMap<T> : ClassMap<T>, ICSVClassMap<T>
|
|
|
+ {
|
|
|
+ public ClassMap<T> ClassMap => this;
|
|
|
+
|
|
|
+ public void Map(string name, Expression<Func<T, object>> expr)
|
|
|
+ {
|
|
|
+ Map(expr).Name(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface ICSVExport
|
|
|
+ {
|
|
|
+ Type Type { get; }
|
|
|
+
|
|
|
+ ICSVClassMap ClassMap { get; }
|
|
|
+
|
|
|
+ IEnumerable<object> Records { get; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface ICSVExport<T> : ICSVExport
|
|
|
+ where T : class
|
|
|
+ {
|
|
|
+ new Type Type => typeof(T);
|
|
|
+ new CSVClassMap<T> ClassMap { get; }
|
|
|
+ new IEnumerable<T> Records { get; }
|
|
|
+
|
|
|
+ Type ICSVExport.Type => Type;
|
|
|
+ ICSVClassMap ICSVExport.ClassMap => ClassMap;
|
|
|
+ IEnumerable<object> ICSVExport.Records => Records;
|
|
|
+ }
|
|
|
+
|
|
|
+ public class CSVExport<T> : ICSVExport<T>
|
|
|
+ where T : class
|
|
|
+ {
|
|
|
+ public CSVClassMap<T> ClassMap { get; } = new CSVClassMap<T>();
|
|
|
+
|
|
|
+ public IEnumerable<T> Records => Items;
|
|
|
+
|
|
|
+ public List<T> Items { get; } = new List<T>();
|
|
|
+
|
|
|
+ public void DefineMapping(List<Tuple<string, Expression<Func<T, object>>>> mappings)
|
|
|
+ {
|
|
|
+ foreach(var (name, expr) in mappings)
|
|
|
+ {
|
|
|
+ ClassMap.Map(name, expr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void Map(string name, Expression<Func<T, object>> expr)
|
|
|
+ {
|
|
|
+ ClassMap.Map(name, expr);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Add(T item)
|
|
|
+ {
|
|
|
+ Items.Add(item);
|
|
|
+ }
|
|
|
+ public void AddRange(IEnumerable<T> items)
|
|
|
+ {
|
|
|
+ Items.AddRange(items);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Defines an interface for posters that can export to CSV.
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TEntity"></typeparam>
|
|
|
+ public interface ICSVPoster<TEntity> : IPoster<TEntity, CSVPosterSettings<TEntity>>
|
|
|
+ where TEntity : Entity, IPostable
|
|
|
+ {
|
|
|
+ ICSVExport Process(IEnumerable<TEntity> entities);
|
|
|
+ }
|
|
|
+}
|