Этап 3. Зависимости
This commit is contained in:
parent
a5df575dab
commit
76fce8f036
1
.gitignore
vendored
1
.gitignore
vendored
@ -398,3 +398,4 @@ FodyWeavers.xsd
|
||||
# JetBrains Rider
|
||||
*.sln.iml
|
||||
|
||||
*.dll
|
||||
|
@ -0,0 +1,22 @@
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using FurnitureAssemFileImplement.Implements;
|
||||
|
||||
namespace FurnitureAssemFileImplement
|
||||
{
|
||||
public class FileImplementationExtension : IImplementationExtension
|
||||
{
|
||||
public int Priority => 1;
|
||||
|
||||
public void RegisterServices()
|
||||
{
|
||||
DependencyManager.Instance.RegisterType<IClientStorage, ClientStorage>();
|
||||
DependencyManager.Instance.RegisterType<IComponentStorage, ComponentStorage>();
|
||||
DependencyManager.Instance.RegisterType<IImplementerStorage, ImplementerStorage>();
|
||||
DependencyManager.Instance.RegisterType<IMessageInfoStorage, MessageInfoStorage>();
|
||||
DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>();
|
||||
DependencyManager.Instance.RegisterType<IFurnitureStorage, FurnitureStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
@ -11,4 +11,8 @@
|
||||
<ProjectReference Include="..\FurnitureAssemblyDataModels\FurnitureAssemblyDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
<Exec Command="copy /Y "$(TargetDir)*.dll" "$(SolutionDir)ImplementationExtensions\*.dll"" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
@ -1,6 +1,7 @@
|
||||
using FurnitureAssemblyBusinessLogic;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -48,31 +49,25 @@ namespace FurnitureAssembly
|
||||
|
||||
private void ButtonAdd_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormComponent));
|
||||
if (service is FormComponent form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormComponent>();
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ButtonUpd_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (dataGridView.SelectedRows.Count == 1)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormComponent));
|
||||
if (service is FormComponent form)
|
||||
{
|
||||
form.Id =
|
||||
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||||
var form = DependencyManager.Instance.Resolve<FormComponent>();
|
||||
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using FurnitureAssemblyContracts.SearchModels;
|
||||
using FurnitureAssemblyDataModels.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@ -86,10 +87,8 @@ namespace FurnitureAssembly
|
||||
|
||||
private void ButtonAdd_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormFurnitureComponent));
|
||||
if (service is FormFurnitureComponent form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormFurnitureComponent>();
|
||||
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
if (form.ComponentModel == null)
|
||||
@ -109,15 +108,14 @@ namespace FurnitureAssembly
|
||||
}
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ButtonUpd_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (dataGridViewFurn.SelectedRows.Count == 1)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormFurnitureComponent));
|
||||
var service = DependencyManager.Instance.Resolve<FormFurnitureComponent>();
|
||||
if (service is FormFurnitureComponent form)
|
||||
{
|
||||
int id =
|
||||
|
@ -1,6 +1,7 @@
|
||||
using FurnitureAssemblyBusinessLogic;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -47,31 +48,27 @@ namespace FurnitureAssembly
|
||||
|
||||
private void ButtonAdd_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormFurniture));
|
||||
if (service is FormFurniture form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormFurniture>();
|
||||
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ButtonUpd_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (dataGridView.SelectedRows.Count == 1)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormFurniture));
|
||||
if (service is FormFurniture form)
|
||||
{
|
||||
form.Id =
|
||||
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||||
var form = DependencyManager.Instance.Resolve<FormFurniture>();
|
||||
|
||||
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
using FurnitureAssemblyBusinessLogic;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
@ -39,31 +40,27 @@ namespace FurnitureAssembly
|
||||
|
||||
private void ButtonAdd_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormImplementer));
|
||||
if (service is FormImplementer form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormImplementer>();
|
||||
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ButtonUpd_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (dataGridView.SelectedRows.Count == 1)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormImplementer));
|
||||
if (service is FormImplementer form)
|
||||
{
|
||||
form.Id =
|
||||
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||||
var form = DependencyManager.Instance.Resolve<FormImplementer>();
|
||||
|
||||
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadData();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
using FurnitureAssemblyBusinessLogic;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -50,32 +51,27 @@ namespace FurnitureAssembly
|
||||
}
|
||||
private void КомпонентыToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormComponents));
|
||||
if (service is FormComponents form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormComponents>();
|
||||
|
||||
form.ShowDialog();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ИзделияToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormFurnitures));
|
||||
if (service is FormFurnitures form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormFurnitures>();
|
||||
|
||||
form.ShowDialog();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void ButtonCreateOrder_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder));
|
||||
if (service is FormCreateOrder form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormCreateOrder>();
|
||||
|
||||
form.ShowDialog();
|
||||
LoadData();
|
||||
}
|
||||
|
||||
}
|
||||
private void ButtonTakeOrderInWork_Click(object sender, EventArgs e)
|
||||
{
|
||||
@ -173,55 +169,43 @@ namespace FurnitureAssembly
|
||||
private void FurnitureComponentsToolStripMenuItem_Click(object sender,
|
||||
EventArgs e)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormReportFurnitureComponents));
|
||||
if (service is FormReportFurnitureComponents form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormReportFurnitureComponents>();
|
||||
|
||||
form.ShowDialog();
|
||||
}
|
||||
|
||||
}
|
||||
private void OrdersToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service =
|
||||
Program.ServiceProvider?.GetService(typeof(FormReportOrders));
|
||||
if (service is FormReportOrders form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormReportOrders>();
|
||||
|
||||
form.ShowDialog();
|
||||
}
|
||||
}
|
||||
|
||||
private void ClientsMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormClients));
|
||||
if (service is FormClients form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormClients>();
|
||||
form.ShowDialog();
|
||||
}
|
||||
}
|
||||
|
||||
private void ImplementersToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormImplementers));
|
||||
if (service is FormImplementers form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormImplementers>();
|
||||
|
||||
form.ShowDialog();
|
||||
}
|
||||
}
|
||||
|
||||
private void doWorkToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
_workModeling.DoWork((
|
||||
Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!,
|
||||
DependencyManager.Instance.Resolve<IImplementerLogic>() as IImplementerLogic)!,
|
||||
_orderLogic);
|
||||
}
|
||||
|
||||
private void mailToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormMail));
|
||||
if (service is FormMail form)
|
||||
{
|
||||
var form = DependencyManager.Instance.Resolve<FormMail>();
|
||||
form.ShowDialog();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void CreateBackUpToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
|
@ -2,20 +2,16 @@ using FurnitureAssemblyBusinessLogic;
|
||||
using FurnitureAssemblyBusinessLogic.OfficePackage.Implements;
|
||||
using FurnitureAssemblyBusinessLogic.OfficePackage;
|
||||
using FurnitureAssemblyContracts.BusinessLogicsContarcts;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using FurnitureAssemblyDatabaseImplement.Implements;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
using FurnitureAssemblyBusinessLogic.MailWorker;
|
||||
using FurnitureAssemblyContracts.BindingModels;
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
|
||||
namespace FurnitureAssembly
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
private static ServiceProvider? _serviceProvider;
|
||||
public static ServiceProvider? ServiceProvider => _serviceProvider;
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
@ -25,13 +21,11 @@ namespace FurnitureAssembly
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
var services = new ServiceCollection();
|
||||
ConfigureServices(services);
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
InitDependency();
|
||||
|
||||
try
|
||||
{
|
||||
var mailSender =
|
||||
_serviceProvider.GetService<AbstractMailWorker>();
|
||||
var mailSender = DependencyManager.Instance.Resolve<AbstractMailWorker>();
|
||||
mailSender?.MailConfig(new MailConfigBindingModel
|
||||
{
|
||||
MailLogin =
|
||||
@ -54,61 +48,56 @@ namespace FurnitureAssembly
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = _serviceProvider.GetService<ILogger>();
|
||||
var logger = DependencyManager.Instance.Resolve<ILogger>();
|
||||
logger?.LogError(ex, "Îøèáêà ðàáîòû ñ ïî÷òîé");
|
||||
}
|
||||
Application.Run(_serviceProvider.GetRequiredService<FormMain>());
|
||||
Application.Run(DependencyManager.Instance.Resolve<FormMain>());
|
||||
|
||||
}
|
||||
|
||||
private static void ConfigureServices(ServiceCollection services)
|
||||
|
||||
|
||||
private static void InitDependency()
|
||||
{
|
||||
services.AddLogging(option =>
|
||||
DependencyManager.InitDependency();
|
||||
|
||||
DependencyManager.Instance.AddLogging(option =>
|
||||
{
|
||||
option.SetMinimumLevel(LogLevel.Information);
|
||||
option.AddNLog("nlog.config");
|
||||
});
|
||||
services.AddTransient<IComponentStorage, ComponentStorage>();
|
||||
services.AddTransient<IOrderStorage, OrderStorage>();
|
||||
services.AddTransient<IFurnitureStorage, FurnitureStorage>();
|
||||
services.AddTransient<IClientStorage, ClientStorage>();
|
||||
services.AddTransient<IImplementerStorage, ImplementerStorage>();
|
||||
services.AddTransient<IMessageInfoStorage, MessageInfoStorage>();
|
||||
services.AddTransient<IBackUpInfo, BackUpInfo>();
|
||||
|
||||
DependencyManager.Instance.RegisterType<IComponentLogic, ComponentLogic>();
|
||||
DependencyManager.Instance.RegisterType<IOrderLogic, OrderLogic>();
|
||||
DependencyManager.Instance.RegisterType<IFurnitureLogic, FurnitureLogic>();
|
||||
DependencyManager.Instance.RegisterType<IReportLogic, ReportLogic>();
|
||||
DependencyManager.Instance.RegisterType<IClientLogic, ClientLogic>();
|
||||
DependencyManager.Instance.RegisterType<IImplementerLogic, ImplementerLogic>();
|
||||
DependencyManager.Instance.RegisterType<IMessageInfoLogic, MessageInfoLogic>();
|
||||
DependencyManager.Instance.RegisterType<IWorkProcess, WorkModeling>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpLogic, BackUpLogic>();
|
||||
|
||||
services.AddTransient<IComponentLogic, ComponentLogic>();
|
||||
services.AddTransient<IOrderLogic, OrderLogic>();
|
||||
services.AddTransient<IFurnitureLogic, FurnitureLogic>();
|
||||
services.AddTransient<IReportLogic, ReportLogic>();
|
||||
services.AddTransient<IClientLogic, ClientLogic>();
|
||||
services.AddTransient<IImplementerLogic, ImplementerLogic>();
|
||||
services.AddTransient<IWorkProcess, WorkModeling>();
|
||||
services.AddTransient<IMessageInfoLogic, MessageInfoLogic>();
|
||||
DependencyManager.Instance.RegisterType<AbstractMailWorker, MailKitWorker>(true);
|
||||
|
||||
services.AddSingleton<AbstractMailWorker, MailKitWorker>();
|
||||
services.AddTransient<IBackUpLogic, BackUpLogic>();
|
||||
DependencyManager.Instance.RegisterType<AbstractSaveToExcel, SaveToExcel>();
|
||||
DependencyManager.Instance.RegisterType<AbstractSaveToWord, SaveToWord>();
|
||||
DependencyManager.Instance.RegisterType<AbstractSaveToPdf, SaveToPdf>();
|
||||
|
||||
|
||||
services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
|
||||
services.AddTransient<AbstractSaveToWord, SaveToWord>();
|
||||
services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
|
||||
|
||||
services.AddTransient<FormMain>();
|
||||
services.AddTransient<FormComponent>();
|
||||
services.AddTransient<FormComponents>();
|
||||
services.AddTransient<FormCreateOrder>();
|
||||
services.AddTransient<FormFurniture>();
|
||||
services.AddTransient<FormFurnitureComponent>();
|
||||
services.AddTransient<FormFurnitures>();
|
||||
services.AddTransient<FormReportFurnitureComponents>();
|
||||
services.AddTransient<FormReportOrders>();
|
||||
services.AddTransient<FormClients>();
|
||||
services.AddTransient<FormImplementers>();
|
||||
services.AddTransient<FormImplementer>();
|
||||
services.AddTransient<FormMail>();
|
||||
DependencyManager.Instance.RegisterType<FormMain>();
|
||||
DependencyManager.Instance.RegisterType<FormComponent>();
|
||||
DependencyManager.Instance.RegisterType<FormComponents>();
|
||||
DependencyManager.Instance.RegisterType<FormCreateOrder>();
|
||||
DependencyManager.Instance.RegisterType<FormFurniture>();
|
||||
DependencyManager.Instance.RegisterType<FormFurnitureComponent>();
|
||||
DependencyManager.Instance.RegisterType<FormFurnitures>();
|
||||
DependencyManager.Instance.RegisterType<FormReportFurnitureComponents>();
|
||||
DependencyManager.Instance.RegisterType<FormReportOrders>();
|
||||
DependencyManager.Instance.RegisterType<FormClients>();
|
||||
DependencyManager.Instance.RegisterType<FormImplementers>();
|
||||
DependencyManager.Instance.RegisterType<FormImplementer>();
|
||||
DependencyManager.Instance.RegisterType<FormMail>();
|
||||
}
|
||||
|
||||
private static void MailCheck(object obj) => ServiceProvider?.GetService<AbstractMailWorker>()?.MailCheck();
|
||||
private static void MailCheck(object obj) => DependencyManager.Instance.Resolve<AbstractMailWorker>()?.MailCheck();
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DI
|
||||
{
|
||||
public class DependencyManager
|
||||
{
|
||||
private readonly IDependencyContainer _dependencyManager;
|
||||
private static DependencyManager? _manager;
|
||||
private static readonly object _locjObject = new();
|
||||
private DependencyManager()
|
||||
{
|
||||
_dependencyManager = new ServiceDependencyContainer();
|
||||
}
|
||||
public static DependencyManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_manager == null) { lock (_locjObject) { _manager = new DependencyManager(); } }
|
||||
return
|
||||
_manager;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Иницализация библиотек, в которых идут установки зависомстей
|
||||
/// </summary>
|
||||
public static void InitDependency()
|
||||
{
|
||||
var ext = ServiceProviderLoader.GetImplementationExtensions();
|
||||
if (ext == null)
|
||||
{
|
||||
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
|
||||
}
|
||||
// регистрируем зависимости
|
||||
ext.RegisterServices();
|
||||
}
|
||||
/// <summary>
|
||||
/// Регистрация логгера
|
||||
/// </summary>
|
||||
/// <param name="configure"></param>
|
||||
public void AddLogging(Action<ILoggingBuilder> configure) =>
|
||||
_dependencyManager.AddLogging(configure);
|
||||
/// <summary>
|
||||
/// Добавление зависимости
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="U"></typeparam>
|
||||
public void RegisterType<T, U>(bool isSingle = false) where U :
|
||||
class, T where T : class => _dependencyManager.RegisterType<T, U>(isSingle);
|
||||
/// <summary>
|
||||
/// Добавление зависимости
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="U"></typeparam>
|
||||
public void RegisterType<T>(bool isSingle = false) where T : class =>
|
||||
_dependencyManager.RegisterType<T>(isSingle);
|
||||
/// <summary>
|
||||
/// Получение класса со всеми зависмостями
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public T Resolve<T>() => _dependencyManager.Resolve<T>();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DI
|
||||
{
|
||||
public interface IDependencyContainer
|
||||
{
|
||||
/// <summary>
|
||||
/// Регистрация логгера
|
||||
/// </summary>
|
||||
/// <param name="configure"></param>
|
||||
void AddLogging(Action<ILoggingBuilder> configure);
|
||||
/// <summary>
|
||||
/// Добавление зависимости
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="U"></typeparam>
|
||||
/// <param name="isSingle"></param>
|
||||
void RegisterType<T, U>(bool isSingle) where U : class, T where T :
|
||||
class;
|
||||
/// <summary>
|
||||
/// Добавление зависимости
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="isSingle"></param>
|
||||
void RegisterType<T>(bool isSingle) where T : class;
|
||||
/// <summary>
|
||||
/// Получение класса со всеми зависмостями
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
T Resolve<T>();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace FurnitureAssemblyContracts.DI
|
||||
{
|
||||
public interface IImplementationExtension
|
||||
{
|
||||
public int Priority { get; }
|
||||
/// <summary>
|
||||
/// Регистрация сервисов
|
||||
/// </summary>
|
||||
public void RegisterServices();
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DI
|
||||
{
|
||||
public class ServiceDependencyContainer : IDependencyContainer
|
||||
{
|
||||
private ServiceProvider? _serviceProvider;
|
||||
|
||||
private readonly ServiceCollection _serviceCollection;
|
||||
|
||||
public ServiceDependencyContainer()
|
||||
{
|
||||
_serviceCollection = new ServiceCollection();
|
||||
}
|
||||
|
||||
public void AddLogging(Action<ILoggingBuilder> configure)
|
||||
{
|
||||
_serviceCollection.AddLogging(configure);
|
||||
}
|
||||
|
||||
public void RegisterType<T, U>(bool isSingle) where U : class, T where T : class
|
||||
{
|
||||
if (isSingle)
|
||||
{
|
||||
_serviceCollection.AddSingleton<T, U>();
|
||||
}
|
||||
else
|
||||
{
|
||||
_serviceCollection.AddTransient<T, U>();
|
||||
}
|
||||
_serviceProvider = null;
|
||||
}
|
||||
|
||||
public void RegisterType<T>(bool isSingle) where T : class
|
||||
{
|
||||
if (isSingle)
|
||||
{
|
||||
_serviceCollection.AddSingleton<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
_serviceCollection.AddTransient<T>();
|
||||
}
|
||||
_serviceProvider = null;
|
||||
}
|
||||
|
||||
public T Resolve<T>()
|
||||
{
|
||||
if (_serviceProvider == null)
|
||||
{
|
||||
_serviceProvider = _serviceCollection.BuildServiceProvider();
|
||||
}
|
||||
return _serviceProvider.GetService<T>()!;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DI
|
||||
{
|
||||
public static partial class ServiceProviderLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// Загрузка всех классов-реализаций IImplementationExtension
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IImplementationExtension? GetImplementationExtensions()
|
||||
{
|
||||
IImplementationExtension? source = null;
|
||||
var files = Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll",
|
||||
SearchOption.AllDirectories);
|
||||
foreach (var file in files.Distinct())
|
||||
{
|
||||
Assembly asm = Assembly.LoadFrom(file);
|
||||
foreach (var t in asm.GetExportedTypes())
|
||||
{
|
||||
if (t.IsClass &&
|
||||
typeof(IImplementationExtension).IsAssignableFrom(t))
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
source = (IImplementationExtension)Activator.CreateInstance(t)!;
|
||||
}
|
||||
else
|
||||
{
|
||||
var newSource = (IImplementationExtension)Activator.CreateInstance(t)!;
|
||||
if (newSource.Priority > source.Priority)
|
||||
{
|
||||
source = newSource;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return source;
|
||||
}
|
||||
private static string TryGetImplementationExtensionsFolder()
|
||||
{
|
||||
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||
while (directory != null && !directory.GetDirectories("ImplementationExtensions",
|
||||
SearchOption.AllDirectories).Any(x => x.Name == "ImplementationExtensions"))
|
||||
{
|
||||
directory = directory.Parent;
|
||||
}
|
||||
return $"{directory?.FullName}\\ImplementationExtensions";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Unity;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace FurnitureAssemblyContracts.DI
|
||||
{
|
||||
public class UnityDependencyContainer : IDependencyContainer
|
||||
{
|
||||
private readonly IUnityContainer container;
|
||||
|
||||
public UnityDependencyContainer()
|
||||
{
|
||||
container = new UnityContainer();
|
||||
}
|
||||
|
||||
public void AddLogging(Action<ILoggingBuilder> configure)
|
||||
{
|
||||
// регистрируем логгер в контейнере
|
||||
var factory = LoggerFactory.Create(configure);
|
||||
container.AddExtension(new LoggingExtension(factory));
|
||||
}
|
||||
|
||||
public void RegisterType<T>(bool isSingle) where T : class
|
||||
{
|
||||
if (isSingle)
|
||||
{
|
||||
container.RegisterSingleton<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
container.RegisterType<T>();
|
||||
}
|
||||
}
|
||||
|
||||
public T Resolve<T>()
|
||||
{
|
||||
return container.Resolve<T>();
|
||||
}
|
||||
|
||||
void IDependencyContainer.RegisterType<T, U>(bool isSingle)
|
||||
{
|
||||
if (isSingle)
|
||||
{
|
||||
container.RegisterSingleton<T, U>();
|
||||
}
|
||||
else
|
||||
{
|
||||
container.RegisterType<T, U>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,8 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -0,0 +1,22 @@
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using FurnitureAssemblyDatabaseImplement.Implements;
|
||||
|
||||
namespace FurnitureAssemblyDatabaseImplement
|
||||
{
|
||||
public class DatabaseImplementationExtension : IImplementationExtension
|
||||
{
|
||||
public int Priority => 2;
|
||||
|
||||
public void RegisterServices()
|
||||
{
|
||||
DependencyManager.Instance.RegisterType<IClientStorage, ClientStorage>();
|
||||
DependencyManager.Instance.RegisterType<IComponentStorage, ComponentStorage>();
|
||||
DependencyManager.Instance.RegisterType<IImplementerStorage, ImplementerStorage>();
|
||||
DependencyManager.Instance.RegisterType<IMessageInfoStorage, MessageInfoStorage>();
|
||||
DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>();
|
||||
DependencyManager.Instance.RegisterType<IFurnitureStorage, FurnitureStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
@ -20,4 +20,8 @@
|
||||
<ProjectReference Include="..\FurnitureAssemblyDataModels\FurnitureAssemblyDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
<Exec Command="copy /Y "$(TargetDir)*.dll" "$(SolutionDir)ImplementationExtensions\*.dll"" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
@ -15,4 +15,8 @@
|
||||
<ProjectReference Include="..\FurnitureAssemblyDataModels\FurnitureAssemblyDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
|
||||
<Exec Command="copy /Y "$(TargetDir)*.dll" "$(SolutionDir)ImplementationExtensions\*.dll"" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
@ -0,0 +1,18 @@
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
|
||||
|
||||
namespace FurnitureAssemblyListImplement.Implements
|
||||
{
|
||||
public class BackUpInfo : IBackUpInfo
|
||||
{
|
||||
public List<T>? GetList<T>() where T : class, new()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Type? GetTypeByModelInterface(string modelInterfaceName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using FurnitureAssemblyContracts.DI;
|
||||
using FurnitureAssemblyContracts.StoragesContracts;
|
||||
using FurnitureAssemblyListImplement.Implements;
|
||||
|
||||
namespace FurnitureAssemblyListImplement
|
||||
{
|
||||
public class ListImplementationExtension : IImplementationExtension
|
||||
{
|
||||
public int Priority => 0;
|
||||
|
||||
public void RegisterServices()
|
||||
{
|
||||
DependencyManager.Instance.RegisterType<IClientStorage, ClientStorage>();
|
||||
DependencyManager.Instance.RegisterType<IComponentStorage, ComponentStorage>();
|
||||
DependencyManager.Instance.RegisterType<IImplementerStorage, ImplementerStorage>();
|
||||
DependencyManager.Instance.RegisterType<IMessageInfoStorage, MessageInfoStorage>();
|
||||
DependencyManager.Instance.RegisterType<IOrderStorage, OrderStorage>();
|
||||
DependencyManager.Instance.RegisterType<IFurnitureStorage, FurnitureStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user