From 740312cc6720a07c01446f7a4d07bbaedf6c9e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 30 Nov 2022 10:45:00 +0300 Subject: [PATCH] Inherit IAbpLazyServiceProvider from IServiceProvider Provide methods without Lazy prefix. --- .../AbpLazyServiceProvider.cs | 46 ++++++++++++++---- .../CachedServiceProviderBase.cs | 10 +--- .../IAbpLazyServiceProvider.cs | 48 ++++++++++++++++++- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AbpLazyServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AbpLazyServiceProvider.cs index 7a18689dbe..8af18416ff 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AbpLazyServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AbpLazyServiceProvider.cs @@ -1,15 +1,42 @@ using System; +using Microsoft.Extensions.DependencyInjection; namespace Volo.Abp.DependencyInjection; [ExposeServices(typeof(IAbpLazyServiceProvider))] -public class AbpLazyServiceProvider : CachedServiceProviderBase, IAbpLazyServiceProvider, ITransientDependency +public class AbpLazyServiceProvider : + CachedServiceProviderBase, + IAbpLazyServiceProvider, + ITransientDependency { public AbpLazyServiceProvider(IServiceProvider serviceProvider) : base(serviceProvider) { } + public T GetService(T defaultValue) + { + return (T)GetService(typeof(T), defaultValue); + } + + public object GetService(Type serviceType, object defaultValue) + { + return GetService(serviceType) ?? defaultValue; + } + + public T GetService(Func factory) + { + return (T)GetService(typeof(T), factory); + } + + public object GetService(Type serviceType, Func factory) + { + return CachedServices.GetOrAdd( + serviceType, + _ => new Lazy(() => factory(ServiceProvider)) + ).Value; + } + public virtual T LazyGetRequiredService() { return (T)LazyGetRequiredService(typeof(T)); @@ -17,7 +44,7 @@ public class AbpLazyServiceProvider : CachedServiceProviderBase, IAbpLazyService public virtual object LazyGetRequiredService(Type serviceType) { - return GetRequiredService(serviceType); + return this.GetRequiredService(serviceType); } public virtual T LazyGetService() @@ -30,26 +57,27 @@ public class AbpLazyServiceProvider : CachedServiceProviderBase, IAbpLazyService return GetService(serviceType); } + #region Old Methods + public virtual T LazyGetService(T defaultValue) { - return (T)LazyGetService(typeof(T), defaultValue); + return GetService(defaultValue); } public virtual object LazyGetService(Type serviceType, object defaultValue) { - return LazyGetService(serviceType) ?? defaultValue; + return GetService(serviceType, defaultValue); } public virtual T LazyGetService(Func factory) { - return (T)LazyGetService(typeof(T), factory); + return GetService(factory); } public virtual object LazyGetService(Type serviceType, Func factory) { - return CachedServices.GetOrAdd( - serviceType, - _ => new Lazy(() => factory(ServiceProvider)) - ).Value; + return GetService(serviceType, factory); } + + #endregion } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/CachedServiceProviderBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/CachedServiceProviderBase.cs index 2116ec8fb7..49da116fea 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/CachedServiceProviderBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/CachedServiceProviderBase.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Volo.Abp.DependencyInjection; -public abstract class CachedServiceProviderBase +public abstract class CachedServiceProviderBase : IServiceProvider { protected IServiceProvider ServiceProvider { get; } protected ConcurrentDictionary> CachedServices { get; } @@ -23,12 +23,4 @@ public abstract class CachedServiceProviderBase _ => new Lazy(() => ServiceProvider.GetService(serviceType)) ).Value; } - - public virtual object GetRequiredService(Type serviceType) - { - return CachedServices.GetOrAdd( - serviceType, - _ => new Lazy(() => ServiceProvider.GetRequiredService(serviceType)) - ).Value; - } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IAbpLazyServiceProvider.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IAbpLazyServiceProvider.cs index 610bb40dfa..bfe08026ca 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IAbpLazyServiceProvider.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IAbpLazyServiceProvider.cs @@ -2,21 +2,65 @@ namespace Volo.Abp.DependencyInjection; -public interface IAbpLazyServiceProvider +public interface IAbpLazyServiceProvider : IServiceProvider { + T GetService(T defaultValue); + + object GetService(Type serviceType, object defaultValue); + + T GetService(Func factory); + + object GetService(Type serviceType, Func factory); + + #region Old Methods + + /// + /// This method is equivalent of the GetRequiredService method. + /// It does exists for backward compatibility. + /// T LazyGetRequiredService(); + /// + /// This method is equivalent of the GetRequiredService method. + /// It does exists for backward compatibility. + /// object LazyGetRequiredService(Type serviceType); + /// + /// This method is equivalent of the GetService method. + /// It does exists for backward compatibility. + /// T LazyGetService(); + /// + /// This method is equivalent of the GetService method. + /// It does exists for backward compatibility. + /// object LazyGetService(Type serviceType); + /// + /// This method is equivalent of the method. + /// It does exists for backward compatibility. + /// T LazyGetService(T defaultValue); - + + /// + /// This method is equivalent of the method. + /// It does exists for backward compatibility. + /// object LazyGetService(Type serviceType, object defaultValue); + /// + /// This method is equivalent of the method. + /// It does exists for backward compatibility. + /// object LazyGetService(Type serviceType, Func factory); + /// + /// This method is equivalent of the method. + /// It does exists for backward compatibility. + /// T LazyGetService(Func factory); + + #endregion }