Add ReaderFactory
This commit is contained in:
parent
dcd42a32eb
commit
5720d2ede0
@ -25,8 +25,7 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
|
|||||||
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
|
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
|
||||||
.AddSingleton<IDatabaseClient, DatabaseClient>()
|
.AddSingleton<IDatabaseClient, DatabaseClient>()
|
||||||
.AddTransient<ICurrencyClient, CurrencyClient>()
|
.AddTransient<ICurrencyClient, CurrencyClient>()
|
||||||
.AddTransient<IFileDialog, FileDialog>()
|
.AddTransient<IFileDialog, FileDialog>();
|
||||||
.AddTransient<IReader, ExcelReader>();
|
|
||||||
|
|
||||||
Services.AddSingleton<WriterFactory>();
|
Services.AddSingleton<WriterFactory>();
|
||||||
Services.AddTransient<ExcelWriter>()
|
Services.AddTransient<ExcelWriter>()
|
||||||
@ -34,6 +33,10 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
|
|||||||
Services.AddTransient<DxfWriter>()
|
Services.AddTransient<DxfWriter>()
|
||||||
.AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>());
|
.AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>());
|
||||||
|
|
||||||
|
Services.AddSingleton<ReaderFactory>();
|
||||||
|
Services.AddTransient<ExcelReader>()
|
||||||
|
.AddTransient<IReader, ExcelReader>(s => s.GetService<ExcelReader>());
|
||||||
|
|
||||||
Services.AddSingleton<ToolFactory>();
|
Services.AddSingleton<ToolFactory>();
|
||||||
|
|
||||||
ServiceProvider = Services.BuildServiceProvider();
|
ServiceProvider = Services.BuildServiceProvider();
|
||||||
|
20
RhSolutions.AddIn/Services/ReaderFactory.cs
Normal file
20
RhSolutions.AddIn/Services/ReaderFactory.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
namespace RhSolutions.Services;
|
||||||
|
|
||||||
|
public class ReaderFactory
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
|
public ReaderFactory(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
_serviceProvider = serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReader GetReader(string readerName)
|
||||||
|
{
|
||||||
|
return readerName switch
|
||||||
|
{
|
||||||
|
"Excel" => (IReader)_serviceProvider.GetService(typeof(ExcelReader)),
|
||||||
|
_ => (IReader)_serviceProvider.GetService(typeof(ExcelReader))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,7 @@ public class WriterFactory
|
|||||||
{
|
{
|
||||||
return writerName switch
|
return writerName switch
|
||||||
{
|
{
|
||||||
|
"Excel" => (IWriter)_serviceProvider.GetService(typeof(ExcelWriter)),
|
||||||
"Dxf" => (IWriter)_serviceProvider.GetService(typeof(DxfWriter)),
|
"Dxf" => (IWriter)_serviceProvider.GetService(typeof(DxfWriter)),
|
||||||
_ => (IWriter)_serviceProvider.GetService(typeof(ExcelWriter))
|
_ => (IWriter)_serviceProvider.GetService(typeof(ExcelWriter))
|
||||||
};
|
};
|
||||||
|
@ -18,6 +18,7 @@ internal class ConvertTool : Tool
|
|||||||
{
|
{
|
||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
||||||
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
var products = _reader.ReadProducts(new[] { worksheet });
|
var products = _reader.ReadProducts(new[] { worksheet });
|
||||||
_writer = _writerFactory.GetWriter("Excel");
|
_writer = _writerFactory.GetWriter("Excel");
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
|
@ -17,6 +17,7 @@ internal class DxfTool : Tool
|
|||||||
{
|
{
|
||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
||||||
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
var products = _reader.ReadProducts(new[] { worksheet });
|
var products = _reader.ReadProducts(new[] { worksheet });
|
||||||
_writer = _writerFactory.GetWriter("Dxf");
|
_writer = _writerFactory.GetWriter("Dxf");
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
|
@ -17,6 +17,7 @@ internal class ExportTool : Tool
|
|||||||
public override void Execute()
|
public override void Execute()
|
||||||
{
|
{
|
||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
var products = _reader.ReadProducts(app.Selection);
|
var products = _reader.ReadProducts(app.Selection);
|
||||||
_writer = _writerFactory.GetWriter("Excel");
|
_writer = _writerFactory.GetWriter("Excel");
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
|
@ -17,6 +17,7 @@ internal class MergeTool : Tool
|
|||||||
{
|
{
|
||||||
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
|
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
|
||||||
string[] files = dialog.GetFiles();
|
string[] files = dialog.GetFiles();
|
||||||
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
var products = _reader.ReadProducts(files);
|
var products = _reader.ReadProducts(files);
|
||||||
_writer = _writerFactory.GetWriter("Excel");
|
_writer = _writerFactory.GetWriter("Excel");
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
|
@ -9,13 +9,14 @@ namespace RhSolutions.Tools;
|
|||||||
#endif
|
#endif
|
||||||
internal abstract class Tool : IDisposable
|
internal abstract class Tool : IDisposable
|
||||||
{
|
{
|
||||||
protected readonly IReader _reader;
|
protected readonly ReaderFactory _readerFactory;
|
||||||
protected readonly WriterFactory _writerFactory;
|
protected readonly WriterFactory _writerFactory;
|
||||||
|
protected IReader _reader;
|
||||||
protected IWriter _writer;
|
protected IWriter _writer;
|
||||||
|
|
||||||
public Tool(IServiceProvider provider)
|
public Tool(IServiceProvider provider)
|
||||||
{
|
{
|
||||||
_reader = provider.GetRequiredService<IReader>();
|
_readerFactory = provider.GetRequiredService<ReaderFactory>();
|
||||||
_writerFactory = provider.GetRequiredService<WriterFactory>();
|
_writerFactory = provider.GetRequiredService<WriterFactory>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user