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
{
var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>();
using Tool tool = toolFactory.GetTool(control.Id);
using ITool tool = toolFactory.GetTool(control.Id);
tool.Execute();
}

View File

@ -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;

View File

@ -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;

View File

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

View File

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

View File

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

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
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<IFileDialog>();
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<IFileDialog>();
string[] files = dialog.GetFiles();
if (files.Length > 0)
{
_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;
internal class OcrTool : Tool
internal class OcrTool : ITool
{
public Application Application { get; set; }
private IOcrClient client = RhSolutionsAddIn.ServiceProvider.GetService<IOcrClient>();
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()
{
}
}

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
{
static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>();
static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>();
static FittingsCalculatorFactory fittingsCalculatorFactory = RhSolutionsAddIn.ServiceProvider.GetService<FittingsCalculatorFactory>();
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;
}
}