Add Tool factory
This commit is contained in:
parent
3f4d7f45b7
commit
b6254b1565
@ -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>();
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ namespace RhSolutions.Tools;
|
||||
#if !NET472
|
||||
[SupportedOSPlatform("windows")]
|
||||
#endif
|
||||
internal class ConvertTool : ToolBase
|
||||
internal class ConvertTool : Tool
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
|
@ -7,7 +7,7 @@ namespace RhSolutions.Tools;
|
||||
#if !NET472
|
||||
[SupportedOSPlatform("windows")]
|
||||
#endif
|
||||
internal class DxfTool : ToolBase
|
||||
internal class DxfTool : Tool
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
|
@ -8,7 +8,7 @@ namespace RhSolutions.Tools;
|
||||
#if !NET472
|
||||
[SupportedOSPlatform("windows")]
|
||||
#endif
|
||||
internal class ExportTool : ToolBase
|
||||
internal class ExportTool : Tool
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
|
@ -11,7 +11,7 @@ namespace RhSolutions.Tools;
|
||||
#if !NET472
|
||||
[SupportedOSPlatform("windows")]
|
||||
#endif
|
||||
internal class MergeTool : ToolBase
|
||||
internal class MergeTool : Tool
|
||||
{
|
||||
public override void Execute()
|
||||
{
|
||||
|
@ -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>();
|
17
RhSolutions.AddIn/Tools/ToolFactory.cs
Normal file
17
RhSolutions.AddIn/Tools/ToolFactory.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user