From 5720d2ede0688bbdf759ff853f894fe3b18a69f9 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 22 May 2023 07:39:48 +0300 Subject: [PATCH] Add ReaderFactory --- RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 7 +++++-- RhSolutions.AddIn/Services/ReaderFactory.cs | 20 ++++++++++++++++++++ RhSolutions.AddIn/Services/WriterFactory.cs | 1 + RhSolutions.AddIn/Tools/ConvertTool.cs | 1 + RhSolutions.AddIn/Tools/DxfTool.cs | 1 + RhSolutions.AddIn/Tools/ExportTool.cs | 1 + RhSolutions.AddIn/Tools/MergeTool.cs | 1 + RhSolutions.AddIn/Tools/Tool.cs | 5 +++-- 8 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 RhSolutions.AddIn/Services/ReaderFactory.cs diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index 004cace..4a6f16a 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -25,8 +25,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn .AddSingleton() .AddSingleton() .AddTransient() - .AddTransient() - .AddTransient(); + .AddTransient(); Services.AddSingleton(); Services.AddTransient() @@ -34,6 +33,10 @@ public sealed class RhSolutionsAddIn : IExcelAddIn Services.AddTransient() .AddTransient(s => s.GetService()); + Services.AddSingleton(); + Services.AddTransient() + .AddTransient(s => s.GetService()); + Services.AddSingleton(); ServiceProvider = Services.BuildServiceProvider(); diff --git a/RhSolutions.AddIn/Services/ReaderFactory.cs b/RhSolutions.AddIn/Services/ReaderFactory.cs new file mode 100644 index 0000000..390ccd0 --- /dev/null +++ b/RhSolutions.AddIn/Services/ReaderFactory.cs @@ -0,0 +1,20 @@ +namespace RhSolutions.Services; + +public class ReaderFactory +{ + private readonly IServiceProvider _serviceProvider; + + public ReaderFactory(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public IReader GetReader(string readerName) + { + return readerName switch + { + "Excel" => (IReader)_serviceProvider.GetService(typeof(ExcelReader)), + _ => (IReader)_serviceProvider.GetService(typeof(ExcelReader)) + }; + } +} diff --git a/RhSolutions.AddIn/Services/WriterFactory.cs b/RhSolutions.AddIn/Services/WriterFactory.cs index 444a62f..66d4102 100644 --- a/RhSolutions.AddIn/Services/WriterFactory.cs +++ b/RhSolutions.AddIn/Services/WriterFactory.cs @@ -13,6 +13,7 @@ public class WriterFactory { return writerName switch { + "Excel" => (IWriter)_serviceProvider.GetService(typeof(ExcelWriter)), "Dxf" => (IWriter)_serviceProvider.GetService(typeof(DxfWriter)), _ => (IWriter)_serviceProvider.GetService(typeof(ExcelWriter)) }; diff --git a/RhSolutions.AddIn/Tools/ConvertTool.cs b/RhSolutions.AddIn/Tools/ConvertTool.cs index cbaaad6..277c527 100644 --- a/RhSolutions.AddIn/Tools/ConvertTool.cs +++ b/RhSolutions.AddIn/Tools/ConvertTool.cs @@ -18,6 +18,7 @@ internal class ConvertTool : Tool { Application app = RhSolutionsAddIn.Excel.Application; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; + _reader = _readerFactory.GetReader("Excel"); var products = _reader.ReadProducts(new[] { worksheet }); _writer = _writerFactory.GetWriter("Excel"); _writer.WriteProducts(products); diff --git a/RhSolutions.AddIn/Tools/DxfTool.cs b/RhSolutions.AddIn/Tools/DxfTool.cs index e011e5d..266a9fa 100644 --- a/RhSolutions.AddIn/Tools/DxfTool.cs +++ b/RhSolutions.AddIn/Tools/DxfTool.cs @@ -17,6 +17,7 @@ internal class DxfTool : Tool { Application app = RhSolutionsAddIn.Excel.Application; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; + _reader = _readerFactory.GetReader("Excel"); var products = _reader.ReadProducts(new[] { worksheet }); _writer = _writerFactory.GetWriter("Dxf"); _writer.WriteProducts(products); diff --git a/RhSolutions.AddIn/Tools/ExportTool.cs b/RhSolutions.AddIn/Tools/ExportTool.cs index ec60d10..ec3efb4 100644 --- a/RhSolutions.AddIn/Tools/ExportTool.cs +++ b/RhSolutions.AddIn/Tools/ExportTool.cs @@ -17,6 +17,7 @@ internal class ExportTool : Tool public override void Execute() { Application app = RhSolutionsAddIn.Excel.Application; + _reader = _readerFactory.GetReader("Excel"); var products = _reader.ReadProducts(app.Selection); _writer = _writerFactory.GetWriter("Excel"); _writer.WriteProducts(products); diff --git a/RhSolutions.AddIn/Tools/MergeTool.cs b/RhSolutions.AddIn/Tools/MergeTool.cs index 345dea7..5b71227 100644 --- a/RhSolutions.AddIn/Tools/MergeTool.cs +++ b/RhSolutions.AddIn/Tools/MergeTool.cs @@ -17,6 +17,7 @@ internal class MergeTool : Tool { IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService(); string[] files = dialog.GetFiles(); + _reader = _readerFactory.GetReader("Excel"); var products = _reader.ReadProducts(files); _writer = _writerFactory.GetWriter("Excel"); _writer.WriteProducts(products); diff --git a/RhSolutions.AddIn/Tools/Tool.cs b/RhSolutions.AddIn/Tools/Tool.cs index c9ae815..29fa854 100644 --- a/RhSolutions.AddIn/Tools/Tool.cs +++ b/RhSolutions.AddIn/Tools/Tool.cs @@ -9,13 +9,14 @@ namespace RhSolutions.Tools; #endif internal abstract class Tool : IDisposable { - protected readonly IReader _reader; + protected readonly ReaderFactory _readerFactory; protected readonly WriterFactory _writerFactory; + protected IReader _reader; protected IWriter _writer; public Tool(IServiceProvider provider) { - _reader = provider.GetRequiredService(); + _readerFactory = provider.GetRequiredService(); _writerFactory = provider.GetRequiredService(); }