using DS.Module.Core.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace DS.Module.Core.Modules;
///
/// IOC管理
///
public class DSIocManage
{
///
/// 服务提供者
///
private IServiceProvider _provider;
///
/// 服务集合
///
private IServiceCollection _services;
///
/// 创建懒加载Ioc管理实例
///
private static readonly Lazy DSInstanceLazy = new Lazy(() => new DSIocManage());
///
/// 构造方法
///
private DSIocManage()
{
}
public static DSIocManage Instance => DSInstanceLazy.Value;
///
/// 设置应用程序服务提供者
///
internal void SetApplicationServiceProvider(IServiceProvider provider)
{
provider.NotNull(nameof(provider));
_provider = provider;
}
///
/// 设置应用程序服务集合
///
public void SetServiceCollection(IServiceCollection services)
{
services.NotNull(nameof(services));
_services = services;
}
///
/// 得到服务
///
///
///
public T GetService()
{
_provider.NotNull(nameof(_provider));
_services.NotNull(nameof(_services));
return _provider.GetService();
}
///
/// 得到日志记录
///
///
///
public ILogger GetLogger()
{
ILoggerFactory factory = _provider.GetService();
return factory.CreateLogger();
}
}