Add Tool factory

This commit is contained in:
Sergey Chebotar 2023-04-14 08:08:46 +03:00
parent 3f4d7f45b7
commit b6254b1565
8 changed files with 27 additions and 14 deletions

View File

@ -26,6 +26,8 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
.AddTransient<IExcelReader, RhExcelReader>()
.AddTransient<IExcelWriter, RhExcelWriter>();
Services.AddSingleton<ToolFactory>();
ServiceProvider = Services.BuildServiceProvider();
Configuration = ServiceProvider.GetService<IAddInConfiguration>();
Excel = ServiceProvider.GetService<Application>();

View File

@ -64,14 +64,8 @@ public class RibbonController : ExcelRibbon
{
try
{
using ToolBase tool = control.Id switch
{
"export" => new ExportTool(),
"convert" => new ConvertTool(),
"merge" => new MergeTool(),
"dxfexport" => new DxfTool(),
_ => throw new Exception("Неизвестный инструмент"),
};
var toolFactory = RhSolutionsAddIn.ServiceProvider.GetService<ToolFactory>();
using Tool tool = toolFactory.GetTool(control.Id);
tool.Execute();
}

View File

@ -8,7 +8,7 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class ConvertTool : ToolBase
internal class ConvertTool : Tool
{
public override void Execute()
{

View File

@ -7,7 +7,7 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class DxfTool : ToolBase
internal class DxfTool : Tool
{
public override void Execute()
{

View File

@ -8,7 +8,7 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class ExportTool : ToolBase
internal class ExportTool : Tool
{
public override void Execute()
{

View File

@ -11,7 +11,7 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class MergeTool : ToolBase
internal class MergeTool : Tool
{
public override void Execute()
{

View File

@ -7,12 +7,12 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal abstract class ToolBase : IDisposable
internal abstract class Tool : IDisposable
{
protected readonly IExcelReader _reader;
protected readonly IExcelWriter _writer;
public ToolBase()
public Tool()
{
_reader = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IExcelReader>();
_writer = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IExcelWriter>();

View File

@ -0,0 +1,17 @@
namespace RhSolutions.Tools;
internal class ToolFactory
{
public Tool GetTool(string toolName)
{
Tool tool = toolName switch
{
"export" => new ExportTool(),
"convert" => new ConvertTool(),
"merge" => new MergeTool(),
"dxfexport" => new DxfTool(),
_ => throw new Exception("Неизвестный инструмент"),
};
return tool;
}
}