@ -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 ( _l ocalizer = = null )
{
_l ocalizer = CreateLocalizer ( ) ;
}
return _l ocalizer ;
}
}
private IStringLocalizer _l ocalizer ;
protected Type LocalizationResource
{
get = > _l ocalizationResource ;
set
{
_l ocalizationResource = value ;
_l ocalizer = null ;
}
}
private Type _l ocalizationResource = typeof ( DefaultResource ) ;
protected ILogger Logger = > _l azyLogger . Value ;
private Lazy < ILogger > _l azyLogger = > new Lazy < ILogger > ( ( ) = > LoggerFactory ? . CreateLogger ( GetType ( ) . FullName ) ? ? NullLogger . Instance , true ) ;
protected ILoggerFactory LoggerFactory = > LazyGetRequiredService ( ref _l oggerFactory ) ;
private ILoggerFactory _l oggerFactory ;
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 < TService > ( 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 ;
}
}
}