From 3800c961166dbcc4553dd846d0291388e60d7e6f Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Sat, 9 Nov 2024 22:59:46 +0300 Subject: [PATCH] Tools refactoring --- .../Controllers/RibbonController.cs | 2 +- RhSolutions.AddIn/Tools/ConvertTool.cs | 18 ++------- RhSolutions.AddIn/Tools/DxfTool.cs | 17 ++------ RhSolutions.AddIn/Tools/ExportTool.cs | 34 ++++++---------- RhSolutions.AddIn/Tools/FittingsTool.cs | 9 ++--- RhSolutions.AddIn/Tools/GuessTool.cs | 33 ++++++---------- RhSolutions.AddIn/Tools/ITool.cs | 9 +++++ RhSolutions.AddIn/Tools/MergeTool.cs | 39 +++++++------------ RhSolutions.AddIn/Tools/OcrTool.cs | 12 +++--- RhSolutions.AddIn/Tools/ReaderWriterTool.cs | 15 +++++++ RhSolutions.AddIn/Tools/Tool.cs | 30 -------------- RhSolutions.AddIn/Tools/ToolFactory.cs | 28 ++++++------- 12 files changed, 88 insertions(+), 158 deletions(-) create mode 100644 RhSolutions.AddIn/Tools/ITool.cs create mode 100644 RhSolutions.AddIn/Tools/ReaderWriterTool.cs delete mode 100644 RhSolutions.AddIn/Tools/Tool.cs diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index 9123940..310f0fe 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -71,7 +71,7 @@ public class RibbonController : ExcelRibbon try { var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService(); - using Tool tool = toolFactory.GetTool(control.Id); + using ITool tool = toolFactory.GetTool(control.Id); tool.Execute(); } diff --git a/RhSolutions.AddIn/Tools/ConvertTool.cs b/RhSolutions.AddIn/Tools/ConvertTool.cs index e77ef6d..f94ad7c 100644 --- a/RhSolutions.AddIn/Tools/ConvertTool.cs +++ b/RhSolutions.AddIn/Tools/ConvertTool.cs @@ -1,20 +1,8 @@ -using RhSolutions.AddIn; -#if !NET472 -using System.Runtime.Versioning; -#endif +namespace RhSolutions.Tools; -namespace RhSolutions.Tools; - -#if !NET472 -[SupportedOSPlatform("windows")] -#endif -internal class ConvertTool : Tool +internal class ConvertTool : ReaderWriterTool, ITool { - public ConvertTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) - { - } - - public override void Execute() + public void Execute() { Application app = RhSolutionsAddIn.Excel.Application; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; diff --git a/RhSolutions.AddIn/Tools/DxfTool.cs b/RhSolutions.AddIn/Tools/DxfTool.cs index fc4e6b7..14624f9 100644 --- a/RhSolutions.AddIn/Tools/DxfTool.cs +++ b/RhSolutions.AddIn/Tools/DxfTool.cs @@ -1,19 +1,8 @@ -#if !NET472 -using System.Runtime.Versioning; -#endif +namespace RhSolutions.Tools; -namespace RhSolutions.Tools; - -#if !NET472 -[SupportedOSPlatform("windows")] -#endif -internal class DxfTool : Tool +internal class DxfTool : ReaderWriterTool, ITool { - public DxfTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) - { - } - - public override void Execute() + public void Execute() { Application app = RhSolutionsAddIn.Excel.Application; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; diff --git a/RhSolutions.AddIn/Tools/ExportTool.cs b/RhSolutions.AddIn/Tools/ExportTool.cs index aefb906..cf6f725 100644 --- a/RhSolutions.AddIn/Tools/ExportTool.cs +++ b/RhSolutions.AddIn/Tools/ExportTool.cs @@ -1,25 +1,13 @@ -#if !NET472 -using System.Runtime.Versioning; -#endif +namespace RhSolutions.Tools; -namespace RhSolutions.Tools; - -#if !NET472 -[SupportedOSPlatform("windows")] -#endif -internal class ExportTool : Tool +internal class ExportTool : ReaderWriterTool, ITool { - public ExportTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) - { - } - - public override void Execute() - { - Application app = RhSolutionsAddIn.Excel.Application; - _reader = _readerFactory.GetReader("Excel"); - var products = _reader.ReadProducts(app.Selection); - _writer = _writerFactory.GetWriter("NewPrice"); - _writer.WriteProducts(products); - } -} - + public void Execute() + { + Application app = RhSolutionsAddIn.Excel.Application; + _reader = _readerFactory.GetReader("Excel"); + var products = _reader.ReadProducts(app.Selection); + _writer = _writerFactory.GetWriter("NewPrice"); + _writer.WriteProducts(products); + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/FittingsTool.cs b/RhSolutions.AddIn/Tools/FittingsTool.cs index 806b346..990facd 100644 --- a/RhSolutions.AddIn/Tools/FittingsTool.cs +++ b/RhSolutions.AddIn/Tools/FittingsTool.cs @@ -1,17 +1,16 @@ namespace RhSolutions.Tools; -internal class FittingsTool : Tool +internal class FittingsTool : ReaderWriterTool, ITool { - private readonly FittingsCalculatorFactory _factory; + private readonly FittingsCalculatorFactory _factory = RhSolutionsAddIn.ServiceProvider.GetService(); private string _calculatorName; - public FittingsTool(ReaderFactory readerFactory, WriterFactory writerFactory, FittingsCalculatorFactory calculatorFactory, string calculatorName) : base(readerFactory, writerFactory) + public FittingsTool(string calculatorName) { - _factory = calculatorFactory; _calculatorName = calculatorName; } - public override void Execute() + public void Execute() { Application app = RhSolutionsAddIn.Excel.Application; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; diff --git a/RhSolutions.AddIn/Tools/GuessTool.cs b/RhSolutions.AddIn/Tools/GuessTool.cs index 43ecfca..fcedc1c 100644 --- a/RhSolutions.AddIn/Tools/GuessTool.cs +++ b/RhSolutions.AddIn/Tools/GuessTool.cs @@ -1,23 +1,14 @@ -#if !NET472 -using System.Runtime.Versioning; -#endif +namespace RhSolutions.Tools; -namespace RhSolutions.Tools; - -internal class GuessTool : Tool +internal class GuessTool : ReaderWriterTool, ITool { - public GuessTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) - { - } - - public override void Execute() - { - Application app = RhSolutionsAddIn.Excel.Application; - Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; - _reader = _readerFactory.GetReader("Guess"); - var products = _reader.ReadProducts(new[] { worksheet }); - _writer = _writerFactory.GetWriter("NewPrice"); - _writer.WriteProducts(products); - } -} - + public void Execute() + { + Application app = RhSolutionsAddIn.Excel.Application; + Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; + _reader = _readerFactory.GetReader("Guess"); + var products = _reader.ReadProducts(new[] { worksheet }); + _writer = _writerFactory.GetWriter("NewPrice"); + _writer.WriteProducts(products); + } +} \ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/ITool.cs b/RhSolutions.AddIn/Tools/ITool.cs new file mode 100644 index 0000000..35e1ac9 --- /dev/null +++ b/RhSolutions.AddIn/Tools/ITool.cs @@ -0,0 +1,9 @@ +#if !NET472 +#endif + +namespace RhSolutions.Tools; + +internal interface ITool: IDisposable +{ + public void Execute(); +} diff --git a/RhSolutions.AddIn/Tools/MergeTool.cs b/RhSolutions.AddIn/Tools/MergeTool.cs index 62d7e73..831e471 100644 --- a/RhSolutions.AddIn/Tools/MergeTool.cs +++ b/RhSolutions.AddIn/Tools/MergeTool.cs @@ -1,28 +1,17 @@ -#if !NET472 -using System.Runtime.Versioning; -#endif +namespace RhSolutions.Tools; -namespace RhSolutions.Tools; - -#if !NET472 -[SupportedOSPlatform("windows")] -#endif -internal class MergeTool : Tool +internal class MergeTool : ReaderWriterTool, ITool { - public MergeTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) - { - } - - public override void Execute() - { - IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService(); - string[] files = dialog.GetFiles(); - if (files.Length > 0) - { - _reader = _readerFactory.GetReader("Excel"); - var products = _reader.ReadProducts(files); - _writer = _writerFactory.GetWriter("NewPrice"); - _writer.WriteProducts(products); - } - } + public void Execute() + { + IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService(); + string[] files = dialog.GetFiles(); + if (files.Length > 0) + { + _reader = _readerFactory.GetReader("Excel"); + var products = _reader.ReadProducts(files); + _writer = _writerFactory.GetWriter("NewPrice"); + _writer.WriteProducts(products); + } + } } diff --git a/RhSolutions.AddIn/Tools/OcrTool.cs b/RhSolutions.AddIn/Tools/OcrTool.cs index 56909f8..4579984 100644 --- a/RhSolutions.AddIn/Tools/OcrTool.cs +++ b/RhSolutions.AddIn/Tools/OcrTool.cs @@ -4,16 +4,11 @@ using OcrClient.Services; namespace RhSolutions.Tools; -internal class OcrTool : Tool +internal class OcrTool : ITool { - public Application Application { get; set; } private IOcrClient client = RhSolutionsAddIn.ServiceProvider.GetService(); - public OcrTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) - { - } - - public override void Execute() + public void Execute() { try { @@ -35,4 +30,7 @@ internal class OcrTool : Tool RhSolutionsAddIn.Excel.Visible = true; } } + public void Dispose() + { + } } \ No newline at end of file diff --git a/RhSolutions.AddIn/Tools/ReaderWriterTool.cs b/RhSolutions.AddIn/Tools/ReaderWriterTool.cs new file mode 100644 index 0000000..77389f0 --- /dev/null +++ b/RhSolutions.AddIn/Tools/ReaderWriterTool.cs @@ -0,0 +1,15 @@ +namespace RhSolutions.Tools; + +internal abstract class ReaderWriterTool: IDisposable +{ + protected IReader _reader; + protected IWriter _writer; + + protected ReaderFactory _readerFactory = RhSolutionsAddIn.ServiceProvider.GetService(); + protected WriterFactory _writerFactory = RhSolutionsAddIn.ServiceProvider.GetService(); + public void Dispose() + { + _reader?.Dispose(); + _writer?.Dispose(); + } +} diff --git a/RhSolutions.AddIn/Tools/Tool.cs b/RhSolutions.AddIn/Tools/Tool.cs deleted file mode 100644 index efb66f0..0000000 --- a/RhSolutions.AddIn/Tools/Tool.cs +++ /dev/null @@ -1,30 +0,0 @@ -#if !NET472 -using System.Runtime.Versioning; -#endif - -namespace RhSolutions.Tools; - -#if !NET472 -[SupportedOSPlatform("windows")] -#endif -internal abstract class Tool : IDisposable -{ - protected readonly ReaderFactory _readerFactory; - protected readonly WriterFactory _writerFactory; - protected IReader _reader; - protected IWriter _writer; - - public Tool(ReaderFactory readerFactory, WriterFactory writerFactory) - { - _readerFactory = readerFactory; - _writerFactory = writerFactory; - } - - public void Dispose() - { - _reader?.Dispose(); - _writer?.Dispose(); - } - - public abstract void Execute(); -} diff --git a/RhSolutions.AddIn/Tools/ToolFactory.cs b/RhSolutions.AddIn/Tools/ToolFactory.cs index a7f7afc..ca8841f 100644 --- a/RhSolutions.AddIn/Tools/ToolFactory.cs +++ b/RhSolutions.AddIn/Tools/ToolFactory.cs @@ -1,26 +1,20 @@ -using OcrClient.Services; - -namespace RhSolutions.Tools; +namespace RhSolutions.Tools; internal class ToolFactory { - static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService(); - static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService(); - static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService(); - public Tool GetTool(string toolName) + public ITool GetTool(string toolName) { - Tool tool = toolName switch + return toolName switch { - "export" => new ExportTool(readerFactory, writerFactory), - "convert" => new ConvertTool(readerFactory, writerFactory), - "merge" => new MergeTool(readerFactory, writerFactory), - "dxfexport" => new DxfTool(readerFactory, writerFactory), - "guess" => new GuessTool(readerFactory, writerFactory), - "fillsleeves" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Sleeves"), - "fillcouplings" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Couplings"), - "ocr" => new OcrTool(readerFactory, writerFactory), + "export" => new ExportTool(), + "convert" => new ConvertTool(), + "merge" => new MergeTool(), + "dxfexport" => new DxfTool(), + "guess" => new GuessTool(), + "fillsleeves" => new FittingsTool("Sleeves"), + "fillcouplings" => new FittingsTool("Couplings"), + "ocr" => new OcrTool(), _ => throw new Exception($"Неизвестный инструмент {toolName}"), }; - return tool; } }