Skip to content

Commit a0a1285

Browse files
Refactoring of the templating system (#936)
* Refactoring some tests * Added ConfiguredAwaitDisposable calls to implement the IAsyncDisposable pattern where possible * Moving utility classes from OpenXmlTemplate.Impl.cs to a separate file and other minor changes * Renaming all method overloads of the OpenXmlTemplater from ApplyTemplate to FillTemplate
1 parent 87ae5b9 commit a0a1285

22 files changed

Lines changed: 419 additions & 812 deletions

benchmarks/MiniExcel.Benchmarks/BenchmarkSections/TemplateExcelBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void MiniExcel_Template_Generate_Test()
5151
})
5252
};
5353

54-
_templater.ApplyTemplate(path.FilePath, templatePath, value);
54+
_templater.FillTemplate(path.FilePath, templatePath, value);
5555
}
5656

5757
[Benchmark(Description = "ClosedXml.Report Template Generate")]

benchmarks/MiniExcel.Benchmarks/BenchmarkSections/XlsxAsyncBenchmark.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public async Task MiniExcel_Template_Generate_Async_Test()
4242
})
4343
};
4444

45-
await _templater.ApplyTemplateAsync(path.FilePath, templatePath, value);
45+
await _templater.FillTemplateAsync(path.FilePath, templatePath, value);
4646
}
4747
}

