2021-03-26 20:09:52 +04:00
|
|
|
|
using System;
|
|
|
|
|
using Unity;
|
|
|
|
|
using Unity.Lifetime;
|
|
|
|
|
|
|
|
|
|
namespace ModelTools.BusinessLogics
|
|
|
|
|
{
|
2021-03-28 19:05:20 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Работа с UnityContainer
|
|
|
|
|
/// </summary>
|
2021-03-26 20:09:52 +04:00
|
|
|
|
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()
|
|
|
|
|
{
|
2021-03-27 23:50:29 +04:00
|
|
|
|
var ext = ServiceProviderLoader.GetImplementationExtensions();
|
2021-03-26 20:09:52 +04:00
|
|
|
|
if (ext.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Отсутствуют компоненты для загрузки зависимостей по модулям");
|
|
|
|
|
}
|
|
|
|
|
// регистрируем в UnityContainaer зависимости
|
|
|
|
|
foreach (var e in ext)
|
|
|
|
|
{
|
|
|
|
|
e.RegisterServices();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 19:05:20 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление зависимости
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <typeparam name="U"></typeparam>
|
2021-03-26 20:09:52 +04:00
|
|
|
|
public static void PublishService<T, U>() where U : T => Container.RegisterType<T, U>(new HierarchicalLifetimeManager());
|
|
|
|
|
|
2021-03-28 19:05:20 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение класса со всеми зависмостями
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
2021-03-26 20:09:52 +04:00
|
|
|
|
public static T Resolve<T>() => Container.Resolve<T>();
|
|
|
|
|
}
|
|
|
|
|
}
|