change ServiceProvider to DependencyManager in Program.cs
This commit is contained in:
parent
1adb6bb874
commit
20cf8869fa
@ -4,22 +4,31 @@ namespace AutoWorkshopContracts.DI
|
||||
{
|
||||
public class DependencyManager
|
||||
{
|
||||
private readonly IDependencyContainer _dependencyManager;
|
||||
|
||||
private readonly IDependencyContainer _dependencyContainer;
|
||||
private static DependencyManager? _manager;
|
||||
|
||||
private static readonly object _locjObject = new();
|
||||
private static readonly object _lock = new();
|
||||
|
||||
private DependencyManager()
|
||||
{
|
||||
_dependencyManager = new ServiceDependencyContainer();
|
||||
_dependencyContainer = new ServiceDependencyContainer();
|
||||
}
|
||||
|
||||
public static DependencyManager Instance { get { if (_manager == null) { lock (_locjObject) { _manager = new DependencyManager(); } } return _manager; } }
|
||||
public static DependencyManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_manager == null)
|
||||
{
|
||||
lock (_lock) { _manager = new DependencyManager(); }
|
||||
}
|
||||
return _manager;
|
||||
}
|
||||
}
|
||||
|
||||
public static void InitDependency()
|
||||
{
|
||||
var Ext = ServiceProviderLoader.GetImplementationExtensions();
|
||||
var Ext = ServiceProviderLoader.GetImplementationExtension();
|
||||
if (Ext == null)
|
||||
{
|
||||
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
|
||||
@ -28,12 +37,12 @@ namespace AutoWorkshopContracts.DI
|
||||
Ext.RegisterServices();
|
||||
}
|
||||
|
||||
public void AddLogging(Action<ILoggingBuilder> Configure) => _dependencyManager.AddLogging(Configure);
|
||||
public void AddLogging(Action<ILoggingBuilder> Configure) => _dependencyContainer.AddLogging(Configure);
|
||||
|
||||
public void RegisterType<T, U>(bool IsSingle = false) where U : class, T where T : class => _dependencyManager.RegisterType<T, U>(IsSingle);
|
||||
public void RegisterType<T, U>(bool IsSingleton = false) where U : class, T where T : class => _dependencyContainer.RegisterType<T, U>(IsSingleton);
|
||||
|
||||
public void RegisterType<T>(bool IsSingle = false) where T : class => _dependencyManager.RegisterType<T>(IsSingle);
|
||||
public void RegisterType<T>(bool IsSingleton = false) where T : class => _dependencyContainer.RegisterType<T>(IsSingleton);
|
||||
|
||||
public T Resolve<T>() => _dependencyManager.Resolve<T>();
|
||||
public T Resolve<T>() => _dependencyContainer.Resolve<T>();
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,10 @@ namespace AutoWorkshopContracts.DI
|
||||
{
|
||||
public static class ServiceProviderLoader
|
||||
{
|
||||
public static IImplementationExtension? GetImplementationExtensions()
|
||||
public static IImplementationExtension? GetImplementationExtension()
|
||||
{
|
||||
IImplementationExtension? Source = null;
|
||||
|
||||
var Files = Directory.GetFiles(TryGetImplementationExtensionsFolder(), "*.dll", SearchOption.AllDirectories);
|
||||
foreach (var File in Files.Distinct())
|
||||
{
|
||||
@ -31,18 +32,19 @@ namespace AutoWorkshopContracts.DI
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Source;
|
||||
}
|
||||
|
||||
private static string TryGetImplementationExtensionsFolder()
|
||||
{
|
||||
var Directory = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory());
|
||||
while (Directory != null && !Directory.GetDirectories("ImplementationExtensions", SearchOption.AllDirectories).Any(x => x.Name == "ImplementationExtensions"))
|
||||
var WorkingDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||
while (WorkingDirectory != null && !WorkingDirectory.GetDirectories("ImplementationExtensions", SearchOption.AllDirectories).Any(x => x.Name == "ImplementationExtensions"))
|
||||
{
|
||||
Directory = Directory.Parent;
|
||||
WorkingDirectory = WorkingDirectory.Parent;
|
||||
}
|
||||
|
||||
return $"{Directory?.FullName}\\ImplementationExtensions";
|
||||
return $"{WorkingDirectory?.FullName}\\ImplementationExtensions";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
22
AutoWorkshopDatabaseImplement/ImplementationExtension.cs
Normal file
22
AutoWorkshopDatabaseImplement/ImplementationExtension.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using AutoWorkshopContracts.DI;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
using AutoWorkshopDatabaseImplement.Implements;
|
||||
|
||||
namespace AutoWorkshopDatabaseImplement
|
||||
{
|
||||
public class ImplementationExtension : 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<IRepairStorage, RepairStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
using AutoWorkshopContracts.DI;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
|
||||
namespace AutoWorkshopDatabaseImplement.Implements
|
||||
{
|
||||
public class ImplementationExtension : 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<IRepairStorage, RepairStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
22
AutoWorkshopFileImplement/ImplementationExtension.cs
Normal file
22
AutoWorkshopFileImplement/ImplementationExtension.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using AutoWorkshopContracts.DI;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
using AutoWorkshopFileImplement.Implements;
|
||||
|
||||
namespace AutoWorkshopFileImplement
|
||||
{
|
||||
public class ImplementationExtension : 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<IRepairStorage, RepairStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
using AutoWorkshopContracts.DI;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
|
||||
namespace AutoWorkshopFileImplement.Implements
|
||||
{
|
||||
public class ImplementationExtension : 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<IRepairStorage, RepairStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
22
AutoWorkshopImplement/ImplementationExtension.cs
Normal file
22
AutoWorkshopImplement/ImplementationExtension.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using AutoWorkshopContracts.DI;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
using AutoWorkshopListImplement.Implements;
|
||||
|
||||
namespace AutoWorkshopListImplement
|
||||
{
|
||||
public class ImplementationExtension : 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<IRepairStorage, RepairStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
using AutoWorkshopContracts.DI;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
|
||||
namespace AutoWorkshopListImplement.Implements
|
||||
{
|
||||
public class ImplementationExtension : 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<IRepairStorage, RepairStorage>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpInfo, BackUpInfo>();
|
||||
}
|
||||
}
|
||||
}
|
@ -2,101 +2,87 @@ using AutoWorkshopBusinessLogic.BusinessLogics;
|
||||
using AutoWorkshopBusinessLogic.OfficePackage.Implements;
|
||||
using AutoWorkshopBusinessLogic.OfficePackage;
|
||||
using AutoWorkshopContracts.BusinessLogicContracts;
|
||||
using AutoWorkshopContracts.StoragesContracts;
|
||||
using AutoWorkshopDatabaseImplement.Implements;
|
||||
using AutoWorkshopView.Forms;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
using AutoWorkshopContracts.BusinessLogicsContracts;
|
||||
using AutoWorkshopBusinessLogic.MailWorker;
|
||||
using AutoWorkshopContracts.BindingModels;
|
||||
using AutoWorkshopContracts.DI;
|
||||
|
||||
namespace AutoWorkshopView
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
private static ServiceProvider? _serviceProvider;
|
||||
public static ServiceProvider? ServiceProvider => _serviceProvider;
|
||||
internal static class Program
|
||||
{
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
ApplicationConfiguration.Initialize();
|
||||
InitDependency();
|
||||
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
ApplicationConfiguration.Initialize();
|
||||
|
||||
var Services = new ServiceCollection();
|
||||
ConfigureServices(Services);
|
||||
|
||||
_serviceProvider = Services.BuildServiceProvider();
|
||||
|
||||
try
|
||||
{
|
||||
var MailSender = _serviceProvider.GetService<AbstractMailWorker>();
|
||||
try
|
||||
{
|
||||
var MailSender = DependencyManager.Instance.Resolve<AbstractMailWorker>();
|
||||
MailSender?.MailConfig(new MailConfigBindingModel
|
||||
{
|
||||
MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty,
|
||||
MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty,
|
||||
SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty,
|
||||
SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]),
|
||||
PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty,
|
||||
PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"])
|
||||
MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty,
|
||||
MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty,
|
||||
SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty,
|
||||
SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClientPort"]),
|
||||
PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty,
|
||||
PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"])
|
||||
});
|
||||
|
||||
var Timer = new System.Threading.Timer(new TimerCallback(MailCheck!), null, 0, 100000);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var Logger = _serviceProvider.GetService<ILogger>();
|
||||
var Logger = DependencyManager.Instance.Resolve<ILogger>();
|
||||
Logger?.LogError(ex, "Îøèáêà ðàáîòû ñ ïî÷òîé");
|
||||
}
|
||||
|
||||
Application.Run(_serviceProvider.GetRequiredService<MainForm>());
|
||||
}
|
||||
Application.Run(DependencyManager.Instance.Resolve<MainForm>());
|
||||
}
|
||||
|
||||
private static void MailCheck(object obj) => ServiceProvider?.GetService<AbstractMailWorker>()?.CheckMailAsync();
|
||||
private static void MailCheck(object obj) => DependencyManager.Instance.Resolve<AbstractMailWorker>()?.CheckMailAsync();
|
||||
|
||||
private static void ConfigureServices(ServiceCollection Services)
|
||||
{
|
||||
Services.AddLogging(option =>
|
||||
{
|
||||
option.SetMinimumLevel(LogLevel.Information);
|
||||
option.AddNLog("nlog.config");
|
||||
});
|
||||
private static void InitDependency()
|
||||
{
|
||||
DependencyManager.InitDependency();
|
||||
DependencyManager.Instance.AddLogging(Option =>
|
||||
{
|
||||
Option.SetMinimumLevel(LogLevel.Information);
|
||||
Option.AddNLog("nlog.config");
|
||||
});
|
||||
|
||||
Services.AddTransient<IComponentStorage, ComponentStorage>();
|
||||
Services.AddTransient<IOrderStorage, OrderStorage>();
|
||||
Services.AddTransient<IRepairStorage, RepairStorage>();
|
||||
Services.AddTransient<IClientStorage, ClientStorage>();
|
||||
Services.AddTransient<IImplementerStorage, ImplementerStorage>();
|
||||
Services.AddTransient<IMessageInfoStorage, MessageInfoStorage>();
|
||||
DependencyManager.Instance.RegisterType<IComponentLogic, ComponentLogic>();
|
||||
DependencyManager.Instance.RegisterType<IOrderLogic, OrderLogic>();
|
||||
DependencyManager.Instance.RegisterType<IRepairLogic, RepairLogic>();
|
||||
DependencyManager.Instance.RegisterType<IReportLogic, ReportLogic>();
|
||||
DependencyManager.Instance.RegisterType<IClientLogic, ClientLogic>();
|
||||
DependencyManager.Instance.RegisterType<IImplementerLogic, ImplementerLogic>();
|
||||
DependencyManager.Instance.RegisterType<IWorkProcess, WorkModeling>();
|
||||
DependencyManager.Instance.RegisterType<IMessageInfoLogic, MessageInfoLogic>();
|
||||
DependencyManager.Instance.RegisterType<IBackUpLogic, BackUpLogic>();
|
||||
|
||||
Services.AddTransient<IComponentLogic, ComponentLogic>();
|
||||
Services.AddTransient<IOrderLogic, OrderLogic>();
|
||||
Services.AddTransient<IRepairLogic, RepairLogic>();
|
||||
Services.AddTransient<IReportLogic, ReportLogic>();
|
||||
Services.AddTransient<IClientLogic, ClientLogic>();
|
||||
Services.AddTransient<IImplementerLogic, ImplementerLogic>();
|
||||
Services.AddTransient<IWorkProcess, WorkModeling>();
|
||||
Services.AddTransient<IMessageInfoLogic, MessageInfoLogic>();
|
||||
DependencyManager.Instance.RegisterType<AbstractSaveToWord, SaveToWord>();
|
||||
DependencyManager.Instance.RegisterType<AbstractSaveToExcel, SaveToExcel>();
|
||||
DependencyManager.Instance.RegisterType<AbstractSaveToPdf, SaveToPdf>();
|
||||
DependencyManager.Instance.RegisterType<AbstractMailWorker, MailKitWorker>(true);
|
||||
|
||||
Services.AddTransient<AbstractSaveToWord, SaveToWord>();
|
||||
Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
|
||||
Services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
|
||||
Services.AddSingleton<AbstractMailWorker, MailKitWorker>();
|
||||
|
||||
Services.AddTransient<MainForm>();
|
||||
Services.AddTransient<FormComponent>();
|
||||
Services.AddTransient<FormComponents>();
|
||||
Services.AddTransient<FormCreateOrder>();
|
||||
Services.AddTransient<FormRepair>();
|
||||
Services.AddTransient<FormRepairComponent>();
|
||||
Services.AddTransient<FormRepairs>();
|
||||
Services.AddTransient<FormReportRepairComponents>();
|
||||
Services.AddTransient<FormReportOrders>();
|
||||
Services.AddTransient<FormClients>();
|
||||
Services.AddTransient<FormImplementers>();
|
||||
Services.AddTransient<FormImplementer>();
|
||||
Services.AddTransient<FormMail>();
|
||||
}
|
||||
}
|
||||
DependencyManager.Instance.RegisterType<MainForm>();
|
||||
DependencyManager.Instance.RegisterType<FormComponent>();
|
||||
DependencyManager.Instance.RegisterType<FormComponents>();
|
||||
DependencyManager.Instance.RegisterType<FormCreateOrder>();
|
||||
DependencyManager.Instance.RegisterType<FormRepair>();
|
||||
DependencyManager.Instance.RegisterType<FormRepairComponent>();
|
||||
DependencyManager.Instance.RegisterType<FormRepairs>();
|
||||
DependencyManager.Instance.RegisterType<FormReportRepairComponents>();
|
||||
DependencyManager.Instance.RegisterType<FormReportOrders>();
|
||||
DependencyManager.Instance.RegisterType<FormClients>();
|
||||
DependencyManager.Instance.RegisterType<FormImplementers>();
|
||||
DependencyManager.Instance.RegisterType<FormImplementer>();
|
||||
DependencyManager.Instance.RegisterType<FormMail>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user