Skip to content

Commit 4049700

Browse files
Moved templating logic to MappingTemplater and renamed MappingExporter's SaveAsAsync method to ExportAsync for consistency
1 parent 848a37a commit 4049700

File tree

8 files changed

+121
-119
lines changed

8 files changed

+121
-119
lines changed

benchmarks/MiniExcel.Benchmarks/BenchmarkSections/CreateExcelBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void MiniExcelCreateWithSimpleMappingTest()
5454
{
5555
using var path = AutoDeletingPath.Create();
5656
using var stream = File.Create(path.FilePath);
57-
_simpleMappingExporter.SaveAs(stream, GetValue());
57+
_simpleMappingExporter.Export(stream, GetValue());
5858
}
5959

6060
[Benchmark(Description = "ClosedXml Create Xlsx")]

benchmarks/MiniExcel.Benchmarks/BenchmarkSections/TemplateExcelBenchmark.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MiniExcelLib.Benchmarks.BenchmarkSections;
99
public class TemplateExcelBenchmark : BenchmarkBase
1010
{
1111
private OpenXmlTemplater _templater;
12-
private MappingExporter _mappingExporter;
12+
private MappingTemplater _mappingTemplater;
1313
private OpenXmlExporter _exporter;
1414

1515
public class Employee
@@ -30,7 +30,7 @@ public void Setup()
3030
config.Property(x => x.Name).ToCell("A2");
3131
config.Property(x => x.Department).ToCell("B2");
3232
});
33-
_mappingExporter = MiniExcel.Exporters.GetMappingExporter(registry);
33+
_mappingTemplater = MiniExcel.Templaters.GetMappingTemplater(registry);
3434
}
3535

3636
[Benchmark(Description = "MiniExcel Template Generate")]
@@ -94,6 +94,6 @@ public void MiniExcel_Mapping_Template_Generate_Test()
9494
Department = "HR"
9595
});
9696

97-
_mappingExporter.ApplyTemplate(outputPath.FilePath, templatePath.FilePath, employees);
97+
_mappingTemplater.ApplyTemplate(outputPath.FilePath, templatePath.FilePath, employees);
9898
}
9999
}
Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
namespace MiniExcelLib.Core.Mapping;
22

