Browse Source

Use IAbpLazyServiceProvider instead of LazyGetRequiredService method.

pull/6815/head
maliming 6 years ago
parent
commit
a7739c2ee3
  1. 25
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs
  2. 30
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationContext.cs
  3. 29
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Toolbars/ToolbarConfigurationContext.cs
  4. 87
      framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/AspNetCore/Mvc/UI/RazorPages/AbpPageModel.cs
  5. 86
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs
  6. 47
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs
  7. 87
      framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/AbpHub.cs
  8. 29
      framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs
  9. 89
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs
  10. 54
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs
  11. 27
      framework/src/Volo.Abp.UI.Navigation/Volo/Abp/Ui/Navigation/MenuConfigurationContext.cs

25
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly.Theming/Toolbars/ToolbarConfigurationContext.cs

@ -4,35 +4,19 @@ using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars
{ {
public class ToolbarConfigurationContext : IToolbarConfigurationContext public class ToolbarConfigurationContext : IToolbarConfigurationContext
{ {
public IServiceProvider ServiceProvider { get; } public IServiceProvider ServiceProvider { get; }
private readonly object _serviceProviderLock = new object();
private TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference) private readonly IAbpLazyServiceProvider _lazyServiceProvider;
{
if (reference == null)
{
lock (_serviceProviderLock)
{
if (reference == null)
{
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
}
}
}
return reference;
}
public IAuthorizationService AuthorizationService => LazyGetRequiredService(typeof(IAuthorizationService), ref _authorizationService); public IAuthorizationService AuthorizationService => _lazyServiceProvider.LazyGetRequiredService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
private IStringLocalizerFactory _stringLocalizerFactory; public IStringLocalizerFactory StringLocalizerFactory => _lazyServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();
public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(typeof(IStringLocalizerFactory),ref _stringLocalizerFactory);
public Toolbar Toolbar { get; } public Toolbar Toolbar { get; }
@ -40,6 +24,7 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly.Theming.Toolbars
{ {
Toolbar = toolbar; Toolbar = toolbar;
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
_lazyServiceProvider = ServiceProvider.GetRequiredService<IAbpLazyServiceProvider>();
} }
public Task<bool> IsGrantedAsync(string policyName) public Task<bool> IsGrantedAsync(string policyName)

30
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.DependencyInjection;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization; using Volo.Abp.Localization;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
@ -13,35 +14,18 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public IFileProvider FileProvider { get; } public IFileProvider FileProvider { get; }
public IServiceProvider ServiceProvider { get; }
private readonly IAbpLazyServiceProvider _lazyServiceProvider;
public BundleConfigurationContext(IServiceProvider serviceProvider, IFileProvider fileProvider) public BundleConfigurationContext(IServiceProvider serviceProvider, IFileProvider fileProvider)
{ {
Files = new List<string>(); Files = new List<string>();
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
_lazyServiceProvider = ServiceProvider.GetRequiredService<IAbpLazyServiceProvider>();
FileProvider = fileProvider; FileProvider = fileProvider;
} }
public IServiceProvider ServiceProvider { get; } public AbpLocalizationOptions LocalizationOptions => _lazyServiceProvider.LazyGetRequiredService<IOptions<AbpLocalizationOptions>>().Value;
private readonly object _serviceProviderLock = new object();
private TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference)
{
if (reference == null)
{
lock (_serviceProviderLock)
{
if (reference == null)
{
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
}
}
}
return reference;
}
private IOptions<AbpLocalizationOptions> _abpLocalizationOptions;
public AbpLocalizationOptions LocalizationOptions =>
LazyGetRequiredService(typeof(IOptions<AbpLocalizationOptions>), ref _abpLocalizationOptions).Value;
} }
} }

