| | | 1 | | using ArturRios.Data.Export.Exporters; |
| | | 2 | | using ArturRios.Data.Export.Interfaces; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Data.Export.Abstractions; |
| | | 6 | | |
| | | 7 | | /// <summary>Resolves exporters from the DI container by <see cref="ExportFormat" />.</summary> |
| | | 8 | | /// <param name="serviceProvider">The service provider holding the registered exporters.</param> |
| | 7 | 9 | | public class ExporterFactory(IServiceProvider serviceProvider) : IExporterFactory |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | | 12 | | public IExporter<T> Resolve<T>(ExportFormat format) where T : class |
| | 6 | 13 | | { |
| | 6 | 14 | | var openType = format switch |
| | 6 | 15 | | { |
| | 1 | 16 | | ExportFormat.Csv => typeof(CsvExporter<>), |
| | 1 | 17 | | ExportFormat.Json => typeof(JsonExporter<>), |
| | 1 | 18 | | ExportFormat.Txt => typeof(TxtExporter<>), |
| | 1 | 19 | | ExportFormat.MessagePack => typeof(MessagePackExporter<>), |
| | 2 | 20 | | ExportFormat.Excel => ResolveExcelType(), |
| | 0 | 21 | | _ => throw new ArgumentOutOfRangeException(nameof(format)) |
| | 6 | 22 | | }; |
| | | 23 | | |
| | 5 | 24 | | return (IExporter<T>)serviceProvider.GetRequiredService(openType.MakeGenericType(typeof(T))); |
| | 5 | 25 | | } |
| | | 26 | | |
| | | 27 | | private Type ResolveExcelType() |
| | 2 | 28 | | { |
| | 2 | 29 | | var registration = serviceProvider.GetService<ExcelExporterRegistration>() |
| | 2 | 30 | | ?? throw new NotSupportedException( |
| | 2 | 31 | | "Excel export requires the ArturRios.Data.Export.Excel package — call AddExcelExport()."); |
| | | 32 | | |
| | 1 | 33 | | return registration.ExporterType; |
| | 1 | 34 | | } |
| | | 35 | | } |