diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2023-05-22 07:39:48 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2023-05-22 07:39:48 +0300 |
commit | 5720d2ede0688bbdf759ff853f894fe3b18a69f9 (patch) | |
tree | ebc7880f2bbde20665842c176b1eb8aa45f1b71b | |
parent | dcd42a32eb0c40aa51b4243d9155bb17361ec9cf (diff) |
Add ReaderFactory
-rw-r--r-- | RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 7 | ||||
-rw-r--r-- | RhSolutions.AddIn/Services/ReaderFactory.cs | 20 | ||||
-rw-r--r-- | RhSolutions.AddIn/Services/WriterFactory.cs | 1 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/ConvertTool.cs | 1 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/DxfTool.cs | 1 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/ExportTool.cs | 1 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/MergeTool.cs | 1 | ||||
-rw-r--r-- | RhSolutions.AddIn/Tools/Tool.cs | 5 |
8 files changed, 33 insertions, 4 deletions
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<IAddInConfiguration, AddInConfiguration>() .AddSingleton<IDatabaseClient, DatabaseClient>() .AddTransient<ICurrencyClient, CurrencyClient>() - .AddTransient<IFileDialog, FileDialog>() - .AddTransient<IReader, ExcelReader>(); + .AddTransient<IFileDialog, FileDialog>(); Services.AddSingleton<WriterFactory>(); Services.AddTransient<ExcelWriter>() @@ -34,6 +33,10 @@ public sealed class RhSolutionsAddIn : IExcelAddIn Services.AddTransient<DxfWriter>() .AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>()); + Services.AddSingleton<ReaderFactory>(); + Services.AddTransient<ExcelReader>() + .AddTransient<IReader, ExcelReader>(s => s.GetService<ExcelReader>()); + Services.AddSingleton<ToolFactory>(); 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<IFileDialog>(); 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<IReader>(); + _readerFactory = provider.GetRequiredService<ReaderFactory>(); _writerFactory = provider.GetRequiredService<WriterFactory>(); } |