Browse Source

Added Dispose to ExcelDynamicReader

Piotr Czajkowski 2 months ago
parent
commit
65a8eed2a7

+ 16 - 1
ExcelORM/ExcelORM/ExcelDynamicReader.cs

@@ -3,7 +3,7 @@ using ExcelORM.Models;
 
 namespace ExcelORM;
 
-public class ExcelDynamicReader
+public class ExcelDynamicReader : IDisposable
 {
     private readonly IXLWorkbook xlWorkbook;
     public bool SkipHidden { get; set; }
@@ -108,4 +108,19 @@ public class ExcelDynamicReader
             };
         }
     }
+
+    public void Dispose()
+    {
+        Dispose(true);
+        GC.SuppressFinalize(this);
+    }
+
+    protected virtual void Dispose(bool disposing)
+    {
+        if (disposing)
+        {
+            xlWorkbook?.Dispose();
+        }
+    }
+    ~ExcelDynamicReader() => Dispose(false);
 }

+ 4 - 4
ExcelORM/ExcelORMTests/DynamicReaderTests.cs

@@ -12,7 +12,7 @@ public class DynamicReaderTests
     [Fact]
     public void Read()
     {
-        var reader = new ExcelDynamicReader(RegularFile);
+        using var reader = new ExcelDynamicReader(RegularFile);
         var results = reader.Read("Sheet 1").ToArray();
         Assert.NotEmpty(results);
     }
@@ -20,7 +20,7 @@ public class DynamicReaderTests
     [Fact]
     public void ReadDifferentTypes()
     {
-        var reader = new ExcelDynamicReader(DifferentTypesFile);
+        using var reader = new ExcelDynamicReader(DifferentTypesFile);
         var results = reader.Read().ToArray();
         Assert.NotEmpty(results);
 
@@ -35,7 +35,7 @@ public class DynamicReaderTests
     [Fact]
     public void ReadAll()
     {
-        var reader = new ExcelDynamicReader(MultipleSheetsFile);
+        using var reader = new ExcelDynamicReader(MultipleSheetsFile);
         var results = reader.ReadAll().ToArray();
         Assert.NotEmpty(results);
     }
@@ -43,7 +43,7 @@ public class DynamicReaderTests
     [Fact]
     public void ReadDifficult()
     {
-        var reader = new ExcelDynamicReader(DifficultFile);
+        using var reader = new ExcelDynamicReader(DifficultFile);
         var results = reader.Read().ToArray();
         Assert.NotEmpty(results);
         Assert.Equal(results.First().Count, results.Last().Count);

+ 6 - 6
ExcelORM/ExcelORMTests/DynamicWriterTests.cs

@@ -14,7 +14,7 @@ public class DynamicWriterTests
         var testFile = Path.GetRandomFileName();
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
-        var reader = new ExcelDynamicReader(DifficultFile);
+        using var reader = new ExcelDynamicReader(DifficultFile);
         var results = reader.Read().ToArray();
         Assert.NotEmpty(results);
 
@@ -22,7 +22,7 @@ public class DynamicWriterTests
         writer.Write(results);
         writer.SaveAs(testFile);
 
-        var savedReader = new ExcelDynamicReader(testFile);
+        using var savedReader = new ExcelDynamicReader(testFile);
         var savedResults = savedReader.Read().ToArray();
         Assert.NotEmpty(savedResults);
         Assert.True(results.First().SequenceEqual(savedResults.First()));
@@ -37,7 +37,7 @@ public class DynamicWriterTests
         var testFile = Path.GetRandomFileName();
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
-        var reader = new ExcelDynamicReader(MultipleSheetsFile);
+        using var reader = new ExcelDynamicReader(MultipleSheetsFile);
         var results = reader.ReadAll().ToArray();
         Assert.NotEmpty(results);
         
@@ -45,7 +45,7 @@ public class DynamicWriterTests
         writer.WriteAll(results);
         writer.SaveAs(testFile);
 
-        var savedReader = new ExcelDynamicReader(testFile);
+        using var savedReader = new ExcelDynamicReader(testFile);
         var savedResults = savedReader.ReadAll().ToArray();
         Assert.NotEmpty(savedResults);
         Assert.Equal(results.First().Name, savedResults.First().Name);
@@ -60,7 +60,7 @@ public class DynamicWriterTests
     public void WriteReadInMemory()
     {
         using var readWorkbook = new XLWorkbook(DifficultFile);
-        var reader = new ExcelDynamicReader(readWorkbook);
+        using var reader = new ExcelDynamicReader(readWorkbook);
         var results = reader.Read().ToArray();
         Assert.NotEmpty(results);
 
@@ -68,7 +68,7 @@ public class DynamicWriterTests
         var writer = new ExcelDynamicWriter(writeWorkbook);
         writer.Write(results);
 
-        var savedReader = new ExcelDynamicReader(writeWorkbook);
+        using var savedReader = new ExcelDynamicReader(writeWorkbook);
         var savedResults = savedReader.Read().ToArray();
         Assert.NotEmpty(savedResults);
         Assert.True(results.First().SequenceEqual(savedResults.First()));