diff --git a/AutoWorkshopContracts/DI/DependencyManager.cs b/AutoWorkshopContracts/DI/DependencyManager.cs index 69e702a..be8dc06 100644 --- a/AutoWorkshopContracts/DI/DependencyManager.cs +++ b/AutoWorkshopContracts/DI/DependencyManager.cs @@ -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 Configure) => _dependencyManager.AddLogging(Configure); + public void AddLogging(Action Configure) => _dependencyContainer.AddLogging(Configure); - public void RegisterType(bool IsSingle = false) where U : class, T where T : class => _dependencyManager.RegisterType(IsSingle); + public void RegisterType(bool IsSingleton = false) where U : class, T where T : class => _dependencyContainer.RegisterType(IsSingleton); - public void RegisterType(bool IsSingle = false) where T : class => _dependencyManager.RegisterType(IsSingle); + public void RegisterType(bool IsSingleton = false) where T : class => _dependencyContainer.RegisterType(IsSingleton); - public T Resolve() => _dependencyManager.Resolve(); + public T Resolve() => _dependencyContainer.Resolve(); } } diff --git a/AutoWorkshopContracts/DI/ServiceProviderLoader.cs b/AutoWorkshopContracts/DI/ServiceProviderLoader.cs index d0738f9..46f8a0b 100644 --- a/AutoWorkshopContracts/DI/ServiceProviderLoader.cs +++ b/AutoWorkshopContracts/DI/ServiceProviderLoader.cs @@ -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"; } } } diff --git a/AutoWorkshopDatabaseImplement/ImplementationExtension.cs b/AutoWorkshopDatabaseImplement/ImplementationExtension.cs new file mode 100644 index 0000000..5edf0f4 --- /dev/null +++ b/AutoWorkshopDatabaseImplement/ImplementationExtension.cs @@ -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(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} diff --git a/AutoWorkshopDatabaseImplement/Implements/ImplementationExtension.cs b/AutoWorkshopDatabaseImplement/Implements/ImplementationExtension.cs deleted file mode 100644 index affba10..0000000 --- a/AutoWorkshopDatabaseImplement/Implements/ImplementationExtension.cs +++ /dev/null @@ -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(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - } - } -} diff --git a/AutoWorkshopFileImplement/ImplementationExtension.cs b/AutoWorkshopFileImplement/ImplementationExtension.cs new file mode 100644 index 0000000..43b1d31 --- /dev/null +++ b/AutoWorkshopFileImplement/ImplementationExtension.cs @@ -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(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} diff --git a/AutoWorkshopFileImplement/Implements/ImplementationExtension.cs b/AutoWorkshopFileImplement/Implements/ImplementationExtension.cs deleted file mode 100644 index e682d43..0000000 --- a/AutoWorkshopFileImplement/Implements/ImplementationExtension.cs +++ /dev/null @@ -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(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - } - } -} diff --git a/AutoWorkshopImplement/ImplementationExtension.cs b/AutoWorkshopImplement/ImplementationExtension.cs new file mode 100644 index 0000000..13f549b --- /dev/null +++ b/AutoWorkshopImplement/ImplementationExtension.cs @@ -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(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } +} diff --git a/AutoWorkshopImplement/Implements/ImplementationExtension.cs b/AutoWorkshopImplement/Implements/ImplementationExtension.cs deleted file mode 100644 index b18e115..0000000 --- a/AutoWorkshopImplement/Implements/ImplementationExtension.cs +++ /dev/null @@ -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(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); - } - } -} diff --git a/AutoWorkshopView/Program.cs b/AutoWorkshopView/Program.cs index e413985..14906fa 100644 --- a/AutoWorkshopView/Program.cs +++ b/AutoWorkshopView/Program.cs @@ -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(); + try + { + var MailSender = DependencyManager.Instance.Resolve(); 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(); + var Logger = DependencyManager.Instance.Resolve(); Logger?.LogError(ex, " "); } - Application.Run(_serviceProvider.GetRequiredService()); - } + Application.Run(DependencyManager.Instance.Resolve()); + } - private static void MailCheck(object obj) => ServiceProvider?.GetService()?.CheckMailAsync(); + private static void MailCheck(object obj) => DependencyManager.Instance.Resolve()?.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(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(true); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddSingleton(); - - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - Services.AddTransient(); - } - } + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + } + } }