DepartmentProject/DepartmentPortal/Common/ModuleTools/BusinessLogics/DependencyManager.cs

69 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>();
/// <summary>
/// Получение класса со всеми зависмостями
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public object Resolve(Type t) => _dependencyManager.Resolve(t);
}
}