3-
public partial class MappingExporter
3+
public sealed partial class MappingExporter()
44
{
5-
private readonly MappingRegistry _registry;
5+
private readonly MappingRegistry _registry = new();
66

7-
public MappingExporter()
8-
{
9-
_registry = new MappingRegistry();
10-
}
11-
12-
public MappingExporter(MappingRegistry registry)
7+
public MappingExporter(MappingRegistry registry) : this()
138
{
149
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
1510
}
1611

1712
[CreateSyncVersion]
18-
public async Task SaveAsAsync<T>(Stream? stream, IEnumerable<T>? values, CancellationToken cancellationToken = default)
19-
where T : class
13+
public async Task ExportAsync<T>(Stream? stream, IEnumerable<T>? values, CancellationToken cancellationToken = default) where T : class
2014
{
2115
if (stream is null)
2216
throw new ArgumentNullException(nameof(stream));
@@ -30,63 +24,4 @@ public async Task SaveAsAsync<T>(Stream? stream, IEnumerable<T>? values, Cancell
3024

3125
await MappingWriter<T>.SaveAsAsync(stream, values, mapping, cancellationToken).ConfigureAwait(false);
3226
}
33-
34-
[CreateSyncVersion]
35-
public async Task ApplyTemplateAsync<T>(
36-
string? outputPath,
37-
string? templatePath,
38-
IEnumerable<T>? values,
39-
CancellationToken cancellationToken = default) where T : class
40-
{
41-
if (string.IsNullOrEmpty(outputPath))
42-
throw new ArgumentException("Output path cannot be null or empty", nameof(outputPath));
43-
if (string.IsNullOrEmpty(templatePath))
44-
throw new ArgumentException("Template path cannot be null or empty", nameof(templatePath));
45-
if (values is null)
46-
throw new ArgumentNullException(nameof(values));
47-
48-
using var outputStream = File.Create(outputPath);
49-
using var templateStream = File.OpenRead(templatePath);
50-
await ApplyTemplateAsync(outputStream, templateStream, values, cancellationToken).ConfigureAwait(false);
51-
}
52-
53-
[CreateSyncVersion]
54-
public async Task ApplyTemplateAsync<T>(
55-
Stream? outputStream,
56-
Stream? templateStream,
57-
IEnumerable<T>? values,
58-
CancellationToken cancellationToken = default) where T : class
59-
{
60-
if (outputStream is null)
61-
throw new ArgumentNullException(nameof(outputStream));
62-
if (templateStream is null)
63-
throw new ArgumentNullException(nameof(templateStream));
64-
if (values is null)
65-
throw new ArgumentNullException(nameof(values));
66-
67-
if (!_registry.HasMapping<T>())
68-
throw new InvalidOperationException($"No mapping configured for type {typeof(T).Name}. Call Configure<{typeof(T).Name}>() first.");
69-
70-
var mapping = _registry.GetMapping<T>();
71-
await MappingTemplateApplicator<T>.ApplyTemplateAsync(
72-
outputStream, templateStream, values, mapping, cancellationToken).ConfigureAwait(false);
73-
}
74-
75-
[CreateSyncVersion]
76-
public async Task ApplyTemplateAsync<T>(
77-
Stream? outputStream,
78-
byte[]? templateBytes,
79-
IEnumerable<T>? values,
80-
CancellationToken cancellationToken = default) where T : class
81-
{
82-
if (outputStream is null)
83-
throw new ArgumentNullException(nameof(outputStream));
84-
if (templateBytes is null)
85-
throw new ArgumentNullException(nameof(templateBytes));
86-
if (values is null)
87-
throw new ArgumentNullException(nameof(values));
88-
89-
using var templateStream = new MemoryStream(templateBytes);
90-
await ApplyTemplateAsync(outputStream, templateStream, values, cancellationToken).ConfigureAwait(false);
91-
}
9227
}

src/MiniExcel.Core/Mapping/MappingImporter.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
namespace MiniExcelLib.Core.Mapping;
22

3-
public partial class MappingImporter
3+
public sealed partial class MappingImporter()
44
{
5-
private readonly MappingRegistry _registry;
5+
private readonly MappingRegistry _registry = new();
66

7-
public MappingImporter()
8-
{
9-
_registry = new MappingRegistry();
10-
}
11-
12-
public MappingImporter(MappingRegistry registry)
7+
public MappingImporter(MappingRegistry registry) : this()
138
{
149
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
1510
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace MiniExcelLib.Core.Mapping;
2+
3+
public sealed partial class MappingTemplater()
4+
{
5+
private readonly MappingRegistry _registry = new();
6+
7+
public MappingTemplater(MappingRegistry registry) : this()
8+
{
9+
_registry = registry ?? throw new ArgumentNullException(nameof(registry));
10+
}
11+
12+
[CreateSyncVersion]
13+
public async Task ApplyTemplateAsync<T>(
14+
string? outputPath,
15+
string? templatePath,
16+
IEnumerable<T>? values,
17+
CancellationToken cancellationToken = default) where T : class
18+
{
19+
if (string.IsNullOrEmpty(outputPath))
20+
throw new ArgumentException("Output path cannot be null or empty", nameof(outputPath));
21+
if (string.IsNullOrEmpty(templatePath))
22+
throw new ArgumentException("Template path cannot be null or empty", nameof(templatePath));
23+
if (values is null)
24+
throw new ArgumentNullException(nameof(values));
25+
26+
using var outputStream = File.Create(outputPath);
27+
using var templateStream = File.OpenRead(templatePath);
28+
await ApplyTemplateAsync(outputStream, templateStream, values, cancellationToken).ConfigureAwait(false);
29+
}
30+
31+
[CreateSyncVersion]
32+
public async Task ApplyTemplateAsync<T>(
33+
Stream? outputStream,
34+
Stream? templateStream,
35+
IEnumerable<T>? values,
36+
CancellationToken cancellationToken = default) where T : class
37+
{
38+
if (outputStream is null)
39+
throw new ArgumentNullException(nameof(outputStream));
40+
if (templateStream is null)
41+
throw new ArgumentNullException(nameof(templateStream));
42+
if (values is null)
43+
throw new ArgumentNullException(nameof(values));
44+
45+
if (!_registry.HasMapping<T>())
46+
throw new InvalidOperationException(
47+
$"No mapping configured for type {typeof(T).Name}. Call Configure<{typeof(T).Name}>() first.");
48+
49+
var mapping = _registry.GetMapping<T>();
50+
await MappingTemplateApplicator<T>.ApplyTemplateAsync(
51+
outputStream, templateStream, values, mapping, cancellationToken).ConfigureAwait(false);
52+
}
53+
54+
[CreateSyncVersion]
55+
public async Task ApplyTemplateAsync<T>(
56+
Stream? outputStream,
57+
byte[]? templateBytes,
58+
IEnumerable<T>? values,
59+
CancellationToken cancellationToken = default) where T : class
60+
{
61+
if (outputStream is null)
62+
throw new ArgumentNullException(nameof(outputStream));
63+
if (templateBytes is null)
64+
throw new ArgumentNullException(nameof(templateBytes));
65+
if (values is null)
66+
throw new ArgumentNullException(nameof(values));
67+
68+
using var templateStream = new MemoryStream(templateBytes);
69+
await ApplyTemplateAsync(outputStream, templateStream, values, cancellationToken).ConfigureAwait(false);
70+
}
71+
}

src/MiniExcel.Core/MiniExcelProviders.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ public sealed class MiniExcelTemplaterProvider
2323
internal MiniExcelTemplaterProvider() { }
2424

2525
public OpenXmlTemplater GetOpenXmlTemplater() => new();
26+
public MappingTemplater GetMappingTemplater(MappingRegistry registry) => new(registry);
2627
}

tests/MiniExcel.Core.Tests/FluentMapping/MiniExcelMappingTemplateTests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public async Task BasicTemplateTest()
6969
};
7070

7171
using var outputPath = AutoDeletingPath.Create();
72-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
73-
await exporter.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), [data]);
72+
var templater = MiniExcel.Templaters.GetMappingTemplater(registry);
73+
await templater.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), [data]);
7474