29
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.DependencyInjection;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Theming; using Volo.Abp.AspNetCore.Mvc.UI.Theming;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars
{ {
public class ToolbarConfigurationContext : IToolbarConfigurationContext public class ToolbarConfigurationContext : IToolbarConfigurationContext
{ {
public IServiceProvider ServiceProvider { get; } public IServiceProvider ServiceProvider { get; }
private readonly object _serviceProviderLock = new object();
private TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference) private readonly IAbpLazyServiceProvider _lazyServiceProvider;
{
if (reference == null)
{
lock (_serviceProviderLock)
{
if (reference == null)
{
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
}
}
}
return reference;
}
public IAuthorizationService AuthorizationService => LazyGetRequiredService(typeof(IAuthorizationService), ref _authorizationService); public IAuthorizationService AuthorizationService => _lazyServiceProvider.LazyGetRequiredService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
private IStringLocalizerFactory _stringLocalizerFactory; public IStringLocalizerFactory StringLocalizerFactory => _lazyServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();
public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(typeof(IStringLocalizerFactory),ref _stringLocalizerFactory);
public ITheme Theme { get; } public ITheme Theme { get; }
@ -44,8 +28,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars
Theme = currentTheme; Theme = currentTheme;
Toolbar = toolbar; Toolbar = toolbar;
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
_lazyServiceProvider = ServiceProvider.GetRequiredService<IAbpLazyServiceProvider>();
} }
public Task<bool> IsGrantedAsync(string policyName) public Task<bool> IsGrantedAsync(string policyName)
{ {
return AuthorizationService.IsGrantedAsync(policyName); return AuthorizationService.IsGrantedAsync(policyName);
@ -62,7 +47,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Toolbars
{ {
return StringLocalizerFactory.Create<T>(); return StringLocalizerFactory.Create<T>();
} }
[NotNull] [NotNull]
public IStringLocalizer GetLocalizer(Type resourceType) public IStringLocalizer GetLocalizer(Type resourceType)
{ {

87
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 System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc.UI.Alerts; using Volo.Abp.AspNetCore.Mvc.UI.Alerts;
using Volo.Abp.AspNetCore.Mvc.Validation; using Volo.Abp.AspNetCore.Mvc.Validation;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -24,67 +25,27 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.RazorPages
{ {
public abstract class AbpPageModel : PageModel public abstract class AbpPageModel : PageModel
{ {
public IServiceProvider ServiceProvider { get; set; } public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
=> LazyGetRequiredService(typeof(TService), ref reference);
protected TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference) public IServiceProvider ServiceProvider { get; set; }
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
}
}
}
return reference;
}
protected IClock Clock => LazyGetRequiredService(ref _clock); protected IClock Clock => LazyServiceProvider.LazyGetRequiredService<IClock>();
private IClock _clock;
protected AlertList Alerts => AlertManager.Alerts; protected AlertList Alerts => AlertManager.Alerts;
protected IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager); protected IUnitOfWorkManager UnitOfWorkManager => LazyServiceProvider.LazyGetRequiredService<IUnitOfWorkManager>();
private IUnitOfWorkManager _unitOfWorkManager;
protected Type ObjectMapperContext { get; set; } protected Type ObjectMapperContext { get; set; }
protected IObjectMapper ObjectMapper protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService<IObjectMapper>(provider =>
{ ObjectMapperContext == null
get ? provider.GetRequiredService<IObjectMapper>()
{ : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext)));
if (_objectMapper != null)
{
return _objectMapper;
}
if (ObjectMapperContext == null)
{
return LazyGetRequiredService(ref _objectMapper);
}
return LazyGetRequiredService(
typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext),
ref _objectMapper
);
}
}
private IObjectMapper _objectMapper;
protected IGuidGenerator GuidGenerator => LazyGetRequiredService(ref _guidGenerator); protected IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService<IGuidGenerator>(SimpleGuidGenerator.Instance);
private IGuidGenerator _guidGenerator;
protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService<ILoggerFactory>();
private ILoggerFactory _loggerFactory;
protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();
private IStringLocalizerFactory _stringLocalizerFactory;
protected IStringLocalizer L protected IStringLocalizer L
{ {
@ -103,31 +64,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.RazorPages
protected Type LocalizationResourceType { get; set; } protected Type LocalizationResourceType { get; set; }
protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetRequiredService<ICurrentUser>();
private ICurrentUser _currentUser;
protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
private ICurrentTenant _currentTenant;
protected ISettingProvider SettingProvider => LazyGetRequiredService(ref _settingProvider); protected ISettingProvider SettingProvider => LazyServiceProvider.LazyGetRequiredService<ISettingProvider>();
private ISettingProvider _settingProvider;
protected IModelStateValidator ModelValidator => LazyGetRequiredService(ref _modelValidator); protected IModelStateValidator ModelValidator => LazyServiceProvider.LazyGetRequiredService<IModelStateValidator>();
private IModelStateValidator _modelValidator;
protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetRequiredService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
protected IAlertManager AlertManager => LazyGetRequiredService(ref _alertManager); protected IAlertManager AlertManager => LazyServiceProvider.LazyGetRequiredService<IAlertManager>();
private IAlertManager _alertManager;
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current;
protected ILogger Logger => _lazyLogger.Value; protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance);
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected IAppUrlProvider AppUrlProvider => LazyGetRequiredService(ref _appUrlProvider); protected IAppUrlProvider AppUrlProvider => LazyServiceProvider.LazyGetRequiredService<IAppUrlProvider>();
private IAppUrlProvider _appUrlProvider;
protected virtual NoContentResult NoContent() //TODO: Is that true to return empty result like that? protected virtual NoContentResult NoContent() //TODO: Is that true to return empty result like that?
{ {

86
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 Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Aspects; using Volo.Abp.Aspects;
using Volo.Abp.AspNetCore.Mvc.Validation; using Volo.Abp.AspNetCore.Mvc.Validation;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Features; using Volo.Abp.Features;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.Localization; using Volo.Abp.Localization;
@ -22,85 +23,41 @@ namespace Volo.Abp.AspNetCore.Mvc
{ {
public abstract class AbpController : Controller, IAvoidDuplicateCrossCuttingConcerns public abstract class AbpController : Controller, IAvoidDuplicateCrossCuttingConcerns
{ {
public IServiceProvider ServiceProvider { get; set; } public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
=> LazyGetRequiredService(typeof(TService), ref reference);
protected TRef LazyGetRequiredService<TRef>(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 IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager); protected IUnitOfWorkManager UnitOfWorkManager => LazyServiceProvider.LazyGetRequiredService<IUnitOfWorkManager>();
private IUnitOfWorkManager _unitOfWorkManager;
protected Type ObjectMapperContext { get; set; } protected Type ObjectMapperContext { get; set; }
protected IObjectMapper ObjectMapper protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService<IObjectMapper>(provider =>
{ ObjectMapperContext == null
get ? provider.GetRequiredService<IObjectMapper>()
{ : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext)));
if (_objectMapper != null)
{
return _objectMapper;
}
if (ObjectMapperContext == null) protected IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService<IGuidGenerator>(SimpleGuidGenerator.Instance);
{
return LazyGetRequiredService(ref _objectMapper);
}
return LazyGetRequiredService( protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService<ILoggerFactory>();
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 ILogger Logger => _lazyLogger.Value; protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance);
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetRequiredService<ICurrentUser>();
private ICurrentUser _currentUser;
protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
private ICurrentTenant _currentTenant;
protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetRequiredService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current;
protected IClock Clock => LazyGetRequiredService(ref _clock); protected IClock Clock => LazyServiceProvider.LazyGetRequiredService<IClock>();
private IClock _clock;
protected IModelStateValidator ModelValidator => LazyGetRequiredService(ref _modelValidator); protected IModelStateValidator ModelValidator => LazyServiceProvider.LazyGetRequiredService<IModelStateValidator>();
private IModelStateValidator _modelValidator;
protected IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker); protected IFeatureChecker FeatureChecker => LazyServiceProvider.LazyGetRequiredService<IFeatureChecker>();
private IFeatureChecker _featureChecker;
protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); protected IAppUrlProvider AppUrlProvider => LazyServiceProvider.LazyGetRequiredService<IAppUrlProvider>();
private IStringLocalizerFactory _stringLocalizerFactory;
protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();
protected IStringLocalizer L protected IStringLocalizer L
{ {
@ -116,9 +73,6 @@ namespace Volo.Abp.AspNetCore.Mvc
} }
private IStringLocalizer _localizer; private IStringLocalizer _localizer;
protected IAppUrlProvider AppUrlProvider => LazyGetRequiredService(ref _appUrlProvider);
private IAppUrlProvider _appUrlProvider;
protected Type LocalizationResource protected Type LocalizationResource
{ {
get => _localizationResource; get => _localizationResource;

47
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs

@ -1,55 +1,22 @@
using System; using System;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ObjectMapping; using Volo.Abp.ObjectMapping;
namespace Volo.Abp.AspNetCore.Mvc namespace Volo.Abp.AspNetCore.Mvc
{ {
public abstract class AbpViewComponent : ViewComponent public abstract class AbpViewComponent : ViewComponent
{ {
public IServiceProvider ServiceProvider { get; set; } public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
=> LazyGetRequiredService(typeof(TService), ref reference);
protected TRef LazyGetRequiredService<TRef>(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 Type ObjectMapperContext { 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( protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService<IObjectMapper>(provider =>
typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext), ObjectMapperContext == null
ref _objectMapper ? provider.GetRequiredService<IObjectMapper>()
); : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext)));
}
}
private IObjectMapper _objectMapper;
} }
} }

87
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.Localization;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization; using Volo.Abp.Localization;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing; using Volo.Abp.Timing;
@ -14,48 +15,23 @@ namespace Volo.Abp.AspNetCore.SignalR
{ {
public abstract class AbpHub : Hub public abstract class AbpHub : Hub
{ {
public IServiceProvider ServiceProvider { get; set; } public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
=> LazyGetRequiredService(typeof(TService), ref reference);
protected TRef LazyGetRequiredService<TRef>(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); protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetService<ILoggerFactory>();
private ILoggerFactory _loggerFactory;
protected ILogger Logger => _lazyLogger.Value; protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance);
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetService<ICurrentUser>();
private ICurrentUser _currentUser;
protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetService<ICurrentTenant>();
private ICurrentTenant _currentTenant;
protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
protected IClock Clock => LazyGetRequiredService(ref _clock); protected IClock Clock => LazyServiceProvider.LazyGetService<IClock>();
private IClock _clock;
protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetService<IStringLocalizerFactory>();
private IStringLocalizerFactory _stringLocalizerFactory;
protected IStringLocalizer L protected IStringLocalizer L
{ {
@ -102,48 +78,23 @@ namespace Volo.Abp.AspNetCore.SignalR
public abstract class AbpHub<T> : Hub<T> public abstract class AbpHub<T> : Hub<T>
where T : class where T : class
{ {
public IServiceProvider ServiceProvider { get; set; } public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
=> LazyGetRequiredService(typeof(TService), ref reference);
protected TRef LazyGetRequiredService<TRef>(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); protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetService<ILoggerFactory>();
private ILoggerFactory _loggerFactory;
protected ILogger Logger => _lazyLogger.Value; protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance);
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetService<ICurrentUser>();
private ICurrentUser _currentUser;
protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetService<ICurrentTenant>();
private ICurrentTenant _currentTenant;
protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
protected IClock Clock => LazyGetRequiredService(ref _clock); protected IClock Clock => LazyServiceProvider.LazyGetService<IClock>();
private IClock _clock;
protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetService<IStringLocalizerFactory>();
private IStringLocalizerFactory _stringLocalizerFactory;
protected IStringLocalizer L protected IStringLocalizer L
{ {

29
framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerBase.cs

@ -1,9 +1,9 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.BackgroundWorkers namespace Volo.Abp.BackgroundWorkers
{ {
@ -13,33 +13,14 @@ namespace Volo.Abp.BackgroundWorkers
public abstract class BackgroundWorkerBase : IBackgroundWorker public abstract class BackgroundWorkerBase : IBackgroundWorker
{ {
//TODO: Add UOW, Localization and other useful properties..? //TODO: Add UOW, Localization and other useful properties..?
public IServiceProvider ServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference) public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
=> LazyGetRequiredService(typeof(TService), ref reference);
protected TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference) public IServiceProvider ServiceProvider { get; set; }
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
}
}
}
return reference;
}
protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService<ILoggerFactory>();
private ILoggerFactory _loggerFactory;
protected ILogger Logger => _lazyLogger.Value; protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance);
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
public virtual Task StartAsync(CancellationToken cancellationToken = default) public virtual Task StartAsync(CancellationToken cancellationToken = default)
{ {

89
framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs

@ -33,86 +33,41 @@ namespace Volo.Abp.Application.Services
IAuditingEnabled, IAuditingEnabled,
ITransientDependency ITransientDependency
{ {
public IServiceProvider ServiceProvider { get; set; } public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
=> LazyGetRequiredService(typeof(TService), ref reference);
protected TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference) public IServiceProvider ServiceProvider { get; set; }
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
}
}
}
return reference;
}
public static string[] CommonPostfixes { get; set; } = { "AppService", "ApplicationService", "Service" }; public static string[] CommonPostfixes { get; set; } = { "AppService", "ApplicationService", "Service" };
public List<string> AppliedCrossCuttingConcerns { get; } = new List<string>(); public List<string> AppliedCrossCuttingConcerns { get; } = new List<string>();
protected IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager); protected IUnitOfWorkManager UnitOfWorkManager => LazyServiceProvider.LazyGetRequiredService<IUnitOfWorkManager>();
private IUnitOfWorkManager _unitOfWorkManager;
protected IAsyncQueryableExecuter AsyncExecuter => LazyGetRequiredService(ref _asyncExecuter);
private IAsyncQueryableExecuter _asyncExecuter;
protected Type ObjectMapperContext { get; set; } protected IAsyncQueryableExecuter AsyncExecuter => LazyServiceProvider.LazyGetRequiredService<IAsyncQueryableExecuter>();
protected IObjectMapper ObjectMapper
{
get
{
if (_objectMapper != null)
{
return _objectMapper;
}
if (ObjectMapperContext == null)
{
return LazyGetRequiredService(ref _objectMapper);
}
return LazyGetRequiredService( protected Type ObjectMapperContext { get; set; }
typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext), protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService<IObjectMapper>(provider =>
ref _objectMapper ObjectMapperContext == null
); ? provider.GetRequiredService<IObjectMapper>()
} : (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext)));
}
private IObjectMapper _objectMapper;
public IGuidGenerator GuidGenerator { get; set; } public IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService<IGuidGenerator>(SimpleGuidGenerator.Instance);
protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService<ILoggerFactory>();
private ILoggerFactory _loggerFactory;
protected ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant); protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
private ICurrentTenant _currentTenant;
protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); protected ICurrentUser CurrentUser => LazyServiceProvider.LazyGetRequiredService<ICurrentUser>();
private ICurrentUser _currentUser;
protected ISettingProvider SettingProvider => LazyGetRequiredService(ref _settingProvider); protected ISettingProvider SettingProvider => LazyServiceProvider.LazyGetRequiredService<ISettingProvider>();
private ISettingProvider _settingProvider;
protected IClock Clock => LazyGetRequiredService(ref _clock); protected IClock Clock => LazyServiceProvider.LazyGetRequiredService<IClock>();
private IClock _clock;
protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); protected IAuthorizationService AuthorizationService => LazyServiceProvider.LazyGetRequiredService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
protected IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker); protected IFeatureChecker FeatureChecker => LazyServiceProvider.LazyGetRequiredService<IFeatureChecker>();
private IFeatureChecker _featureChecker;
protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); protected IStringLocalizerFactory StringLocalizerFactory => LazyServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();
private IStringLocalizerFactory _stringLocalizerFactory;
protected IStringLocalizer L protected IStringLocalizer L
{ {
@ -141,13 +96,7 @@ namespace Volo.Abp.Application.Services
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current; protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current;
protected ILogger Logger => _lazyLogger.Value; protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance);
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected ApplicationService()
{
GuidGenerator = SimpleGuidGenerator.Instance;
}
/// <summary> /// <summary>
/// Checks for given <paramref name="policyName"/>. /// Checks for given <paramref name="policyName"/>.

54
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs

@ -1,7 +1,7 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.Linq; using Volo.Abp.Linq;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -11,44 +11,20 @@ namespace Volo.Abp.Domain.Services
{ {
public abstract class DomainService : IDomainService public abstract class DomainService : IDomainService
{ {
public IAbpLazyServiceProvider LazyServiceProvider { get; set; }
public IServiceProvider ServiceProvider { get; set; } public IServiceProvider ServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference) protected IClock Clock => LazyServiceProvider.LazyGetRequiredService<IClock>();
{
if (reference == null) public IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetService<IGuidGenerator>(SimpleGuidGenerator.Instance);
{
lock (ServiceProviderLock) protected ILoggerFactory LoggerFactory => LazyServiceProvider.LazyGetRequiredService<ILoggerFactory>();
{
if (reference == null) protected ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
{
reference = ServiceProvider.GetRequiredService<TService>(); protected IAsyncQueryableExecuter AsyncExecuter => LazyServiceProvider.LazyGetRequiredService<IAsyncQueryableExecuter>();
}
} protected ILogger Logger => LazyServiceProvider.LazyGetService<ILogger>(provider => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance);
}
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<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected DomainService()
{
GuidGenerator = SimpleGuidGenerator.Instance;
}
} }
} }

27
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.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.UI.Navigation namespace Volo.Abp.UI.Navigation
{ {
public class MenuConfigurationContext : IMenuConfigurationContext public class MenuConfigurationContext : IMenuConfigurationContext
{ {
public IServiceProvider ServiceProvider { get; } public IServiceProvider ServiceProvider { get; }
private readonly object _serviceProviderLock = new object();
private TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference) private readonly IAbpLazyServiceProvider _lazyServiceProvider;
{
if (reference == null)
{
lock (_serviceProviderLock)
{
if (reference == null)
{
reference = (TRef)ServiceProvider.GetRequiredService(serviceType);
}
}
}
return reference;
}
public IAuthorizationService AuthorizationService => LazyGetRequiredService(typeof(IAuthorizationService), ref _authorizationService); public IAuthorizationService AuthorizationService => _lazyServiceProvider.LazyGetRequiredService<IAuthorizationService>();
private IAuthorizationService _authorizationService;
private IStringLocalizerFactory _stringLocalizerFactory; public IStringLocalizerFactory StringLocalizerFactory => _lazyServiceProvider.LazyGetRequiredService<IStringLocalizerFactory>();
public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(typeof(IStringLocalizerFactory),ref _stringLocalizerFactory);
public ApplicationMenu Menu { get; } public ApplicationMenu Menu { get; }
@ -40,6 +24,7 @@ namespace Volo.Abp.UI.Navigation
{ {
Menu = menu; Menu = menu;
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
_lazyServiceProvider = ServiceProvider.GetRequiredService<IAbpLazyServiceProvider>();
} }
public Task<bool> IsGrantedAsync(string policyName) public Task<bool> IsGrantedAsync(string policyName)
@ -58,7 +43,7 @@ namespace Volo.Abp.UI.Navigation
{ {
return StringLocalizerFactory.Create<T>(); return StringLocalizerFactory.Create<T>();
} }
[NotNull] [NotNull]
public IStringLocalizer GetLocalizer(Type resourceType) public IStringLocalizer GetLocalizer(Type resourceType)
{ {

Loading…
Cancel
Save