Browse Source

Added Dispose to ExcelReader

Piotr Czajkowski 2 months ago
parent
commit
ddfa0913ae

+ 16 - 2
ExcelORM/ExcelORM/ExcelReader.cs

@@ -1,11 +1,10 @@
 using System.Runtime.CompilerServices;
 using ClosedXML.Excel;
-using ExcelORM.Interfaces;
 using ExcelORM.Models;
 
 namespace ExcelORM;
 
-public class ExcelReader
+public class ExcelReader : IDisposable
 {
     private readonly IXLWorkbook xlWorkbook;
     public bool SkipHidden { get; set; }
@@ -110,4 +109,19 @@ public class ExcelReader
                 yield return item;
         }
     }
+
+    public void Dispose()
+    {
+        Dispose(true);
+        GC.SuppressFinalize(this);
+    }
+
+    protected virtual void Dispose(bool disposing)
+    {
+        if (disposing)
+        {
+            xlWorkbook?.Dispose();
+        }
+    }
+    ~ExcelReader() => Dispose(false);
 }

+ 11 - 11
ExcelORM/ExcelORMTests/ReaderTests.cs

@@ -15,7 +15,7 @@ public class ReaderTests
     [Fact]
     public void Read()
     {
-        var reader = new ExcelReader(RegularFile);
+        using var reader = new ExcelReader(RegularFile);
         var results = reader.Read<Test>().ToArray();
         Assert.NotEmpty(results);
     }
