Browse Source

Added Dispose to ExcelWriter

Piotr Czajkowski 3 months ago
parent
commit
c98fe62b7d
2 changed files with 27 additions and 12 deletions
  1. 16 1
      ExcelORM/ExcelORM/ExcelWriter.cs
  2. 11 11
      ExcelORM/ExcelORMTests/WriterTests.cs

+ 16 - 1
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -6,7 +6,7 @@ using ExcelORM.Models;
 
 namespace ExcelORM;
 
-public class ExcelWriter
+public class ExcelWriter : IDisposable
 {
     private readonly IXLWorkbook xlWorkbook;
     public ExcelWriter(string? path = null)
@@ -119,4 +119,19 @@ public class ExcelWriter
         xlWorkbook.SaveAs(path);
         converter?.MakeCompatible(path);
     } 
+
+    public void Dispose()
+    {
+        Dispose(true);
+        GC.SuppressFinalize(this);
+    }
+
+    protected virtual void Dispose(bool disposing)
+    {
+        if (disposing)
+        {
+            xlWorkbook?.Dispose();
+        }
+    }
+    ~ExcelWriter() => Dispose(false);
 }

+ 11 - 11
ExcelORM/ExcelORMTests/WriterTests.cs

@@ -26,7 +26,7 @@ public class WriterTests
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
         const string worksheetName = "Test";
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayOfThree, worksheetName);
         writer.SaveAs(testFile);
 
@@ -51,7 +51,7 @@ public class WriterTests
         var testFile = Path.GetRandomFileName();
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayOfThree);
         writer.SaveAs(testFile);
 
@@ -76,7 +76,7 @@ public class WriterTests
         File.Copy(ForAppend, testFile);
 
         uint headerRowIndex = 3;
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayOfThree, append: true, headerRowIndex: headerRowIndex);
         writer.SaveAs(testFile);
 
@@ -99,7 +99,7 @@ public class WriterTests
         testFile = Path.ChangeExtension(testFile, "xlsx");
         File.Copy(ForAppendHeaderFirst, testFile);
 
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayOfThree, append: true);
         writer.SaveAs(testFile);
 
@@ -130,7 +130,7 @@ public class WriterTests
         
         var list = new List<TestTypes>{ expected };
         
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(list);
         writer.SaveAs(testFile);
 
@@ -161,7 +161,7 @@ public class WriterTests
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
         const string worksheetName = "Test";
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayWithSkip, worksheetName);
         writer.SaveAs(testFile);
 
@@ -192,7 +192,7 @@ public class WriterTests
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
         const string worksheetName = "Test";
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayWithSkipMiddle, worksheetName);
         writer.SaveAs(testFile);
 
@@ -223,7 +223,7 @@ public class WriterTests
         var testFile = Path.GetRandomFileName();
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayWithFormulas);
         writer.SaveAs(testFile);
 
@@ -252,7 +252,7 @@ public class WriterTests
         var testFile = Path.GetRandomFileName();
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayNumbersWithFormulas);
         writer.SaveAs(testFile);
 
@@ -282,7 +282,7 @@ public class WriterTests
         var testFile = Path.GetRandomFileName();
         testFile = Path.ChangeExtension(testFile, "xlsx");
 
-        var writer = new ExcelWriter(testFile);
+        using var writer = new ExcelWriter(testFile);
         writer.Write(arrayWithHyperlinks);
         writer.SaveAs(testFile);
 
@@ -306,7 +306,7 @@ public class WriterTests
     {
         using var workbook = new XLWorkbook();
         const string worksheetName = "Test";
-        var writer = new ExcelWriter(workbook);
+        using var writer = new ExcelWriter(workbook);
         writer.Write(arrayOfThree, worksheetName);
 
         var reader = new ExcelReader(workbook);