PIbd-23_Vrazhkin_S_A_Furnit.../FurnitureAssembly/FurnitureAssemblyContracts/DI/UnityDependencyContainer.cs
2024-05-14 11:56:24 +04:00

53 lines
1.3 KiB
C#

using Microsoft.Extensions.Logging;
using Unity;
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>();
}
}
}
}