54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
|
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>();
|
|||
|
}
|
|||
|
}
|