Browse Source

Lazy resolve services in the base classes.

pull/1066/head
maliming 7 years ago
parent
commit
6b5893ff3d
  1. 55
      framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/AspNetCore/Mvc/UI/RazorPages/AbpPageModel.cs
  2. 49
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs
  3. 49
      framework/src/Volo.Abp.Ddd.Application/Volo/Abp/Application/Services/ApplicationService.cs
  4. 28
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Services/DomainService.cs

55
framework/src/Volo.Abp.AspNetCore.Mvc.UI/Volo/Abp/AspNetCore/Mvc/UI/RazorPages/AbpPageModel.cs

@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -21,19 +22,43 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.RazorPages
{
public abstract class AbpPageModel : PageModel
{
public IClock Clock { get; set; }
public IServiceProvider ServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = ServiceProvider.GetRequiredService<TService>();
}
}
}
return reference;
}
public IClock Clock => LazyGetRequiredService(ref _clock);
private IClock _clock;
public AlertList Alerts => AlertManager.Alerts;
public IUnitOfWorkManager UnitOfWorkManager { get; set; }
public IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager);
private IUnitOfWorkManager _unitOfWorkManager;
public IObjectMapper ObjectMapper { get; set; }
public IObjectMapper ObjectMapper => LazyGetRequiredService(ref _objectMapper);
private IObjectMapper _objectMapper;
public IGuidGenerator GuidGenerator { get; set; }
public IGuidGenerator GuidGenerator => LazyGetRequiredService(ref _guidGenerator);
private IGuidGenerator _guidGenerator;
public ILoggerFactory LoggerFactory { get; set; }
public ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory);
private ILoggerFactory _loggerFactory;
public IStringLocalizerFactory StringLocalizerFactory { get; set; }
public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory);
private IStringLocalizerFactory _stringLocalizerFactory;
public IStringLocalizer L
{
@ -56,17 +81,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.RazorPages
protected Type LocalizationResourceType { get; set; }
public ICurrentUser CurrentUser { get; set; }
public ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser);
private ICurrentUser _currentUser;
public ICurrentTenant CurrentTenant { get; set; }
public ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant);
private ICurrentTenant _currentTenant;
public ISettingProvider SettingProvider { get; set; }
public ISettingProvider SettingProvider => LazyGetRequiredService(ref _settingProvider);
private ISettingProvider _settingProvider;
public IModelStateValidator ModelValidator { get; set; }
public IModelStateValidator ModelValidator => LazyGetRequiredService(ref _modelValidator);
private IModelStateValidator _modelValidator;
public IAuthorizationService AuthorizationService { get; set; }
public IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService);
private IAuthorizationService _authorizationService;
public IAlertManager AlertManager { get; set; }
public IAlertManager AlertManager => LazyGetRequiredService(ref _alertManager);
private IAlertManager _alertManager;
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current;

49
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs

@ -2,6 +2,7 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Aspects;
@ -18,27 +19,55 @@ namespace Volo.Abp.AspNetCore.Mvc
{
public abstract class AbpController : Controller, IAvoidDuplicateCrossCuttingConcerns
{
public IUnitOfWorkManager UnitOfWorkManager { get; set; }
public IServiceProvider ServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = ServiceProvider.GetRequiredService<TService>();
}
}
}
return reference;
}
public IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager);
private IUnitOfWorkManager _unitOfWorkManager;
public IObjectMapper ObjectMapper { get; set; }
public IObjectMapper ObjectMapper => LazyGetRequiredService(ref _objectMapper);
private IObjectMapper _objectMapper;
public IGuidGenerator GuidGenerator { get; set; }
public IGuidGenerator GuidGenerator => LazyGetRequiredService(ref _guidGenerator);
private IGuidGenerator _guidGenerator;
public ILoggerFactory LoggerFactory { get; set; }
public ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory);
private ILoggerFactory _loggerFactory;
public ICurrentUser CurrentUser { get; set; }
public ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser);
private ICurrentUser _currentUser;
public ICurrentTenant CurrentTenant { get; set; }
public ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant);
private ICurrentTenant _currentTenant;
public IAuthorizationService AuthorizationService { get; set; }
public IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService);
private IAuthorizationService _authorizationService;
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current;
public IClock Clock { get; set; }
public IClock Clock => LazyGetRequiredService(ref _clock);
private IClock _clock;
public IModelStateValidator ModelValidator { get; set; }
public IModelStateValidator ModelValidator => LazyGetRequiredService(ref _modelValidator);
private IModelStateValidator _modelValidator;
public IFeatureChecker FeatureChecker { get; set; }
public IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker);
private IFeatureChecker _featureChecker;
public List<string> AppliedCrossCuttingConcerns { get; } = new List<string>();

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

