61 lines
1.2 KiB
C#
61 lines
1.2 KiB
C#
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 SewingDressesContracts.DI
|
|
{
|
|
public class ServiceDependencyContainer : IDependencyContainer
|
|
{
|
|
private ServiceProvider? _serviceProvider;
|
|
private readonly ServiceCollection _serviceCollection;
|
|
|
|
public ServiceDependencyContainer()
|
|
{
|
|
_serviceCollection = new ServiceCollection();
|
|
}
|
|
public void AddLogging(Action<ILoggingBuilder> configure)
|
|
{
|
|
_serviceCollection.AddLogging(configure);
|
|
}
|
|
|
|
public void RegisterType<T>(bool isSingle) where T : class
|
|
{
|
|
if (isSingle)
|
|
{
|
|
_serviceCollection.AddSingleton<T>();
|
|
}
|
|
else
|
|
{
|
|
_serviceCollection.AddTransient<T>();
|
|
}
|
|
_serviceProvider = null;
|
|
}
|
|
|
|
public T Resolve<T>()
|
|
{
|
|
if (_serviceProvider == null)
|
|
{
|
|
_serviceProvider = _serviceCollection.BuildServiceProvider();
|
|
}
|
|
return _serviceProvider.GetService<T>();
|
|
}
|
|
|
|
void IDependencyContainer.RegisterType<T, U>(bool isSingle)
|
|
{
|
|
if (isSingle)
|
|
{
|
|
_serviceCollection.AddSingleton<T, U>();
|
|
}
|
|
else
|
|
{
|
|
_serviceCollection.AddTransient<T, U>();
|
|
}
|
|
_serviceProvider = null;
|
|
}
|
|
}
|
|
}
|