ISEbd-21_Zinovev_V.V._Furni.../FurnitureAssembly/FurnitureAssemblyContracts/DI/UnityDependencyContainer.cs
2024-06-17 18:47:33 +04:00

59 lines
1.4 KiB
C#

using Microsoft.Extensions.Logging;
using Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity.Microsoft.Logging;
namespace FurnitureAssemblyContracts.DI
{
public class UnityDependencyContainer : IDependencyContainer
{
private readonly IUnityContainer container;
public UnityDependencyContainer()
{
container = new UnityContainer();
}
public void AddLogging(Action<ILoggingBuilder> configure)
{
// регистрируем логгер в контейнере
var factory = LoggerFactory.Create(configure);
container.AddExtension(new LoggingExtension(factory));
}
public void RegisterType<T>(bool isSingle) where T : class
{
if (isSingle)
{
container.RegisterSingleton<T>();
}
else
{
container.RegisterType<T>();
}
}
public T Resolve<T>()
{
return container.Resolve<T>();
}
void IDependencyContainer.RegisterType<T, U>(bool isSingle)
{
if (isSingle)
{
container.RegisterSingleton<T, U>();
}
else
{
container.RegisterType<T, U>();
}
}
}
}