From e15f566068f25db427711ebf3db00ce341985327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 27 Oct 2020 15:14:05 +0300 Subject: [PATCH] Complete the AbpComponentBase --- .../Volo.Abp.AspNetCore.Components.csproj | 2 + .../AbpAspNetCoreComponentsModule.cs | 6 +- .../AspNetCore/Components/AbpComponentBase.cs | 63 ++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Components/Volo.Abp.AspNetCore.Components.csproj b/framework/src/Volo.Abp.AspNetCore.Components/Volo.Abp.AspNetCore.Components.csproj index 5ce18bc7f0..973f49118c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components/Volo.Abp.AspNetCore.Components.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Components/Volo.Abp.AspNetCore.Components.csproj @@ -15,7 +15,9 @@ + + diff --git a/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpAspNetCoreComponentsModule.cs b/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpAspNetCoreComponentsModule.cs index e125ee5f8d..08c7ed8e9f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpAspNetCoreComponentsModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpAspNetCoreComponentsModule.cs @@ -2,13 +2,17 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Components.DependencyInjection; using Volo.Abp.DynamicProxy; +using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.ObjectMapping; +using Volo.Abp.Security; namespace Volo.Abp.AspNetCore.Components { [DependsOn( - typeof(AbpObjectMappingModule) + typeof(AbpObjectMappingModule), + typeof(AbpSecurityModule), + typeof(AbpLocalizationModule) )] public class AbpAspNetCoreComponentsModule : AbpModule { diff --git a/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpComponentBase.cs b/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpComponentBase.cs index 94b61cdc66..ebc56dd4a2 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpComponentBase.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components/Volo/Abp/AspNetCore/Components/AbpComponentBase.cs @@ -1,13 +1,57 @@ using System; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Volo.Abp.Localization; using Volo.Abp.ObjectMapping; +using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Components { public class AbpComponentBase : OwningComponentBase { - //TODO: IStringLocalizer, CurrentUser, Logger, AuthorizationService + protected IStringLocalizerFactory StringLocalizerFactory => LazyGetRequiredService(ref _stringLocalizerFactory); + private IStringLocalizerFactory _stringLocalizerFactory; + + protected IStringLocalizer L + { + get + { + if (_localizer == null) + { + _localizer = CreateLocalizer(); + } + + return _localizer; + } + } + private IStringLocalizer _localizer; + + protected Type LocalizationResource + { + get => _localizationResource; + set + { + _localizationResource = value; + _localizer = null; + } + } + private Type _localizationResource = typeof(DefaultResource); + + protected ILogger Logger => _lazyLogger.Value; + private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); + + protected ILoggerFactory LoggerFactory => LazyGetRequiredService(ref _loggerFactory); + private ILoggerFactory _loggerFactory; + + protected IAuthorizationService AuthorizationService => LazyGetRequiredService(ref _authorizationService); + private IAuthorizationService _authorizationService; + + protected ICurrentUser CurrentUser => LazyGetRequiredService(ref _currentUser); + private ICurrentUser _currentUser; protected IObjectMapper ObjectMapper { @@ -31,6 +75,7 @@ namespace Volo.Abp.AspNetCore.Components } private IObjectMapper _objectMapper; + protected Type ObjectMapperContext { get; set; } protected TService LazyGetRequiredService(ref TService reference) => LazyGetRequiredService(typeof(TService), ref reference); @@ -44,5 +89,21 @@ namespace Volo.Abp.AspNetCore.Components return reference; } + + protected virtual IStringLocalizer CreateLocalizer() + { + if (LocalizationResource != null) + { + return StringLocalizerFactory.Create(LocalizationResource); + } + + var localizer = StringLocalizerFactory.CreateDefaultOrNull(); + if (localizer == null) + { + throw new AbpException($"Set {nameof(LocalizationResource)} or define the default localization resource type (by configuring the {nameof(AbpLocalizationOptions)}.{nameof(AbpLocalizationOptions.DefaultResourceType)}) to be able to use the {nameof(L)} object!"); + } + + return localizer; + } } }