diff --git a/DepartmentPortal/Common/DesktopTools/Controls/AbstractGenerticControlEntityList.cs b/DepartmentPortal/Common/DesktopTools/Controls/AbstractGenerticControlEntityList.cs index d09cb1b..6f3b904 100644 --- a/DepartmentPortal/Common/DesktopTools/Controls/AbstractGenerticControlEntityList.cs +++ b/DepartmentPortal/Common/DesktopTools/Controls/AbstractGenerticControlEntityList.cs @@ -30,6 +30,7 @@ namespace DesktopTools.Controls { InitializeComponent(); InitEvents(); + _businessLogic = DependencyManager.Instance.Resolve(); } public override void Open() diff --git a/DepartmentPortal/Common/ModuleTools/BusinessLogics/BusinessLogicCore.cs b/DepartmentPortal/Common/ModuleTools/BusinessLogics/BusinessLogicCore.cs index d8769e9..8e09628 100644 --- a/DepartmentPortal/Common/ModuleTools/BusinessLogics/BusinessLogicCore.cs +++ b/DepartmentPortal/Common/ModuleTools/BusinessLogics/BusinessLogicCore.cs @@ -38,7 +38,7 @@ namespace ModuleTools.BusinessLogics { Service = service; Errors = new List<(string Title, string Message)>(); - Security = UnityContainerConfigurator.Resolve(); + Security = DependencyManager.Instance.Resolve(); _entity = entity; _serviceOperation = serviceOperation; } diff --git a/DepartmentPortal/Common/ModuleTools/BusinessLogics/DependencyManager.cs b/DepartmentPortal/Common/ModuleTools/BusinessLogics/DependencyManager.cs new file mode 100644 index 0000000..3fa1776 --- /dev/null +++ b/DepartmentPortal/Common/ModuleTools/BusinessLogics/DependencyManager.cs @@ -0,0 +1,62 @@ +using ModuleTools.Interfaces; +using System; + +namespace ModuleTools.BusinessLogics +{ + /// + /// Менеджер для работы с зависимостями + /// + public class DependencyManager + { + private readonly IDependencyManager _dependencyManager; + + private static DependencyManager _manager; + + private static readonly object _locjObject = new object(); + + private DependencyManager() + { + _dependencyManager = new UnityContainerManager(); + } + + public static DependencyManager Instance { get { if (_manager == null) { lock (_locjObject) { _manager = new DependencyManager(); } } return _manager; } } + + /// + /// Иницализация библиотек, в которых идут установки зависомстей + /// + public static void InitDependency() + { + var ext = ServiceProviderLoader.GetImplementationExtensions(); + if (ext.Count == 0) + { + throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям"); + } + // регистрируем зависимости + foreach (var e in ext) + { + e.RegisterServices(); + } + } + + /// + /// Добавление зависимости + /// + /// + /// + public void RegisterType() where U : T => _dependencyManager.RegisterType(); + + + /// + /// Добавление зависимости + /// + /// + public void RegisterType() => _dependencyManager.RegisterType(); + + /// + /// Получение класса со всеми зависмостями + /// + /// + /// + public T Resolve() => _dependencyManager.Resolve(); + } +} \ No newline at end of file diff --git a/DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerConfigurator.cs b/DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerConfigurator.cs deleted file mode 100644 index 0a3c7eb..0000000 --- a/DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerConfigurator.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using Unity; -using Unity.Lifetime; - -namespace ModuleTools.BusinessLogics -{ - /// - /// Работа с UnityContainer - /// - public class UnityContainerConfigurator - { - private static IUnityContainer _unityContainer; - - public static IUnityContainer Container - { - get - { - if (_unityContainer == null) _unityContainer = new UnityContainer(); - return _unityContainer; - } - } - - /// - /// Инициализация сервисов - /// - public static void InitServices() - { - var ext = ServiceProviderLoader.GetImplementationExtensions(); - if (ext.Count == 0) - { - throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям"); - } - // регистрируем в UnityContainaer зависимости - foreach (var e in ext) - { - e.RegisterServices(); - } - } - - /// - /// Добавление зависимости - /// - /// - /// - public static void PublishService() where U : T => Container.RegisterType(new HierarchicalLifetimeManager()); - - /// - /// Получение класса со всеми зависмостями - /// - /// - /// - public static T Resolve() => Container.Resolve(); - } -} \ No newline at end of file diff --git a/DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerManager.cs b/DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerManager.cs new file mode 100644 index 0000000..faadb9f --- /dev/null +++ b/DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerManager.cs @@ -0,0 +1,22 @@ +using ModuleTools.Interfaces; +using Unity; +using Unity.Lifetime; + +namespace ModuleTools.BusinessLogics +{ + /// + /// Работа с UnityContainer + /// + public class UnityContainerManager : IDependencyManager + { + private readonly IUnityContainer _unityContainer; + + public UnityContainerManager() => _unityContainer = new UnityContainer(); + + public void RegisterType() where U : T => _unityContainer.RegisterType(new HierarchicalLifetimeManager()); + + public void RegisterType() => _unityContainer.RegisterType(new HierarchicalLifetimeManager()); + + public T Resolve() => _unityContainer.Resolve(); + } +} \ No newline at end of file diff --git a/DepartmentPortal/Common/ModuleTools/Interfaces/IDependencyManager.cs b/DepartmentPortal/Common/ModuleTools/Interfaces/IDependencyManager.cs new file mode 100644 index 0000000..f4f09b4 --- /dev/null +++ b/DepartmentPortal/Common/ModuleTools/Interfaces/IDependencyManager.cs @@ -0,0 +1,29 @@ +namespace ModuleTools.Interfaces +{ + /// + /// Интерфейс установки зависмости между элементами + /// + public interface IDependencyManager + { + /// + /// Добавление зависимости + /// + /// + /// + void RegisterType() where U : T; + + + /// + /// Добавление зависимости + /// + /// + void RegisterType(); + + /// + /// Получение класса со всеми зависмостями + /// + /// + /// + T Resolve(); + } +} \ No newline at end of file diff --git a/DepartmentPortal/DepartmentPortalDesctop/FormEnter.cs b/DepartmentPortal/DepartmentPortalDesctop/FormEnter.cs index d2caaae..733ba9e 100644 --- a/DepartmentPortal/DepartmentPortalDesctop/FormEnter.cs +++ b/DepartmentPortal/DepartmentPortalDesctop/FormEnter.cs @@ -31,7 +31,7 @@ namespace DepartmentPortalDesctop } try { - var securityManager = UnityContainerConfigurator.Resolve(); + var securityManager = DependencyManager.Instance.Resolve(); Task.WaitAll(Task.Run(async () => await securityManager.LoginAsync(textBoxLogin.Text, textBoxPassword.Text))); DialogResult = DialogResult.OK; Close(); diff --git a/DepartmentPortal/DepartmentPortalDesctop/Program.cs b/DepartmentPortal/DepartmentPortalDesctop/Program.cs index 49a5aa9..86e377a 100644 --- a/DepartmentPortal/DepartmentPortalDesctop/Program.cs +++ b/DepartmentPortal/DepartmentPortalDesctop/Program.cs @@ -14,10 +14,10 @@ namespace DepartmentPortalDesctop [STAThread] static void Main() { - UnityContainerConfigurator.PublishService(); - UnityContainerConfigurator.InitServices(); + DependencyManager.Instance.RegisterType(); + DependencyManager.InitDependency(); - var securityManager = UnityContainerConfigurator.Resolve(); + var securityManager = DependencyManager.Instance.Resolve(); securityManager.CheckStartDataSource(); Application.SetHighDpiMode(HighDpiMode.SystemAware); @@ -28,7 +28,7 @@ namespace DepartmentPortalDesctop if (form.ShowDialog() == DialogResult.OK && securityManager.IsAuth) { - Application.Run(UnityContainerConfigurator.Resolve()); + Application.Run(DependencyManager.Instance.Resolve()); } } } diff --git a/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs b/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs index b3f53c9..6ea45f7 100644 --- a/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs +++ b/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs @@ -9,10 +9,10 @@ namespace SecurityDatabaseImplementation { public void RegisterServices() { - UnityContainerConfigurator.PublishService(); - UnityContainerConfigurator.PublishService(); - UnityContainerConfigurator.PublishService(); - UnityContainerConfigurator.PublishService(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); } } } \ No newline at end of file diff --git a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/AccessesControl.cs b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/AccessesControl.cs index 37d8a0d..26ebeb7 100644 --- a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/AccessesControl.cs +++ b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/AccessesControl.cs @@ -18,8 +18,7 @@ namespace SecurityWindowsDesktop.Controls public AccessesControl() { InitializeComponent(); - _businessLogic = UnityContainerConfigurator.Resolve(); - _roleBusinessLogic = UnityContainerConfigurator.Resolve(); + _roleBusinessLogic = DependencyManager.Instance.Resolve(); Title = "Доступы"; Id = new Guid("6eebc4c4-cb86-4368-93e0-33dbdbb7409a"); Order = 1; diff --git a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/EnviromentSettingControl.cs b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/EnviromentSettingControl.cs index 7429669..d491103 100644 --- a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/EnviromentSettingControl.cs +++ b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/EnviromentSettingControl.cs @@ -14,7 +14,6 @@ namespace SecurityWindowsDesktop.Controls public EnviromentSettingControl() { InitializeComponent(); - _businessLogic = UnityContainerConfigurator.Resolve(); Title = "Настройки Среды"; Id = new Guid("b3865c23-b1db-475b-b95c-aa51edc60388"); Order = 1; diff --git a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/RolesControl.cs b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/RolesControl.cs index 97f8569..67d1cde 100644 --- a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/RolesControl.cs +++ b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/RolesControl.cs @@ -14,7 +14,6 @@ namespace SecurityWindowsDesktop.Controls public RolesControl() : base() { InitializeComponent(); - _businessLogic = UnityContainerConfigurator.Resolve(); Title = "Роли"; Id = new Guid("6a33ce5c-e950-4294-9f75-2a0b35941bf7"); Order = 1; diff --git a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/UsersControl.cs b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/UsersControl.cs index ac2f7af..b97f6da 100644 --- a/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/UsersControl.cs +++ b/DepartmentPortal/Security/SecurityWindowsDesktop/Controls/UsersControl.cs @@ -14,7 +14,6 @@ namespace SecurityWindowsDesktop.Controls public UsersControl() : base() { InitializeComponent(); - _businessLogic = UnityContainerConfigurator.Resolve(); Title = "Пользователи"; Id = new Guid("d5596997-d1f5-4e5e-b94b-6bdd6bca3452"); Order = 1; diff --git a/DepartmentPortal/Security/SecurityWindowsDesktop/SecurityWindowDesktopExtension.cs b/DepartmentPortal/Security/SecurityWindowsDesktop/SecurityWindowDesktopExtension.cs index 5830c7d..fca70e1 100644 --- a/DepartmentPortal/Security/SecurityWindowsDesktop/SecurityWindowDesktopExtension.cs +++ b/DepartmentPortal/Security/SecurityWindowsDesktop/SecurityWindowDesktopExtension.cs @@ -1,10 +1,11 @@ using DesktopTools.Controls; +using DesktopTools.Interfaces; +using DesktopTools.Models; using ModuleTools.BindingModels; using ModuleTools.BusinessLogics; using ModuleTools.Enums; using ModuleTools.Interfaces; using ModuleTools.Models; -using SecurityBusinessLogic.BusinessLogics; using SecurityWindowsDesktop.Controls; using System.Collections.Generic; @@ -14,7 +15,7 @@ namespace SecurityWindowsDesktop { public List GetListControlEntityList() { - var manager = UnityContainerConfigurator.Resolve(); + var manager = DependencyManager.Instance.Resolve(); if (manager == null) { return null;