Browse Source

Inherit IAbpLazyServiceProvider from IServiceProvider

Provide methods without Lazy prefix.
pull/14879/head
Halil İbrahim Kalkan 4 years ago
parent
commit
740312cc67
  1. 46
      framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AbpLazyServiceProvider.cs
  2. 10
      framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/CachedServiceProviderBase.cs
  3. 48
      framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IAbpLazyServiceProvider.cs

46
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/AbpLazyServiceProvider.cs

@ -1,15 +1,42 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection;
namespace Volo.Abp.DependencyInjection; namespace Volo.Abp.DependencyInjection;
[ExposeServices(typeof(IAbpLazyServiceProvider))] [ExposeServices(typeof(IAbpLazyServiceProvider))]
public class AbpLazyServiceProvider : CachedServiceProviderBase, IAbpLazyServiceProvider, ITransientDependency public class AbpLazyServiceProvider :
CachedServiceProviderBase,
IAbpLazyServiceProvider,
ITransientDependency
{ {
public AbpLazyServiceProvider(IServiceProvider serviceProvider) public AbpLazyServiceProvider(IServiceProvider serviceProvider)
: base(serviceProvider) : base(serviceProvider)
{ {
} }
public T GetService<T>(T defaultValue)
{
return (T)GetService(typeof(T), defaultValue);
}
public object GetService(Type serviceType, object defaultValue)
{
return GetService(serviceType) ?? defaultValue;
}
public T GetService<T>(Func<IServiceProvider, object> factory)
{
return (T)GetService(typeof(T), factory);
}
public object GetService(Type serviceType, Func<IServiceProvider, object> factory)
{
return CachedServices.GetOrAdd(
serviceType,
_ => new Lazy<object>(() => factory(ServiceProvider))
).Value;
}
public virtual T LazyGetRequiredService<T>() public virtual T LazyGetRequiredService<T>()
{ {
return (T)LazyGetRequiredService(typeof(T)); return (T)LazyGetRequiredService(typeof(T));
@ -17,7 +44,7 @@ public class AbpLazyServiceProvider : CachedServiceProviderBase, IAbpLazyService
public virtual object LazyGetRequiredService(Type serviceType) public virtual object LazyGetRequiredService(Type serviceType)
{ {
return GetRequiredService(serviceType); return this.GetRequiredService(serviceType);
} }
public virtual T LazyGetService<T>() public virtual T LazyGetService<T>()
@ -30,26 +57,27 @@ public class AbpLazyServiceProvider : CachedServiceProviderBase, IAbpLazyService
return GetService(serviceType); return GetService(serviceType);
} }
#region Old Methods
public virtual T LazyGetService<T>(T defaultValue) public virtual T LazyGetService<T>(T defaultValue)
{ {
return (T)LazyGetService(typeof(T), defaultValue); return GetService(defaultValue);
} }
public virtual object LazyGetService(Type serviceType, object defaultValue) public virtual object LazyGetService(Type serviceType, object defaultValue)
{ {
return LazyGetService(serviceType) ?? defaultValue; return GetService(serviceType, defaultValue);
} }
public virtual T LazyGetService<T>(Func<IServiceProvider, object> factory) public virtual T LazyGetService<T>(Func<IServiceProvider, object> factory)
{ {
return (T)LazyGetService(typeof(T), factory); return GetService<T>(factory);
} }
public virtual object LazyGetService(Type serviceType, Func<IServiceProvider, object> factory) public virtual object LazyGetService(Type serviceType, Func<IServiceProvider, object> factory)
{ {
return CachedServices.GetOrAdd( return GetService(serviceType, factory);
serviceType,
_ => new Lazy<object>(() => factory(ServiceProvider))
).Value;
} }
#endregion
} }

10
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/CachedServiceProviderBase.cs

@ -4,7 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace Volo.Abp.DependencyInjection; namespace Volo.Abp.DependencyInjection;
public abstract class CachedServiceProviderBase public abstract class CachedServiceProviderBase : IServiceProvider
{ {
protected IServiceProvider ServiceProvider { get; } protected IServiceProvider ServiceProvider { get; }
protected ConcurrentDictionary<Type, Lazy<object>> CachedServices { get; } protected ConcurrentDictionary<Type, Lazy<object>> CachedServices { get; }
@ -23,12 +23,4 @@ public abstract class CachedServiceProviderBase
_ => new Lazy<object>(() => ServiceProvider.GetService(serviceType)) _ => new Lazy<object>(() => ServiceProvider.GetService(serviceType))
).Value; ).Value;
} }
public virtual object GetRequiredService(Type serviceType)
{
return CachedServices.GetOrAdd(
serviceType,
_ => new Lazy<object>(() => ServiceProvider.GetRequiredService(serviceType))
).Value;
}
} }

48
framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/IAbpLazyServiceProvider.cs

@ -2,21 +2,65 @@
namespace Volo.Abp.DependencyInjection; namespace Volo.Abp.DependencyInjection;
public interface IAbpLazyServiceProvider public interface IAbpLazyServiceProvider : IServiceProvider
{ {
T GetService<T>(T defaultValue);
object GetService(Type serviceType, object defaultValue);
T GetService<T>(Func<IServiceProvider, object> factory);
object GetService(Type serviceType, Func<IServiceProvider, object> factory);
#region Old Methods
/// <summary>
/// This method is equivalent of the GetRequiredService method.
/// It does exists for backward compatibility.
/// </summary>
T LazyGetRequiredService<T>(); T LazyGetRequiredService<T>();
/// <summary>
/// This method is equivalent of the GetRequiredService method.
/// It does exists for backward compatibility.
/// </summary>
object LazyGetRequiredService(Type serviceType); object LazyGetRequiredService(Type serviceType);
/// <summary>
/// This method is equivalent of the GetService method.
/// It does exists for backward compatibility.
/// </summary>
T LazyGetService<T>(); T LazyGetService<T>();
/// <summary>
/// This method is equivalent of the GetService method.
/// It does exists for backward compatibility.
/// </summary>
object LazyGetService(Type serviceType); object LazyGetService(Type serviceType);
/// <summary>
/// This method is equivalent of the <see cref="GetService{T}(T)"/> method.
/// It does exists for backward compatibility.
/// </summary>
T LazyGetService<T>(T defaultValue); T LazyGetService<T>(T defaultValue);
/// <summary>
/// This method is equivalent of the <see cref="GetService(Type, object)"/> method.
/// It does exists for backward compatibility.
/// </summary>
object LazyGetService(Type serviceType, object defaultValue); object LazyGetService(Type serviceType, object defaultValue);
/// <summary>
/// This method is equivalent of the <see cref="GetService(Type, Func{IServiceProvider, object})"/> method.
/// It does exists for backward compatibility.
/// </summary>
object LazyGetService(Type serviceType, Func<IServiceProvider, object> factory); object LazyGetService(Type serviceType, Func<IServiceProvider, object> factory);
/// <summary>
/// This method is equivalent of the <see cref="GetService{T}(T)"/> method.
/// It does exists for backward compatibility.
/// </summary>
T LazyGetService<T>(Func<IServiceProvider, object> factory); T LazyGetService<T>(Func<IServiceProvider, object> factory);
#endregion
} }

Loading…
Cancel
Save