using DS.Module.Core.Extensions; using Microsoft.Extensions.DependencyInjection; namespace DS.Module.Core.Modules; public class LazyServiceProvider: ILazyServiceProvider,ITransientDependency { protected Dictionary CacheServices { get; set; } protected IServiceProvider ServiceProvider { get; set; } public LazyServiceProvider(IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; CacheServices = new Dictionary(); } public T LazyGetRequiredService() { return (T)LazyGetRequiredService(typeof(T)); } public object LazyGetRequiredService(Type serviceType) { return CacheServices.GetOrAdd(serviceType, serviceType => ServiceProvider.GetRequiredService(serviceType)); } public T LazyGetService() { return (T)LazyGetService(typeof(T)); } public object LazyGetService(Type serviceType) { return CacheServices.GetOrAdd(serviceType, serviceType => ServiceProvider.GetService(serviceType)); } public T LazyGetService(T defaultValue) { return (T)LazyGetService(typeof(T), defaultValue); } public object LazyGetService(Type serviceType, object defaultValue) { return LazyGetService(serviceType) ?? defaultValue; } public object LazyGetService(Type serviceType, Func factory) { return CacheServices.GetOrAdd(serviceType, serviceType => factory(ServiceProvider)); } public T LazyGetService(Func factory) { return (T)LazyGetService(typeof(T), factory); } }