@@ -23,11 +23,11 @@ public class ReaderTests
     [Fact]
     public void ReadHidden()
     {
-        var reader = new ExcelReader(HiddenFile);
+        using var reader = new ExcelReader(HiddenFile);
         var results = reader.Read<Test>().ToArray();
         Assert.NotEmpty(results);
 
-        var readerHidden = new ExcelReader(HiddenFile) { SkipHidden = true };
+        using var readerHidden = new ExcelReader(HiddenFile) { SkipHidden = true };
         var resultsHidden = readerHidden.Read<Test>().ToArray();
         Assert.NotEmpty(resultsHidden);
         Assert.NotEqual(results.Length, resultsHidden.Length);
@@ -36,11 +36,11 @@ public class ReaderTests
     [Fact]
     public void ReadFiltered()
     {
-        var reader = new ExcelReader(FilteredFile);
+        using var reader = new ExcelReader(FilteredFile);
         var results = reader.Read<Test>().ToArray();
         Assert.NotEmpty(results);
 
-        var readerFiltered = new ExcelReader(FilteredFile) { ObeyFilter = true };
+        using var readerFiltered = new ExcelReader(FilteredFile) { ObeyFilter = true };
         var resultsFiltered = readerFiltered.Read<Test>().ToArray();
         Assert.NotEmpty(resultsFiltered);
         Assert.NotEqual(results.Length, resultsFiltered.Length);
@@ -49,7 +49,7 @@ public class ReaderTests
     [Fact]
     public void ReadDifficult()
     {
-        var reader = new ExcelReader(DifficultFile);
+        using var reader = new ExcelReader(DifficultFile);
         var results = reader.Read<Test>("Tab").ToArray();
         Assert.NotEmpty(results);
 
@@ -66,7 +66,7 @@ public class ReaderTests
     [Fact]
     public void ReadMultipleSheets()
     {
-        var reader = new ExcelReader(MultipleSheetsFile);
+        using var reader = new ExcelReader(MultipleSheetsFile);
         var results = reader.ReadAll<Test>().ToArray();
         Assert.NotEmpty(results);
         Assert.Equal(6, results.Length);
@@ -75,7 +75,7 @@ public class ReaderTests
     [Fact]
     public void ReadDifferentTypes()
     {
-        var reader = new ExcelReader(DifferentTypesFile);
+        using var reader = new ExcelReader(DifferentTypesFile);
         var results = reader.Read<TestTypes>().ToArray();
         Assert.NotEmpty(results);
     }
@@ -83,7 +83,7 @@ public class ReaderTests
     [Fact]
     public void ReadDifferentTypesWithSkip()
     {
-        var reader = new ExcelReader(DifferentTypesFile);
+        using var reader = new ExcelReader(DifferentTypesFile);
         var results = reader.Read<TestSkip>().ToArray();
         Assert.NotEmpty(results);
         Assert.All(results, x => Assert.Null(x.Text));
@@ -94,7 +94,7 @@ public class ReaderTests
     [Fact]
     public void ReadDifferentTypesWithSkipMiddle()
     {
-        var reader = new ExcelReader(DifferentTypesFile);
+        using var reader = new ExcelReader(DifferentTypesFile);
         var results = reader.Read<TestSkipMiddle>().ToArray();
         Assert.NotEmpty(results);
         Assert.NotNull(results.FirstOrDefault(x => x.Text != null));
@@ -105,7 +105,7 @@ public class ReaderTests
     [Fact]
     public void ReadWithFormula()
     {
-        var reader = new ExcelReader(WithFormulaFile);
+        using var reader = new ExcelReader(WithFormulaFile);
         var results = reader.Read<TestWithFormula>().ToArray();
         Assert.NotEmpty(results);
     }

+ 15 - 15
ExcelORM/ExcelORMTests/WriterTests.cs

@@ -30,7 +30,7 @@ public class WriterTests
         writer.Write(arrayOfThree, worksheetName);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<Test>(worksheetName).ToArray();
         Assert.Equal(3, readArray.Length);
 
@@ -40,8 +40,8 @@ public class WriterTests
         writer.Write(listOfTwo, worksheetName, true);
         writer.SaveAs(testFile);
         
-        reader = new ExcelReader(testFile);
-        Assert.Equal(5, reader.Read<Test>(worksheetName).Count());
+        using var newReader = new ExcelReader(testFile);
+        Assert.Equal(5, newReader.Read<Test>(worksheetName).Count());
         File.Delete(testFile);
     }
     
@@ -55,14 +55,14 @@ public class WriterTests
         writer.Write(arrayOfThree);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         Assert.Equal(arrayOfThree.Length, reader.Read<Test>().Count());
 
         writer.Write(listOfTwo, append: true);
         writer.SaveAs(testFile);
         
-        reader = new ExcelReader(testFile);
-        Assert.Equal(5, reader.Read<Test>().Count());
+        using var newReader = new ExcelReader(testFile);
+        Assert.Equal(5, newReader.Read<Test>().Count());
         File.Delete(testFile);
     }
 
@@ -80,7 +80,7 @@ public class WriterTests
         writer.Write(arrayOfThree, append: true, headerRowIndex: headerRowIndex);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<Test>(startFrom: headerRowIndex).ToArray();
         Assert.Equal(6, readArray.Length);
 
@@ -103,7 +103,7 @@ public class WriterTests
         writer.Write(arrayOfThree, append: true);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<Test>().ToArray();
         Assert.Equal(6, readArray.Length);
 
@@ -134,7 +134,7 @@ public class WriterTests
         writer.Write(list);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var result = reader.Read<TestTypes>().ToList();
         Assert.Single(result);
         var first = result.First();
@@ -165,7 +165,7 @@ public class WriterTests
         writer.Write(arrayWithSkip, worksheetName);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<TestSkip>(worksheetName).ToArray();
         Assert.Equal(arrayWithSkip.Length, readArray.Length);
 
@@ -196,7 +196,7 @@ public class WriterTests
         writer.Write(arrayWithSkipMiddle, worksheetName);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<TestSkipMiddle>(worksheetName).ToArray();
         Assert.Equal(arrayWithSkipMiddle.Length, readArray.Length);
 
@@ -227,7 +227,7 @@ public class WriterTests
         writer.Write(arrayWithFormulas);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<TestWithFormula>().ToArray();
         Assert.Equal(arrayWithFormulas.Length, readArray.Length);
 
@@ -256,7 +256,7 @@ public class WriterTests
         writer.Write(arrayNumbersWithFormulas);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<TestNumbersWithFormula>().ToArray();
         Assert.Equal(arrayNumbersWithFormulas.Length, readArray.Length);
 
@@ -286,7 +286,7 @@ public class WriterTests
         writer.Write(arrayWithHyperlinks);
         writer.SaveAs(testFile);
 
-        var reader = new ExcelReader(testFile);
+        using var reader = new ExcelReader(testFile);
         var readArray = reader.Read<TestWithHyperlink>().ToArray();
         Assert.Equal(arrayWithFormulas.Length, readArray.Length);
 
@@ -309,7 +309,7 @@ public class WriterTests
         using var writer = new ExcelWriter(workbook);
         writer.Write(arrayOfThree, worksheetName);
 
-        var reader = new ExcelReader(workbook);
+        using var reader = new ExcelReader(workbook);
         var readArray = reader.Read<Test>(worksheetName).ToArray();
         Assert.Equal(3, readArray.Length);