PIbd-21_Putincev_D.M._FoodO.../FoodOrders/FoodOrdersContracts/DI/UnityDependencyContainer.cs

38 lines
1.0 KiB
C#
Raw Normal View History

using Microsoft.Extensions.Logging;
using Unity.Microsoft.Logging;
using Unity;
namespace FoodOrdersContracts.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, U>(bool isSingle) where U : class, T where T : class
{
_container.RegisterType<T, U>(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient);
}
public void RegisterType<T>(bool isSingle) where T : class
{
_container.RegisterType<T>(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient);
}
public T Resolve<T>()
{
return _container.Resolve<T>();
}
}
}