diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs index 7174dbe431..4e30a50206 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs @@ -4,35 +4,19 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars { public class ToolbarConfigurationContext : IToolbarConfigurationContext { public IServiceProvider ServiceProvider { get; } - private readonly object _serviceProviderLock = new object(); - private TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (_serviceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } - - return reference; - } + private readonly IAbpLazyServiceProvider _lazyServiceProvider; - public IAuthorizationService AuthorizationService => LazyGetRequiredService(typeof(IAuthorizationService), ref _authorizationService); - private IAuthorizationService _authorizationService; + public IAuthorizationService AuthorizationService => _lazyServiceProvider.LazyGetRequiredService(); - private IStringLocalizerFactory _stringLocalizerFactory; - public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(typeof(IStringLocalizerFactory),ref _stringLocalizerFactory); + public IStringLocalizerFactory StringLocalizerFactory => _lazyServiceProvider.LazyGetRequiredService(); public Toolbar Toolbar { get; } @@ -40,6 +24,7 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars { Toolbar = toolbar; ServiceProvider = serviceProvider; + _lazyServiceProvider = ServiceProvider.GetRequiredService(); } public Task IsGrantedAsync(string policyName) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationContext.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationContext.cs index 98fe3546c5..00c3762ed9 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationContext.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationContext.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; using Volo.Abp.Localization; namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling @@ -13,35 +14,18 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling public IFileProvider FileProvider { get; } + public IServiceProvider ServiceProvider { get; } + + private readonly IAbpLazyServiceProvider _lazyServiceProvider; + public BundleConfigurationContext(IServiceProvider serviceProvider, IFileProvider fileProvider) { Files = new List(); ServiceProvider = serviceProvider; + _lazyServiceProvider = ServiceProvider.GetRequiredService(); FileProvider = fileProvider; } - public IServiceProvider ServiceProvider { get; } - private readonly object _serviceProviderLock = new object(); - - private TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (_serviceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } - - return reference; - } - - private IOptions _abpLocalizationOptions; - - public AbpLocalizationOptions LocalizationOptions => - LazyGetRequiredService(typeof(IOptions), ref _abpLocalizationOptions).Value; + public AbpLocalizationOptions LocalizationOptions => _lazyServiceProvider.LazyGetRequiredService>().Value; } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarConfigurationContext.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarConfigurationContext.cs index 37f3308198..7ae40af60f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarConfigurationContext.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarConfigurationContext.cs @@ -5,35 +5,19 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using Volo.Abp.AspNetCore.Mvc.UI.Theming; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars { public class ToolbarConfigurationContext : IToolbarConfigurationContext { public IServiceProvider ServiceProvider { get; } - private readonly object _serviceProviderLock = new object(); - private TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (_serviceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } - - return reference; - } + private readonly IAbpLazyServiceProvider _lazyServiceProvider; - public IAuthorizationService AuthorizationService => LazyGetRequiredService(typeof(IAuthorizationService), ref _authorizationService); - private IAuthorizationService _authorizationService; + public IAuthorizationService AuthorizationService => _lazyServiceProvider.LazyGetRequiredService(); - private IStringLocalizerFactory _stringLocalizerFactory; - public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(typeof(IStringLocalizerFactory),ref _stringLocalizerFactory); + public IStringLocalizerFactory StringLocalizerFactory => _lazyServiceProvider.LazyGetRequiredService(); public ITheme Theme { get; } @@ -44,8 +28,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars Theme = currentTheme; Toolbar = toolbar; ServiceProvider = serviceProvider; + _lazyServiceProvider = ServiceProvider.GetRequiredService(); } - + public Task IsGrantedAsync(string policyName) { return AuthorizationService.IsGrantedAsync(policyName); @@ -62,7 +47,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars { return StringLocalizerFactory.Create(); } - + [NotNull] public IStringLocalizer GetLocalizer(Type resourceType) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/AspNetCore/Mvc/UI/RazorPages/AbpPageModel.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/AspNetCore/Mvc/UI/RazorPages/AbpPageModel.cs index fcbe9af59c..449747d0cc 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/AspNetCore/Mvc/UI/RazorPages/AbpPageModel.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/AspNetCore/Mvc/UI/RazorPages/AbpPageModel.cs @@ -10,6 +10,7 @@ using System; using System.Threading.Tasks; using Volo.Abp.AspNetCore.Mvc.UI.Alerts; using Volo.Abp.AspNetCore.Mvc.Validation; +using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; using Volo.Abp.Localization; using Volo.Abp.MultiTenancy; @@ -24,67 +25,27 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.RazorPages { public abstract class AbpPageModel : PageModel { - public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - - protected TService LazyGetRequiredService(ref TService reference) - => LazyGetRequiredService(typeof(TService), ref reference); + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - protected TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } - - return reference; - } + public IServiceProvider ServiceProvider { get; set; } - protected IClock Clock => LazyGetRequiredService(ref _clock); - private IClock _clock; + protected IClock Clock => LazyServiceProvider.LazyGetRequiredService(); protected AlertList Alerts => AlertManager.Alerts; - protected IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager); - private IUnitOfWorkManager _unitOfWorkManager; + protected IUnitOfWorkManager UnitOfWorkManager => LazyServiceProvider.LazyGetRequiredService(); protected Type ObjectMapperContext { get; set; } - protected IObjectMapper ObjectMapper - { - get - { - if (_objectMapper != null) - { - return _objectMapper; - } - - if (ObjectMapperContext == null) - { - return LazyGetRequiredService(ref _objectMapper); - } - - return LazyGetRequiredService( - typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext), - ref _objectMapper - ); - } - } - private IObjectMapper _objectMapper; + protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService(provider => + ObjectMapperContext == null + ? provider.GetRequiredService() + : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext))); - protected IGuidGenerator GuidGenerator => LazyGetRequiredService(ref _guidGenerator); - private IGuidGenerator _guidGenerator; + protected IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService(SimpleGuidGenerator.Instance); - protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); - private ILoggerFactory _loggerFactory; + protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService(); - protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); - private IStringLocalizerFactory _stringLocalizerFactory; + protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetRequiredService(); protected IStringLocalizer L { @@ -103,31 +64,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.RazorPages protected Type LocalizationResourceType { get; set; } - protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); - private ICurrentUser _currentUser; + protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetRequiredService(); - protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); - private ICurrentTenant _currentTenant; + protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService(); - protected ISettingProvider SettingProvider => LazyGetRequiredService(ref _settingProvider); - private ISettingProvider _settingProvider; + protected ISettingProvider SettingProvider => LazyServiceProvider.LazyGetRequiredService(); - protected IModelStateValidator ModelValidator => LazyGetRequiredService(ref _modelValidator); - private IModelStateValidator _modelValidator; + protected IModelStateValidator ModelValidator => LazyServiceProvider.LazyGetRequiredService(); - protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); - private IAuthorizationService _authorizationService; + protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetRequiredService(); - protected IAlertManager AlertManager => LazyGetRequiredService(ref _alertManager); - private IAlertManager _alertManager; + protected IAlertManager AlertManager => LazyServiceProvider.LazyGetRequiredService(); protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; - protected ILogger Logger => _lazyLogger.Value; - private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); + protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance); - protected IAppUrlProvider AppUrlProvider => LazyGetRequiredService(ref _appUrlProvider); - private IAppUrlProvider _appUrlProvider; + protected IAppUrlProvider AppUrlProvider => LazyServiceProvider.LazyGetRequiredService(); protected virtual NoContentResult NoContent() //TODO: Is that true to return empty result like that? { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs index f83353092e..52f1d251cb 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Aspects; using Volo.Abp.AspNetCore.Mvc.Validation; +using Volo.Abp.DependencyInjection; using Volo.Abp.Features; using Volo.Abp.Guids; using Volo.Abp.Localization; @@ -22,85 +23,41 @@ namespace Volo.Abp.AspNetCore.Mvc { public abstract class AbpController : Controller, IAvoidDuplicateCrossCuttingConcerns { - public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - - protected TService LazyGetRequiredService(ref TService reference) - => LazyGetRequiredService(typeof(TService), ref reference); - - protected TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - return reference; - } + public IServiceProvider ServiceProvider { get; set; } - protected IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager); - private IUnitOfWorkManager _unitOfWorkManager; + protected IUnitOfWorkManager UnitOfWorkManager => LazyServiceProvider.LazyGetRequiredService(); protected Type ObjectMapperContext { get; set; } - protected IObjectMapper ObjectMapper - { - get - { - if (_objectMapper != null) - { - return _objectMapper; - } + protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService(provider => + ObjectMapperContext == null + ? provider.GetRequiredService() + : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext))); - if (ObjectMapperContext == null) - { - return LazyGetRequiredService(ref _objectMapper); - } + protected IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService(SimpleGuidGenerator.Instance); - return LazyGetRequiredService( - typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext), - ref _objectMapper - ); - } - } - private IObjectMapper _objectMapper; - - protected IGuidGenerator GuidGenerator => LazyGetRequiredService(ref _guidGenerator); - private IGuidGenerator _guidGenerator; - - protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); - private ILoggerFactory _loggerFactory; + protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService(); - protected ILogger Logger => _lazyLogger.Value; - private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); + protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance); - protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); - private ICurrentUser _currentUser; + protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetRequiredService(); - protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); - private ICurrentTenant _currentTenant; + protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService(); - protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); - private IAuthorizationService _authorizationService; + protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetRequiredService(); protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; - protected IClock Clock => LazyGetRequiredService(ref _clock); - private IClock _clock; + protected IClock Clock => LazyServiceProvider.LazyGetRequiredService(); - protected IModelStateValidator ModelValidator => LazyGetRequiredService(ref _modelValidator); - private IModelStateValidator _modelValidator; + protected IModelStateValidator ModelValidator => LazyServiceProvider.LazyGetRequiredService(); - protected IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker); - private IFeatureChecker _featureChecker; + protected IFeatureChecker FeatureChecker => LazyServiceProvider.LazyGetRequiredService(); - protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); - private IStringLocalizerFactory _stringLocalizerFactory; + protected IAppUrlProvider AppUrlProvider => LazyServiceProvider.LazyGetRequiredService(); + + protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetRequiredService(); protected IStringLocalizer L { @@ -116,9 +73,6 @@ namespace Volo.Abp.AspNetCore.Mvc } private IStringLocalizer _localizer; - protected IAppUrlProvider AppUrlProvider => LazyGetRequiredService(ref _appUrlProvider); - private IAppUrlProvider _appUrlProvider; - protected Type LocalizationResource { get => _localizationResource; diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs index 2c0e38c13c..2ac02b8ac5 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs @@ -1,55 +1,22 @@ using System; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.DependencyInjection; using Volo.Abp.ObjectMapping; namespace Volo.Abp.AspNetCore.Mvc { public abstract class AbpViewComponent : ViewComponent { - public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - - protected TService LazyGetRequiredService(ref TService reference) - => LazyGetRequiredService(typeof(TService), ref reference); - - protected TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - return reference; - } + public IServiceProvider ServiceProvider { get; set; } protected Type ObjectMapperContext { get; set; } - protected IObjectMapper ObjectMapper - { - get - { - if (_objectMapper != null) - { - return _objectMapper; - } - - if (ObjectMapperContext == null) - { - return LazyGetRequiredService(ref _objectMapper); - } - return LazyGetRequiredService( - typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext), - ref _objectMapper - ); - } - } - private IObjectMapper _objectMapper; + protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService(provider => + ObjectMapperContext == null + ? provider.GetRequiredService() + : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext))); } } diff --git a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpHub.cs b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpHub.cs index 4e23a4cb41..a9d9316f41 100644 --- a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpHub.cs +++ b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpHub.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.DependencyInjection; using Volo.Abp.Localization; using Volo.Abp.MultiTenancy; using Volo.Abp.Timing; @@ -14,48 +15,23 @@ namespace Volo.Abp.AspNetCore.SignalR { public abstract class AbpHub : Hub { - public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - - protected TService LazyGetRequiredService(ref TService reference) - => LazyGetRequiredService(typeof(TService), ref reference); - - protected TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - return reference; - } + public IServiceProvider ServiceProvider { get; set; } - protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); - private ILoggerFactory _loggerFactory; + protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetService(); - protected ILogger Logger => _lazyLogger.Value; - private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); + protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance); - protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); - private ICurrentUser _currentUser; + protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetService(); - protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); - private ICurrentTenant _currentTenant; + protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetService(); - protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); - private IAuthorizationService _authorizationService; + protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetService(); - protected IClock Clock => LazyGetRequiredService(ref _clock); - private IClock _clock; + protected IClock Clock => LazyServiceProvider.LazyGetService(); - protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); - private IStringLocalizerFactory _stringLocalizerFactory; + protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetService(); protected IStringLocalizer L { @@ -102,48 +78,23 @@ namespace Volo.Abp.AspNetCore.SignalR public abstract class AbpHub : Hub where T : class { - public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - - protected TService LazyGetRequiredService(ref TService reference) - => LazyGetRequiredService(typeof(TService), ref reference); - - protected TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - return reference; - } + public IServiceProvider ServiceProvider { get; set; } - protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); - private ILoggerFactory _loggerFactory; + protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetService(); - protected ILogger Logger => _lazyLogger.Value; - private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); + protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance); - protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); - private ICurrentUser _currentUser; + protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetService(); - protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); - private ICurrentTenant _currentTenant; + protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetService(); - protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); - private IAuthorizationService _authorizationService; + protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetService(); - protected IClock Clock => LazyGetRequiredService(ref _clock); - private IClock _clock; + protected IClock Clock => LazyServiceProvider.LazyGetService(); - protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); - private IStringLocalizerFactory _stringLocalizerFactory; + protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetService(); protected IStringLocalizer L { diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs index bf654cf6a7..85523d7ee9 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs @@ -1,9 +1,9 @@ using System; using System.Threading; using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.BackgroundWorkers { @@ -13,33 +13,14 @@ namespace Volo.Abp.BackgroundWorkers public abstract class BackgroundWorkerBase : IBackgroundWorker { //TODO: Add UOW, Localization and other useful properties..? - public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - protected TService LazyGetRequiredService(ref TService reference) - => LazyGetRequiredService(typeof(TService), ref reference); + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - protected TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } - - return reference; - } + public IServiceProvider ServiceProvider { get; set; } - protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); - private ILoggerFactory _loggerFactory; + protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService(); - protected ILogger Logger => _lazyLogger.Value; - private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); + protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance); public virtual Task StartAsync(CancellationToken cancellationToken = default) { diff --git a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs index 900343800f..232afb4280 100644 --- a/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs +++ b/framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs @@ -33,86 +33,41 @@ namespace Volo.Abp.Application.Services IAuditingEnabled, ITransientDependency { - public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - - protected TService LazyGetRequiredService(ref TService reference) - => LazyGetRequiredService(typeof(TService), ref reference); + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } - protected TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } - - return reference; - } + public IServiceProvider ServiceProvider { get; set; } public static string[] CommonPostfixes { get; set; } = { "AppService", "ApplicationService", "Service" }; public List AppliedCrossCuttingConcerns { get; } = new List(); - protected IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager); - private IUnitOfWorkManager _unitOfWorkManager; - - protected IAsyncQueryableExecuter AsyncExecuter => LazyGetRequiredService(ref _asyncExecuter); - private IAsyncQueryableExecuter _asyncExecuter; + protected IUnitOfWorkManager UnitOfWorkManager => LazyServiceProvider.LazyGetRequiredService(); - protected Type ObjectMapperContext { get; set; } - protected IObjectMapper ObjectMapper - { - get - { - if (_objectMapper != null) - { - return _objectMapper; - } - - if (ObjectMapperContext == null) - { - return LazyGetRequiredService(ref _objectMapper); - } + protected IAsyncQueryableExecuter AsyncExecuter => LazyServiceProvider.LazyGetRequiredService(); - return LazyGetRequiredService( - typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext), - ref _objectMapper - ); - } - } - private IObjectMapper _objectMapper; + protected Type ObjectMapperContext { get; set; } + protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService(provider => + ObjectMapperContext == null + ? provider.GetRequiredService() + : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext))); - public IGuidGenerator GuidGenerator { get; set; } + public IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService(SimpleGuidGenerator.Instance); - protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); - private ILoggerFactory _loggerFactory; + protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService(); - protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); - private ICurrentTenant _currentTenant; + protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService(); - protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); - private ICurrentUser _currentUser; + protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetRequiredService(); - protected ISettingProvider SettingProvider => LazyGetRequiredService(ref _settingProvider); - private ISettingProvider _settingProvider; + protected ISettingProvider SettingProvider => LazyServiceProvider.LazyGetRequiredService(); - protected IClock Clock => LazyGetRequiredService(ref _clock); - private IClock _clock; + protected IClock Clock => LazyServiceProvider.LazyGetRequiredService(); - protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); - private IAuthorizationService _authorizationService; + protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetRequiredService(); - protected IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker); - private IFeatureChecker _featureChecker; + protected IFeatureChecker FeatureChecker => LazyServiceProvider.LazyGetRequiredService(); - protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); - private IStringLocalizerFactory _stringLocalizerFactory; + protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetRequiredService(); protected IStringLocalizer L { @@ -141,13 +96,7 @@ namespace Volo.Abp.Application.Services protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; - protected ILogger Logger => _lazyLogger.Value; - private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); - - protected ApplicationService() - { - GuidGenerator = SimpleGuidGenerator.Instance; - } + protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance); /// /// Checks for given . diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs index 9792cbd0e6..751b938f1d 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs @@ -1,7 +1,7 @@ using System; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.DependencyInjection; using Volo.Abp.Guids; using Volo.Abp.Linq; using Volo.Abp.MultiTenancy; @@ -11,44 +11,20 @@ namespace Volo.Abp.Domain.Services { public abstract class DomainService : IDomainService { + public IAbpLazyServiceProvider LazyServiceProvider { get; set; } + public IServiceProvider ServiceProvider { get; set; } - protected readonly object ServiceProviderLock = new object(); - protected TService LazyGetRequiredService(ref TService reference) - { - if (reference == null) - { - lock (ServiceProviderLock) - { - if (reference == null) - { - reference = ServiceProvider.GetRequiredService(); - } - } - } - - return reference; - } - - protected IClock Clock => LazyGetRequiredService(ref _clock); - private IClock _clock; - - public IGuidGenerator GuidGenerator { get; set; } - - protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); - private ILoggerFactory _loggerFactory; - - protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); - private ICurrentTenant _currentTenant; - - protected IAsyncQueryableExecuter AsyncExecuter => LazyGetRequiredService(ref _asyncExecuter); - private IAsyncQueryableExecuter _asyncExecuter; - - protected ILogger Logger => _lazyLogger.Value; - private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); - - protected DomainService() - { - GuidGenerator = SimpleGuidGenerator.Instance; - } + + protected IClock Clock => LazyServiceProvider.LazyGetRequiredService(); + + public IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService(SimpleGuidGenerator.Instance); + + protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService(); + + protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService(); + + protected IAsyncQueryableExecuter AsyncExecuter => LazyServiceProvider.LazyGetRequiredService(); + + protected ILogger Logger => LazyServiceProvider.LazyGetService(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance); } } diff --git a/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuConfigurationContext.cs b/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuConfigurationContext.cs index e5550d2d09..d5c6ebe468 100644 --- a/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuConfigurationContext.cs +++ b/framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuConfigurationContext.cs @@ -4,35 +4,19 @@ using JetBrains.Annotations; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Localization; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.UI.Navigation { public class MenuConfigurationContext : IMenuConfigurationContext { public IServiceProvider ServiceProvider { get; } - private readonly object _serviceProviderLock = new object(); - private TRef LazyGetRequiredService(Type serviceType, ref TRef reference) - { - if (reference == null) - { - lock (_serviceProviderLock) - { - if (reference == null) - { - reference = (TRef)ServiceProvider.GetRequiredService(serviceType); - } - } - } - - return reference; - } + private readonly IAbpLazyServiceProvider _lazyServiceProvider; - public IAuthorizationService AuthorizationService => LazyGetRequiredService(typeof(IAuthorizationService), ref _authorizationService); - private IAuthorizationService _authorizationService; + public IAuthorizationService AuthorizationService => _lazyServiceProvider.LazyGetRequiredService(); - private IStringLocalizerFactory _stringLocalizerFactory; - public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(typeof(IStringLocalizerFactory),ref _stringLocalizerFactory); + public IStringLocalizerFactory StringLocalizerFactory => _lazyServiceProvider.LazyGetRequiredService(); public ApplicationMenu Menu { get; } @@ -40,6 +24,7 @@ namespace Volo.Abp.UI.Navigation { Menu = menu; ServiceProvider = serviceProvider; + _lazyServiceProvider = ServiceProvider.GetRequiredService(); } public Task IsGrantedAsync(string policyName) @@ -58,7 +43,7 @@ namespace Volo.Abp.UI.Navigation { return StringLocalizerFactory.Create(); } - + [NotNull] public IStringLocalizer GetLocalizer(Type resourceType) {