работа с зависимостми переделана на универсальность

This commit is contained in:
kotcheshir73 2021-03-28 19:48:15 +04:00
parent f63cbb816d
commit 65b2c39f0b
14 changed files with 128 additions and 71 deletions

View File

@ -30,6 +30,7 @@ namespace DesktopTools.Controls
{
InitializeComponent();
InitEvents();
_businessLogic = DependencyManager.Instance.Resolve<BL>();
}
public override void Open()

View File

@ -38,7 +38,7 @@ namespace ModuleTools.BusinessLogics
{
Service = service;
Errors = new List<(string Title, string Message)>();
Security = UnityContainerConfigurator.Resolve<ISecurityManager>();
Security = DependencyManager.Instance.Resolve<ISecurityManager>();
_entity = entity;
_serviceOperation = serviceOperation;
}

View File

@ -0,0 +1,62 @@
using ModuleTools.Interfaces;
using System;
namespace ModuleTools.BusinessLogics
{
/// <summary>
/// Менеджер для работы с зависимостями
/// </summary>
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; } }
/// <summary>
/// Иницализация библиотек, в которых идут установки зависомстей
/// </summary>
public static void InitDependency()
{
var ext = ServiceProviderLoader.GetImplementationExtensions();
if (ext.Count == 0)
{
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
}
// регистрируем зависимости
foreach (var e in ext)
{
e.RegisterServices();
}
}
/// <summary>
/// Добавление зависимости
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
public void RegisterType<T, U>() where U : T => _dependencyManager.RegisterType<T, U>();
/// <summary>
/// Добавление зависимости
/// </summary>
/// <typeparam name="T"></typeparam>
public void RegisterType<T>() => _dependencyManager.RegisterType<T>();
/// <summary>
/// Получение класса со всеми зависмостями
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>() => _dependencyManager.Resolve<T>();
}
}

View File

@ -1,54 +0,0 @@
using System;
using Unity;
using Unity.Lifetime;
namespace ModuleTools.BusinessLogics
{
/// <summary>
/// Работа с UnityContainer
/// </summary>
public class UnityContainerConfigurator
{
private static IUnityContainer _unityContainer;
public static IUnityContainer Container
{
get
{
if (_unityContainer == null) _unityContainer = new UnityContainer();
return _unityContainer;
}
}
/// <summary>
/// Инициализация сервисов
/// </summary>
public static void InitServices()
{
var ext = ServiceProviderLoader.GetImplementationExtensions();
if (ext.Count == 0)
{
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
}
// регистрируем в UnityContainaer зависимости
foreach (var e in ext)
{
e.RegisterServices();
}
}
/// <summary>
/// Добавление зависимости
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
public static void PublishService<T, U>() where U : T => Container.RegisterType<T, U>(new HierarchicalLifetimeManager());
/// <summary>
/// Получение класса со всеми зависмостями
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Resolve<T>() => Container.Resolve<T>();
}
}

View File

@ -0,0 +1,22 @@
using ModuleTools.Interfaces;
using Unity;
using Unity.Lifetime;
namespace ModuleTools.BusinessLogics
{
/// <summary>
/// Работа с UnityContainer
/// </summary>
public class UnityContainerManager : IDependencyManager
{
private readonly IUnityContainer _unityContainer;
public UnityContainerManager() => _unityContainer = new UnityContainer();
public void RegisterType<T, U>() where U : T => _unityContainer.RegisterType<T, U>(new HierarchicalLifetimeManager());
public void RegisterType<T>() => _unityContainer.RegisterType<T>(new HierarchicalLifetimeManager());
public T Resolve<T>() => _unityContainer.Resolve<T>();
}
}

View File

@ -0,0 +1,29 @@
namespace ModuleTools.Interfaces
{
/// <summary>
/// Интерфейс установки зависмости между элементами
/// </summary>
public interface IDependencyManager
{
/// <summary>
/// Добавление зависимости
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="U"></typeparam>
void RegisterType<T, U>() where U : T;
/// <summary>
/// Добавление зависимости
/// </summary>
/// <typeparam name="T"></typeparam>
void RegisterType<T>();
/// <summary>
/// Получение класса со всеми зависмостями
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T Resolve<T>();
}
}

