2024-05-22 16:34:25 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace FurnitureAssemblyContracts.DI
|
|
|
|
|
{
|
2024-05-22 17:31:21 +04:00
|
|
|
|
// Менеджер для работы с зависимостями
|
2024-05-22 16:34:25 +04:00
|
|
|
|
public class DependencyManager
|
|
|
|
|
{
|
|
|
|
|
private readonly IDependencyContainer _dependencyManager;
|
|
|
|
|
|
|
|
|
|
private static DependencyManager? _manager;
|
|
|
|
|
|
|
|
|
|
private static readonly object _locjObject = new();
|
|
|
|
|
|
|
|
|
|
private DependencyManager()
|
|
|
|
|
{
|
|
|
|
|
_dependencyManager = new UnityDependencyContainer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DependencyManager Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_manager == null) { lock (_locjObject) { _manager = new DependencyManager(); } }
|
|
|
|
|
|
|
|
|
|
return _manager;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 17:31:21 +04:00
|
|
|
|
// Инициализация библиотек, в которых идут установки зависимостей
|
2024-05-22 16:34:25 +04:00
|
|
|
|
public static void InitDependency()
|
|
|
|
|
{
|
|
|
|
|
var ext = ServiceProviderLoader.GetImplementationExtensions();
|
|
|
|
|
|
|
|
|
|
if (ext == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 17:31:21 +04:00
|
|
|
|
// Регистрируем зависимости
|
2024-05-22 16:34:25 +04:00
|
|
|
|
ext.RegisterServices();
|
2024-05-22 17:07:35 +04:00
|
|
|
|
|
|
|
|
|
var extBusiness = ServiceProviderLoader.GetBusinessLogicImplementationExtensions();
|
|
|
|
|
|
|
|
|
|
if (extBusiness == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Регистрируем зависимости бизнес-логики
|
|
|
|
|
extBusiness.RegisterServices();
|
2024-05-22 16:34:25 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Регистрация логгера
|
|
|
|
|
public void AddLogging(Action<ILoggingBuilder> configure) => _dependencyManager.AddLogging(configure);
|
|
|
|
|
|
|
|
|
|
// Добавление зависимости
|
|
|
|
|
public void RegisterType<T, U>(bool isSingle = false) where U : class, T where T : class => _dependencyManager.RegisterType<T, U>(isSingle);
|
|
|
|
|
|
|
|
|
|
// Добавление зависимости
|
|
|
|
|
public void RegisterType<T>(bool isSingle = false) where T : class => _dependencyManager.RegisterType<T>(isSingle);
|
|
|
|
|
|
2024-05-22 17:31:21 +04:00
|
|
|
|
// Получение класса со всеми зависимостями
|
2024-05-22 16:34:25 +04:00
|
|
|
|
public T Resolve<T>() => _dependencyManager.Resolve<T>();
|
|
|
|
|
}
|
|
|
|
|
}
|