using MAX.Core.Infrastructure; using System; using System.Collections.Concurrent; namespace MAX.Core.DependencyInjection; public interface ILazyServiceProvider { object LazyGetService(Type serviceType); T LazyGetService(); } public class LazyServiceProvider : SingleInstance, ILazyServiceProvider { protected ConcurrentDictionary> CachedServices { get; } private LazyServiceProvider() { CachedServices = new ConcurrentDictionary>(); } public virtual T LazyGetService() { return (T)LazyGetService(typeof(T)); } public virtual object LazyGetService(Type serviceType) { return GetService(serviceType); } public virtual object GetService(Type serviceType) { return CachedServices.GetOrAdd( serviceType, _ => new Lazy(() => IocManager.Resolve(serviceType)) ).Value; } }