View File

@ -31,7 +31,7 @@ namespace DepartmentPortalDesctop
}
try
{
var securityManager = UnityContainerConfigurator.Resolve<ISecurityManager>();
var securityManager = DependencyManager.Instance.Resolve<ISecurityManager>();
Task.WaitAll(Task.Run(async () => await securityManager.LoginAsync(textBoxLogin.Text, textBoxPassword.Text)));
DialogResult = DialogResult.OK;
Close();

View File

@ -14,10 +14,10 @@ namespace DepartmentPortalDesctop
[STAThread]
static void Main()
{
UnityContainerConfigurator.PublishService<ISecurityManager, SecurityManager>();
UnityContainerConfigurator.InitServices();
DependencyManager.Instance.RegisterType<ISecurityManager, SecurityManager>();
DependencyManager.InitDependency();
var securityManager = UnityContainerConfigurator.Resolve<ISecurityManager>();
var securityManager = DependencyManager.Instance.Resolve<ISecurityManager>();
securityManager.CheckStartDataSource();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
@ -28,7 +28,7 @@ namespace DepartmentPortalDesctop
if (form.ShowDialog() == DialogResult.OK && securityManager.IsAuth)
{
Application.Run(UnityContainerConfigurator.Resolve<FormMain>());
Application.Run(DependencyManager.Instance.Resolve<FormMain>());
}
}
}

View File

@ -9,10 +9,10 @@ namespace SecurityDatabaseImplementation
{
public void RegisterServices()
{
UnityContainerConfigurator.PublishService<IAccessService, AccessService>();
UnityContainerConfigurator.PublishService<IEnviromentSettingService, EnviromentSettingService>();
UnityContainerConfigurator.PublishService<IRoleService, RoleService>();
UnityContainerConfigurator.PublishService<IUserService, UserService>();
DependencyManager.Instance.RegisterType<IAccessService, AccessService>();
DependencyManager.Instance.RegisterType<IEnviromentSettingService, EnviromentSettingService>();
DependencyManager.Instance.RegisterType<IRoleService, RoleService>();
DependencyManager.Instance.RegisterType<IUserService, UserService>();
}
}
}

View File

@ -18,8 +18,7 @@ namespace SecurityWindowsDesktop.Controls
public AccessesControl()
{
InitializeComponent();
_businessLogic = UnityContainerConfigurator.Resolve<AccessBusinessLogic>();
_roleBusinessLogic = UnityContainerConfigurator.Resolve<RoleBusinessLogic>();
_roleBusinessLogic = DependencyManager.Instance.Resolve<RoleBusinessLogic>();
Title = "Доступы";
Id = new Guid("6eebc4c4-cb86-4368-93e0-33dbdbb7409a");
Order = 1;

View File

@ -14,7 +14,6 @@ namespace SecurityWindowsDesktop.Controls
public EnviromentSettingControl()
{
InitializeComponent();
_businessLogic = UnityContainerConfigurator.Resolve<EnviromentSettingBusinessLogic>();
Title = "Настройки Среды";
Id = new Guid("b3865c23-b1db-475b-b95c-aa51edc60388");
Order = 1;

View File

@ -14,7 +14,6 @@ namespace SecurityWindowsDesktop.Controls
public RolesControl() : base()
{
InitializeComponent();
_businessLogic = UnityContainerConfigurator.Resolve<RoleBusinessLogic>();
Title = "Роли";
Id = new Guid("6a33ce5c-e950-4294-9f75-2a0b35941bf7");
Order = 1;

View File

@ -14,7 +14,6 @@ namespace SecurityWindowsDesktop.Controls
public UsersControl() : base()
{
InitializeComponent();
_businessLogic = UnityContainerConfigurator.Resolve<UserBusinessLogic>();
Title = "Пользователи";
Id = new Guid("d5596997-d1f5-4e5e-b94b-6bdd6bca3452");
Order = 1;

View File

@ -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<WindowDesktopExtensionControlModel> GetListControlEntityList()
{
var manager = UnityContainerConfigurator.Resolve<ISecurityManager>();
var manager = DependencyManager.Instance.Resolve<ISecurityManager>();
if (manager == null)
{
return null;