85 lines
2.0 KiB
C#
85 lines
2.0 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Unity;
|
||
using Unity.Microsoft.Logging;
|
||
|
||
namespace AircraftPlantContracts.DependencyInjections
|
||
{
|
||
/// <summary>
|
||
/// Реализация интерфейса установки зависимости между элементами
|
||
/// </summary>
|
||
public class UnityDependencyContainer : IDependencyContainer
|
||
{
|
||
/// <summary>
|
||
/// Контейнер сервисов для регистрации зависимостей
|
||
/// </summary>
|
||
private readonly UnityContainer _container;
|
||
|
||
/// <summary>
|
||
/// Конструктор
|
||
/// </summary>
|
||
public UnityDependencyContainer()
|
||
{
|
||
_container = new UnityContainer();
|
||
}
|
||
|
||
/// <summary>
|
||
/// Регистрация логгера
|
||
/// </summary>
|
||
/// <param name="configure"></param>
|
||
public void AddLogging(Action<ILoggingBuilder> configure)
|
||
{
|
||
_container.AddExtension(new LoggingExtension(LoggerFactory.Create(configure)));
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление зависимости
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <typeparam name="U"></typeparam>
|
||
/// <param name="isSingle"></param>
|
||
public void RegisterType<T, U>(bool isSingle) where U : class, T where T : class
|
||
{
|
||
if (isSingle)
|
||
{
|
||
_container.RegisterSingleton<T, U>();
|
||
}
|
||
else
|
||
{
|
||
_container.RegisterType<T, U>();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление зависимости
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="isSingle"></param>
|
||
public void RegisterType<T>(bool isSingle) where T : class
|
||
{
|
||
if (isSingle)
|
||
{
|
||
_container.RegisterSingleton<T>();
|
||
}
|
||
else
|
||
{
|
||
_container.RegisterType<T>();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение класса со всеми зависимостями
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <returns></returns>
|
||
public T Resolve<T>()
|
||
{
|
||
return _container.Resolve<T>();
|
||
}
|
||
}
|
||
}
|