src/MiniExcel.Csv/Api/CsvImporter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,11 @@ public async Task<ICollection<string>> GetColumnNamesAsync(string path, bool use
135135
public async Task<ICollection<string>> GetColumnNamesAsync(Stream stream, bool useHeaderRow = false,
136136
CsvConfiguration? configuration = null, CancellationToken cancellationToken = default)
137137
{
138-
#pragma warning disable CA2007 // We need to assign the AsyncEnumerator before we can call ConfigureAwait on it
139-
await using var enumerator = QueryAsync(stream, useHeaderRow, configuration, cancellationToken).GetAsyncEnumerator(cancellationToken);
140-
#pragma warning restore CA2007
138+
var enumerator = QueryAsync(stream, useHeaderRow, configuration, cancellationToken).GetAsyncEnumerator(cancellationToken);
139+
await using var disposableEnumerator = enumerator.ConfigureAwait(false);
141140

142-
_ = enumerator.ConfigureAwait(false);
143141
if (await enumerator.MoveNextAsync().ConfigureAwait(false))
144-
{
145142
return (enumerator.Current as IDictionary<string, object>)?.Keys ?? [];
146-
}
147143

148144
return [];
149145
}

src/MiniExcel.OpenXml/Api/OpenXmlImporter.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,11 @@ public async Task<ICollection<string>> GetColumnNamesAsync(Stream stream, bool u
251251
string? sheetName = null, string startCell = "A1", OpenXmlConfiguration? configuration = null,
252252
CancellationToken cancellationToken = default)
253253
{
254-
#pragma warning disable CA2007 // We need to assign the AsyncEnumerator before we can call ConfigureAwait on it
255-
await using var enumerator = QueryAsync(stream, useHeaderRow, sheetName, startCell, configuration, cancellationToken).GetAsyncEnumerator(cancellationToken);
256-
#pragma warning restore CA2007
254+
var enumerator = QueryAsync(stream, useHeaderRow, sheetName, startCell, configuration, cancellationToken).GetAsyncEnumerator(cancellationToken);
255+
await using var disposableEnumerator = enumerator.ConfigureAwait(false);
257256

258-
_ = enumerator.ConfigureAwait(false);
259257
if (await enumerator.MoveNextAsync().ConfigureAwait(false))
260-
{
261258
return (enumerator.Current as IDictionary<string, object?>)?.Keys ?? [];
262-
}
263259

264260
return [];
265261
}

src/MiniExcel.OpenXml/Api/OpenXmlTemplater.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public async Task AddPictureAsync(Stream excelStream, CancellationToken cancella
2424
}
2525

2626
[CreateSyncVersion]
27-
public async Task ApplyTemplateAsync(string path, string templatePath, object value, bool overwriteFile = false,
27+
public async Task FillTemplateAsync(string path, string templatePath, object value, bool overwriteFile = false,
2828
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
2929
{
3030
using var stream = overwriteFile ? File.Create(path) : File.Open(path, FileMode.CreateNew);
31-
await ApplyTemplateAsync(stream, templatePath, value, configuration, cancellationToken).ConfigureAwait(false);
31+
await FillTemplateAsync(stream, templatePath, value, configuration, cancellationToken).ConfigureAwait(false);
3232
}
3333

3434
[CreateSyncVersion]
35-
public async Task ApplyTemplateAsync(string path, Stream templateStream, object value, bool overwriteFile = false,
35+
public async Task FillTemplateAsync(string path, Stream templateStream, object value, bool overwriteFile = false,
3636
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
3737
{
3838
using var stream = overwriteFile ? File.Create(path) : File.Open(path, FileMode.CreateNew);
@@ -41,31 +41,31 @@ public async Task ApplyTemplateAsync(string path, Stream templateStream, object
4141
}
4242

4343
[CreateSyncVersion]
44-
public async Task ApplyTemplateAsync(Stream stream, string templatePath, object value,
44+
public async Task FillTemplateAsync(Stream stream, string templatePath, object value,
4545
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
4646
{
4747
var template = GetOpenXmlTemplate(stream, configuration);
4848
await template.SaveAsByTemplateAsync(templatePath, value, cancellationToken).ConfigureAwait(false);
4949
}
5050

5151
[CreateSyncVersion]
52-
public async Task ApplyTemplateAsync(Stream stream, Stream templateStream, object value,
52+
public async Task FillTemplateAsync(Stream stream, Stream templateStream, object value,
5353
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
5454
{
5555
var template = GetOpenXmlTemplate(stream, configuration);
5656
await template.SaveAsByTemplateAsync(templateStream, value, cancellationToken).ConfigureAwait(false);
5757
}
5858

5959
[CreateSyncVersion]
60-
public async Task ApplyTemplateAsync(string path, byte[] templateBytes, object value, bool overwriteFile = false,
60+
public async Task FillTemplateAsync(string path, byte[] templateBytes, object value, bool overwriteFile = false,
6161
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
6262
{
6363
using var stream = overwriteFile ? File.Create(path) : File.Open(path, FileMode.CreateNew);
64-
await ApplyTemplateAsync(stream, templateBytes, value, configuration, cancellationToken).ConfigureAwait(false);
64+
await FillTemplateAsync(stream, templateBytes, value, configuration, cancellationToken).ConfigureAwait(false);
6565
}
6666

6767
[CreateSyncVersion]
68-
public async Task ApplyTemplateAsync(Stream stream, byte[] templateBytes, object value,
68+
public async Task FillTemplateAsync(Stream stream, byte[] templateBytes, object value,
6969
OpenXmlConfiguration? configuration = null, CancellationToken cancellationToken = default)
7070
{
7171
var template = GetOpenXmlTemplate(stream, configuration);

src/MiniExcel.OpenXml/OpenXmlWriter.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ private async Task<int> CreateSheetXmlAsync(object? values, string sheetPath, IP
184184
var rowsWritten = 0;
185185

186186
#if NET10_0_OR_GREATER
187-
using var zipStream = await entry.OpenAsync(cancellationToken).ConfigureAwait(false);
187+
var zipStream = await entry.OpenAsync(cancellationToken).ConfigureAwait(false);
188+
await using var disposableZipStream = zipStream.ConfigureAwait(false);
188189
#else
189190
using var zipStream = entry.Open();
190191
#endif
@@ -618,17 +619,18 @@ private async Task InsertContentTypesXmlAsync(CancellationToken cancellationToke
618619
await GenerateContentTypesXmlAsync(cancellationToken).ConfigureAwait(false);
619620
return;
620621
}
622+
621623
#if NET5_0_OR_GREATER
622-
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
623624
#if NET10_0_OR_GREATER
624-
await using var stream = await contentTypesZipEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
625+
var stream = await contentTypesZipEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
625626
#else
626-
await using var stream = contentTypesZipEntry.Open();
627+
var stream = contentTypesZipEntry.Open();
627628
#endif
628-
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
629+
await using var disposableStream = stream.ConfigureAwait(false);
629630
#else
630631
using var stream = contentTypesZipEntry.Open();
631632
#endif
633+
632634
#if NETCOREAPP2_0_OR_GREATER
633635
var doc = await XDocument.LoadAsync(stream, LoadOptions.None, cancellationToken).ConfigureAwait(false);
634636
#else
@@ -672,18 +674,17 @@ private async Task CreateZipEntryAsync(string path, string? contentType, string
672674
var entry = _archive.CreateEntry(path, CompressionLevel.Fastest);
673675

674676
#if NET5_0_OR_GREATER
675-
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
676677
#if NET10_0_OR_GREATER
677-
await using (var zipStream = await entry.OpenAsync(cancellationToken).ConfigureAwait(false))
678+
var zipStream = await entry.OpenAsync(cancellationToken).ConfigureAwait(false);
678679
#else
679-
await using (var zipStream = entry.Open())
680+
var zipStream = entry.Open();
680681
#endif
681-
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
682+
await using var disposableZipStream = zipStream.ConfigureAwait(false);
682683
#else
683-
using (var zipStream = entry.Open())
684+
using var zipStream = entry.Open();
684685
#endif
685-
using (var writer = new EnhancedStreamWriter(zipStream, Utf8WithBom, _configuration.BufferSize))
686-
await writer.WriteAsync(content, cancellationToken).ConfigureAwait(false);
686+
using var writer = new EnhancedStreamWriter(zipStream, Utf8WithBom, _configuration.BufferSize);
687+
await writer.WriteAsync(content, cancellationToken).ConfigureAwait(false);
687688

688689
if (!string.IsNullOrEmpty(contentType))
689690
_zipDictionary.Add(path, new ZipPackageInfo(entry, contentType));
@@ -697,17 +698,16 @@ private async Task CreateZipEntryAsync(string path, byte[] content, Cancellation
697698
var entry = _archive.CreateEntry(path, CompressionLevel.Fastest);
698699

699700
#if NET5_0_OR_GREATER
700-
#pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task
701701
#if NET10_0_OR_GREATER
702-
await using var zipStream = await entry.OpenAsync(cancellationToken).ConfigureAwait(false);
702+
var zipStream = await entry.OpenAsync(cancellationToken).ConfigureAwait(false);
703703
#else
704-
await using var zipStream = entry.Open();
704+
var zipStream = entry.Open();
705705
#endif
706+
await using var disposableZipStream = zipStream.ConfigureAwait(false);
706707
await zipStream.WriteAsync(content, cancellationToken).ConfigureAwait(false);
707-
#pragma warning restore CA2007 // Consider calling ConfigureAwait on the awaited task
708708
#else
709709
using var zipStream = entry.Open();
710710
await zipStream.WriteAsync(content, 0, content.Length, cancellationToken).ConfigureAwait(false);
711711
#endif
712712
}
713-
}
713+
}

0 commit comments

Comments
 (0)