using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GarmentFactoryContracts.DependencyInjection { public class ServiceDependencyContainer : IDependencyContainer { private ServiceProvider? _serviceProvider; private readonly ServiceCollection _serviceCollection; public ServiceDependencyContainer() { _serviceCollection = new ServiceCollection(); } public void AddLogging(Action configure) { _serviceCollection.AddLogging(configure); } public void RegisterType(bool isSingle) where U : class, T where T : class { if (isSingle) { _serviceCollection.AddSingleton(); } else { _serviceCollection.AddTransient(); } _serviceProvider = null; } public void RegisterType(bool isSingle) where T : class { if (isSingle) { _serviceCollection.AddSingleton(); } else { _serviceCollection.AddTransient(); } _serviceProvider = null; } public T Resolve() { if (_serviceProvider == null) { _serviceProvider = _serviceCollection.BuildServiceProvider(); } return _serviceProvider.GetService()!; } } }