Tools refactoring
This commit is contained in:
parent
cc52668afc
commit
3800c96116
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
@ -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;
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
#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()
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Execute()
|
|
||||||
{
|
{
|
||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
_reader = _readerFactory.GetReader("Excel");
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
@ -22,4 +11,3 @@ internal class ExportTool : Tool
|
|||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
@ -1,16 +1,8 @@
|
|||||||
#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()
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Execute()
|
|
||||||
{
|
{
|
||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
||||||
@ -20,4 +12,3 @@ internal class GuessTool : Tool
|
|||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
RhSolutions.AddIn/Tools/ITool.cs
Normal file
9
RhSolutions.AddIn/Tools/ITool.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#if !NET472
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace RhSolutions.Tools;
|
||||||
|
|
||||||
|
internal interface ITool: IDisposable
|
||||||
|
{
|
||||||
|
public void Execute();
|
||||||
|
}
|
@ -1,19 +1,8 @@
|
|||||||
#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()
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Execute()
|
|
||||||
{
|
{
|
||||||
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
|
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
|
||||||
string[] files = dialog.GetFiles();
|
string[] files = dialog.GetFiles();
|
||||||
|
@ -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()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
15
RhSolutions.AddIn/Tools/ReaderWriterTool.cs
Normal file
15
RhSolutions.AddIn/Tools/ReaderWriterTool.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user