7575
var rows = _importer.Query(outputPath.ToString(), useHeaderRow: false).ToList();
7676

@@ -128,8 +128,8 @@ public async Task StreamOverloadTest()
128128
using (var outputStream = File.Create(outputPath.ToString()))
129129
using (var templateStream = File.OpenRead(templatePath.ToString()))
130130
{
131-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
132-
await exporter.ApplyTemplateAsync(outputStream, templateStream, [data]);
131+
var templater = MiniExcel.Templaters.GetMappingTemplater(registry);
132+
await templater.ApplyTemplateAsync(outputStream, templateStream, [data]);
133133
}
134134

135135
var rows = _importer.Query(outputPath.ToString(), useHeaderRow: false).ToList();
@@ -169,8 +169,8 @@ public async Task ByteArrayOverloadTest()
169169
using var outputPath = AutoDeletingPath.Create();
170170
using (var outputStream = File.Create(outputPath.ToString()))
171171
{
172-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
173-
await exporter.ApplyTemplateAsync(outputStream, templateBytes, [data]);
172+
var templater = MiniExcel.Templaters.GetMappingTemplater(registry);
173+
await templater.ApplyTemplateAsync(outputStream, templateBytes, [data]);
174174
}
175175

176176
var rows = _importer.Query(outputPath.ToString(), useHeaderRow: false).ToList();
@@ -234,8 +234,8 @@ public async Task CollectionTemplateTest()
234234
};
235235

236236
using var outputPath = AutoDeletingPath.Create();
237-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
238-
await exporter.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), [dept]);
237+
var templater = MiniExcel.Templaters.GetMappingTemplater(registry);
238+
await templater.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), [dept]);
239239

240240
var rows = _importer.Query(outputPath.ToString(), useHeaderRow: false).ToList();
241241

@@ -282,8 +282,8 @@ public async Task EmptyDataTest()
282282
});
283283

284284
using var outputPath = AutoDeletingPath.Create();
285-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
286-
await exporter.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), Array.Empty<TestEntity>());
285+
var templater = MiniExcel.Templaters.GetMappingTemplater(registry);
286+
await templater.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), Array.Empty<TestEntity>());
287287

288288
var rows = _importer.Query(outputPath.ToString(), useHeaderRow: false).ToList();
289289
Assert.Equal(3, rows.Count); // Column headers + our headers + empty data row
@@ -326,8 +326,8 @@ public async Task NullValuesTest()
326326

327327
// Apply template
328328
using var outputPath = AutoDeletingPath.Create();
329-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
330-
await exporter.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), [data]);
329+
var templater = MiniExcel.Templaters.GetMappingTemplater(registry);
330+
await templater.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), [data]);
331331

332332
// Verify null handling
333333
// Verify - use useHeaderRow=false since we want to see all rows
@@ -371,8 +371,8 @@ public async Task MultipleItemsTest()
371371

372372
// Apply template
373373
using var outputPath = AutoDeletingPath.Create();
374-
var exporter = MiniExcel.Exporters.GetMappingExporter(registry);
375-
await exporter.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), data);
374+
var templater = MiniExcel.Templaters.GetMappingTemplater(registry);
375+
await templater.ApplyTemplateAsync(outputPath.ToString(), templatePath.ToString(), data);
376376

377377
// Verify - should only update first item since mapping is for specific cells
378378
// Verify - use useHeaderRow=false since we want to see all rows

0 commit comments

Comments
 (0)