From 5797844543dbacab67020906aa07e5c9fe198f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Thu, 16 May 2024 23:31:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20Un?= =?UTF-8?q?ity-=D0=BA=D0=BE=D0=BD=D1=82=D0=B5=D0=B9=D0=BD=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DI/DependencyManager.cs | 4 +- .../DI/UnityDependencyContainer.cs | 37 +++++++++++++++++++ .../IceCreamShopContracts.csproj | 2 + 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 IceCreamShop/IceCreamShopContracts/DI/UnityDependencyContainer.cs diff --git a/IceCreamShop/IceCreamShopContracts/DI/DependencyManager.cs b/IceCreamShop/IceCreamShopContracts/DI/DependencyManager.cs index a1f4b33..c30650b 100644 --- a/IceCreamShop/IceCreamShopContracts/DI/DependencyManager.cs +++ b/IceCreamShop/IceCreamShopContracts/DI/DependencyManager.cs @@ -12,8 +12,8 @@ namespace IceCreamShopContracts.DI private DependencyManager() { - _dependencyManager = new ServiceDependencyContainer(); - } + _dependencyManager = new UnityDependencyContainer(); + } public static DependencyManager Instance { get { if (_manager == null) { lock (_lockObject) { _manager = new DependencyManager(); } } return _manager; } } diff --git a/IceCreamShop/IceCreamShopContracts/DI/UnityDependencyContainer.cs b/IceCreamShop/IceCreamShopContracts/DI/UnityDependencyContainer.cs new file mode 100644 index 0000000..7a09e75 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/DI/UnityDependencyContainer.cs @@ -0,0 +1,37 @@ +using Microsoft.Extensions.Logging; +using Unity.Microsoft.Logging; +using Unity; + +namespace IceCreamShopContracts.DI +{ + public class UnityDependencyContainer : IDependencyContainer + { + private readonly IUnityContainer _container; + + public UnityDependencyContainer() + { + _container = new UnityContainer(); + } + + public void AddLogging(Action configure) + { + var factory = LoggerFactory.Create(configure); + _container.AddExtension(new LoggingExtension(factory)); + } + + void IDependencyContainer.RegisterType(bool isSingle) + { + _container.RegisterType(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient); + } + + public void RegisterType(bool isSingle) where T : class + { + _container.RegisterType(isSingle ? TypeLifetime.Singleton : TypeLifetime.Transient); + } + + public T Resolve() + { + return _container.Resolve(); + } + } +} diff --git a/IceCreamShop/IceCreamShopContracts/IceCreamShopContracts.csproj b/IceCreamShop/IceCreamShopContracts/IceCreamShopContracts.csproj index b7213e7..046ae9e 100644 --- a/IceCreamShop/IceCreamShopContracts/IceCreamShopContracts.csproj +++ b/IceCreamShop/IceCreamShopContracts/IceCreamShopContracts.csproj @@ -8,6 +8,8 @@ + +