From 65b2c39f0b10b860b651240f8978d9e295ad60cc Mon Sep 17 00:00:00 2001 From: kotcheshir73 Date: Sun, 28 Mar 2021 19:48:15 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20=D1=81?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BD=D0=B0=20=D1=83=D0=BD=D0=B8=D0=B2=D0=B5?= =?UTF-8?q?=D1=80=D1=81=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractGenerticControlEntityList.cs | 1 + .../BusinessLogics/BusinessLogicCore.cs | 2 +- .../BusinessLogics/DependencyManager.cs | 62 +++++++++++++++++++ .../UnityContainerConfigurator.cs | 54 ---------------- .../BusinessLogics/UnityContainerManager.cs | 22 +++++++ .../Interfaces/IDependencyManager.cs | 29 +++++++++ .../DepartmentPortalDesctop/FormEnter.cs | 2 +- .../DepartmentPortalDesctop/Program.cs | 8 +-- .../SecurityImplementationExtensions.cs | 8 +-- .../Controls/AccessesControl.cs | 3 +- .../Controls/EnviromentSettingControl.cs | 1 - .../Controls/RolesControl.cs | 1 - .../Controls/UsersControl.cs | 1 - .../SecurityWindowDesktopExtension.cs | 5 +- 14 files changed, 128 insertions(+), 71 deletions(-) create mode 100644 DepartmentPortal/Common/ModuleTools/BusinessLogics/DependencyManager.cs delete mode 100644 DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerConfigurator.cs create mode 100644 DepartmentPortal/Common/ModuleTools/BusinessLogics/UnityContainerManager.cs create mode 100644 DepartmentPortal/Common/ModuleTools/Interfaces/IDependencyManager.cs 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;