mirror of https://github.com/abpframework/abp.git
11 changed files with 130 additions and 460 deletions
@ -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<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); |
|||
} |
|||
} |
|||
} |
|||
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<IObjectMapper>(provider => |
|||
ObjectMapperContext == null |
|||
? provider.GetRequiredService<IObjectMapper>() |
|||
: (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext))); |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue