Tools refactoring

This commit is contained in:
Serghei Cebotari 2024-11-09 22:59:46 +03:00
parent cc52668afc
commit 3800c96116
12 changed files with 88 additions and 158 deletions

View File

@ -71,7 +71,7 @@ public class RibbonController : ExcelRibbon
try try
{ {
var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>(); var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>();
using Tool tool = toolFactory.GetTool(control.Id); using ITool tool = toolFactory.GetTool(control.Id);
tool.Execute(); tool.Execute();
} }

View File

@ -1,20 +1,8 @@
using RhSolutions.AddIn; namespace RhSolutions.Tools;
#if !NET472
using System.Runtime.Versioning;
#endif
namespace RhSolutions.Tools; internal class ConvertTool : ReaderWriterTool, ITool
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class ConvertTool : Tool
{ {
public ConvertTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) public void Execute()
{
}
public override void Execute()
{ {
Application app = RhSolutionsAddIn.Excel.Application; Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;

View File

@ -1,19 +1,8 @@
#if !NET472 namespace RhSolutions.Tools;
using System.Runtime.Versioning;
#endif
namespace RhSolutions.Tools; internal class DxfTool : ReaderWriterTool, ITool
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class DxfTool : Tool
{ {
public DxfTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) public void Execute()
{
}
public override void Execute()
{ {
Application app = RhSolutionsAddIn.Excel.Application; Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;

View File

@ -1,25 +1,13 @@
#if !NET472 namespace RhSolutions.Tools;
using System.Runtime.Versioning;
#endif
namespace RhSolutions.Tools; internal class ExportTool : ReaderWriterTool, ITool
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class ExportTool : Tool
{ {
public ExportTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) public void Execute()
{ {
} Application app = RhSolutionsAddIn.Excel.Application;
_reader = _readerFactory.GetReader("Excel");
public override void Execute() var products = _reader.ReadProducts(app.Selection);
{ _writer = _writerFactory.GetWriter("NewPrice");
Application app = RhSolutionsAddIn.Excel.Application; _writer.WriteProducts(products);
_reader = _readerFactory.GetReader("Excel"); }
var products = _reader.ReadProducts(app.Selection); }
_writer = _writerFactory.GetWriter("NewPrice");
_writer.WriteProducts(products);
}
}

View File

@ -1,17 +1,16 @@
namespace RhSolutions.Tools; namespace RhSolutions.Tools;
internal class FittingsTool : Tool internal class FittingsTool : ReaderWriterTool, ITool
{ {
private readonly FittingsCalculatorFactory _factory; private readonly FittingsCalculatorFactory _factory = RhSolutionsAddIn.ServiceProvider.GetService<FittingsCalculatorFactory>();
private string _calculatorName; private string _calculatorName;
public FittingsTool(ReaderFactory readerFactory, WriterFactory writerFactory, FittingsCalculatorFactory calculatorFactory, string calculatorName) : base(readerFactory, writerFactory) public FittingsTool(string calculatorName)
{ {
_factory = calculatorFactory;
_calculatorName = calculatorName; _calculatorName = calculatorName;
} }
public override void Execute() public void Execute()
{ {
Application app = RhSolutionsAddIn.Excel.Application; Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;

View File

@ -1,23 +1,14 @@
#if !NET472 namespace RhSolutions.Tools;
using System.Runtime.Versioning;
#endif
namespace RhSolutions.Tools; internal class GuessTool : ReaderWriterTool, ITool
internal class GuessTool : Tool
{ {
public GuessTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) public void Execute()
{ {
} Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
public override void Execute() _reader = _readerFactory.GetReader("Guess");
{ var products = _reader.ReadProducts(new[] { worksheet });
Application app = RhSolutionsAddIn.Excel.Application; _writer = _writerFactory.GetWriter("NewPrice");
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet; _writer.WriteProducts(products);
_reader = _readerFactory.GetReader("Guess"); }
var products = _reader.ReadProducts(new[] { worksheet }); }
_writer = _writerFactory.GetWriter("NewPrice");
_writer.WriteProducts(products);
}
}

View File

@ -0,0 +1,9 @@
#if !NET472
#endif
namespace RhSolutions.Tools;
internal interface ITool: IDisposable
{
public void Execute();
}

View File

@ -1,28 +1,17 @@
#if !NET472 namespace RhSolutions.Tools;
using System.Runtime.Versioning;
#endif
namespace RhSolutions.Tools; internal class MergeTool : ReaderWriterTool, ITool
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class MergeTool : Tool
{ {
public MergeTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) public void Execute()
{ {
} IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
string[] files = dialog.GetFiles();
public override void Execute() if (files.Length > 0)
{ {
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>(); _reader = _readerFactory.GetReader("Excel");
string[] files = dialog.GetFiles(); var products = _reader.ReadProducts(files);
if (files.Length > 0) _writer = _writerFactory.GetWriter("NewPrice");
{ _writer.WriteProducts(products);
_reader = _readerFactory.GetReader("Excel"); }
var products = _reader.ReadProducts(files); }
_writer = _writerFactory.GetWriter("NewPrice");
_writer.WriteProducts(products);
}
}
} }

View File

@ -4,16 +4,11 @@ using OcrClient.Services;
namespace RhSolutions.Tools; namespace RhSolutions.Tools;
internal class OcrTool : Tool internal class OcrTool : ITool
{ {
public Application Application { get; set; }
private IOcrClient client = RhSolutionsAddIn.ServiceProvider.GetService<IOcrClient>(); private IOcrClient client = RhSolutionsAddIn.ServiceProvider.GetService<IOcrClient>();
public OcrTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory) public void Execute()
{
}
public override void Execute()
{ {
try try
{ {
@ -35,4 +30,7 @@ internal class OcrTool : Tool
RhSolutionsAddIn.Excel.Visible = true; RhSolutionsAddIn.Excel.Visible = true;
} }
} }
public void Dispose()
{
}
} }

View File

@ -0,0 +1,15 @@
namespace RhSolutions.Tools;
internal abstract class ReaderWriterTool: IDisposable
{
protected IReader _reader;
protected IWriter _writer;
protected ReaderFactory _readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>();
protected WriterFactory _writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>();
public void Dispose()
{
_reader?.Dispose();
_writer?.Dispose();
}
}

View File

@ -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();
}

View File

@ -1,26 +1,20 @@
using OcrClient.Services; namespace RhSolutions.Tools;
namespace RhSolutions.Tools;
internal class ToolFactory internal class ToolFactory
{ {
static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>(); public ITool GetTool(string toolName)
static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>();
static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService<FittingsCalculatorFactory>();
public Tool GetTool(string toolName)
{ {
Tool tool = toolName switch return toolName switch
{ {
"export" => new ExportTool(readerFactory, writerFactory), "export" => new ExportTool(),
"convert" => new ConvertTool(readerFactory, writerFactory), "convert" => new ConvertTool(),
"merge" => new MergeTool(readerFactory, writerFactory), "merge" => new MergeTool(),
"dxfexport" => new DxfTool(readerFactory, writerFactory), "dxfexport" => new DxfTool(),
"guess" => new GuessTool(readerFactory, writerFactory), "guess" => new GuessTool(),
"fillsleeves" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Sleeves"), "fillsleeves" => new FittingsTool("Sleeves"),
"fillcouplings" => new FittingsTool(readerFactory, writerFactory, fittingsCalculatorFactory, "Couplings"), "fillcouplings" => new FittingsTool("Couplings"),
"ocr" => new OcrTool(readerFactory, writerFactory), "ocr" => new OcrTool(),
_ => throw new Exception($"Неизвестный инструмент {toolName}"), _ => throw new Exception($"Неизвестный инструмент {toolName}"),
}; };
return tool;
} }
} }