Pibd-22_Presnyakova.V.V_Jew.../JewelryStoreContracts/DI/UnityDependencyContainer.cs

40 lines
944 B
C#
Raw Normal View History

2023-05-02 14:50:43 +04:00
using Microsoft.Extensions.Logging;
2023-05-09 22:09:41 +04:00
using System.ComponentModel;
2023-05-02 14:50:43 +04:00
using Unity;
2023-05-09 22:09:41 +04:00
using Unity.Lifetime;
2023-05-02 14:50:43 +04:00
using Unity.Microsoft.Logging;
namespace JewelryStoreContracts.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
{
_container.RegisterType<T>(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient);
}
public T Resolve<T>()
{
return _container.Resolve<T>();
}
void IDependencyContainer.RegisterType<T, U>(bool isSingle)
{
_container.RegisterType<T, U>(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient);
}
}
}