@ -1,5 +1,6 @@
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -31,31 +32,59 @@ namespace Volo.Abp.Application.Services
IAuditingEnabled,
ITransientDependency
{
public IServiceProvider ServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = ServiceProvider.GetRequiredService<TService>();
}
}
}
return reference;
}
public static string[] CommonPostfixes { get; set; } = { "AppService", "ApplicationService", "Service" };
public List<string> AppliedCrossCuttingConcerns { get; } = new List<string>();
public IUnitOfWorkManager UnitOfWorkManager { get; set; }
public IUnitOfWorkManager UnitOfWorkManager => LazyGetRequiredService(ref _unitOfWorkManager);
private IUnitOfWorkManager _unitOfWorkManager;
public IObjectMapper ObjectMapper { get; set; }
public IObjectMapper ObjectMapper => LazyGetRequiredService(ref _objectMapper);
private IObjectMapper _objectMapper;
public IGuidGenerator GuidGenerator { get; set; }
public ILoggerFactory LoggerFactory { get; set; }
public ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory);
private ILoggerFactory _loggerFactory;
public ICurrentTenant CurrentTenant { get; set; }
public ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant);
private ICurrentTenant _currentTenant;
public ICurrentUser CurrentUser { get; set; }
public ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser);
private ICurrentUser _currentUser;
public ISettingProvider SettingProvider { get; set; }
public ISettingProvider SettingProvider => LazyGetRequiredService(ref _settingProvider);
private ISettingProvider _settingProvider;
public IClock Clock { get; set; }
public IClock Clock => LazyGetRequiredService(ref _clock);
private IClock _clock;
public IAuthorizationService AuthorizationService { get; set; }
public IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService);
private IAuthorizationService _authorizationService;
public IFeatureChecker FeatureChecker { get; set; }
public IFeatureChecker FeatureChecker => LazyGetRequiredService(ref _featureChecker);
private IFeatureChecker _featureChecker;
public IStringLocalizerFactory StringLocalizerFactory { get; set; }
public IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory);
private IStringLocalizerFactory _stringLocalizerFactory;
public IStringLocalizer L => _localizer ?? (_localizer = StringLocalizerFactory.Create(LocalizationResource));
private IStringLocalizer _localizer;

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

@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Guids;
@ -9,13 +10,34 @@ namespace Volo.Abp.Domain.Services
{
public abstract class DomainService : IDomainService
{
public IClock Clock { get; set; }
public IServiceProvider ServiceProvider { get; set; }
protected readonly object ServiceProviderLock = new object();
protected TService LazyGetRequiredService<TService>(ref TService reference)
{
if (reference == null)
{
lock (ServiceProviderLock)
{
if (reference == null)
{
reference = ServiceProvider.GetRequiredService<TService>();
}
}
}
return reference;
}
public IClock Clock => LazyGetRequiredService(ref _clock);
private IClock _clock;
public IGuidGenerator GuidGenerator { get; set; }
public ILoggerFactory LoggerFactory { get; set; }
public ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory);
private ILoggerFactory _loggerFactory;
public ICurrentTenant CurrentTenant { get; set; }
public ICurrentTenant CurrentTenant => LazyGetRequiredService(ref _currentTenant);
private ICurrentTenant _currentTenant;
protected ILogger Logger => _lazyLogger.Value;
private Lazy<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);

Loading…
Cancel
Save