mirror of https://github.com/abpframework/abp.git
27 changed files with 414 additions and 33 deletions
@ -1,15 +1,55 @@ |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using System; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.ObjectMapping; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc |
|||
{ |
|||
public abstract class AbpViewComponent : ViewComponent |
|||
{ |
|||
public IObjectMapper ObjectMapper { get; set; } |
|||
public IServiceProvider ServiceProvider { get; set; } |
|||
protected readonly object ServiceProviderLock = new object(); |
|||
|
|||
protected AbpViewComponent() |
|||
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; |
|||
} |
|||
|
|||
protected Type ObjectMapperContext { get; set; } |
|||
public 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; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,23 @@ |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using Volo.Abp.AutoMapper; |
|||
using Volo.Abp.ObjectMapping; |
|||
|
|||
namespace Microsoft.Extensions.DependencyInjection |
|||
{ |
|||
public static class AbpAutoMapperServiceCollectionExtensions |
|||
{ |
|||
public static IServiceCollection AddAutoMapperObjectMapper(this IServiceCollection services) |
|||
{ |
|||
return services.Replace( |
|||
ServiceDescriptor.Transient<IAutoObjectMappingProvider, AutoMapperAutoObjectMappingProvider>() |
|||
); |
|||
} |
|||
|
|||
public static IServiceCollection AddAutoMapperObjectMapper<TContext>(this IServiceCollection services) |
|||
{ |
|||
return services.Replace( |
|||
ServiceDescriptor.Transient<IAutoObjectMappingProvider<TContext>, AutoMapperAutoObjectMappingProvider<TContext>>() |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
using AutoMapper; |
|||
using Volo.Abp.AutoMapper; |
|||
|
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public static class AbpAutoMapperObjectMapperExtensions |
|||
{ |
|||
public static IMapper GetMapper(this IObjectMapper objectMapper) |
|||
{ |
|||
return objectMapper.AutoObjectMappingProvider.GetMapper(); |
|||
} |
|||
|
|||
public static IMapper GetMapper(this IAutoObjectMappingProvider autoObjectMappingProvider) |
|||
{ |
|||
if (autoObjectMappingProvider is AutoMapperAutoObjectMappingProvider autoMapperAutoObjectMappingProvider) |
|||
{ |
|||
return autoMapperAutoObjectMappingProvider.MapperAccessor.Mapper; |
|||
} |
|||
|
|||
throw new AbpException($"Given object is not an instance of {typeof(AutoMapperAutoObjectMappingProvider).AssemblyQualifiedName}. The type of the given object it {autoObjectMappingProvider.GetType().AssemblyQualifiedName}"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.EventBus.Distributed |
|||
{ |
|||
public class EtoMappingDictionaryItem |
|||
{ |
|||
public Type EtoType { get; } |
|||
|
|||
public Type ObjectMappingContextType { get; } |
|||
|
|||
public EtoMappingDictionaryItem(Type etoType, Type objectMappingContextType = null) |
|||
{ |
|||
EtoType = etoType; |
|||
ObjectMappingContextType = objectMappingContextType; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.test.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.0</TargetFramework> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.ObjectMapping\Volo.Abp.ObjectMapping.csproj" /> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public abstract class AbpObjectMappingTestBase : AbpIntegratedTest<AbpObjectMappingTestModule> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpObjectMappingModule), |
|||
typeof(AbpTestBaseModule) |
|||
)] |
|||
public class AbpObjectMappingTestModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.AddTest1AutoObjectMappingProvider<MappingContext1>(); |
|||
context.Services.AddTest2AutoObjectMappingProvider<MappingContext2>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public class ContextSpecificMapper_Tests : AbpObjectMappingTestBase |
|||
{ |
|||
[Fact] |
|||
public void Should_Resolve_Correct_ObjectMappper_For_Specific_Context() |
|||
{ |
|||
GetRequiredService<IObjectMapper<MappingContext1>>() |
|||
.ShouldBeOfType(typeof(DefaultObjectMapper<MappingContext1>)); |
|||
|
|||
GetRequiredService<IAutoObjectMappingProvider<MappingContext1>>() |
|||
.ShouldBeOfType(typeof(Test1AutoObjectMappingProvider<MappingContext1>)); |
|||
|
|||
GetRequiredService<IObjectMapper<MappingContext2>>() |
|||
.ShouldBeOfType(typeof(DefaultObjectMapper<MappingContext2>)); |
|||
|
|||
GetRequiredService<IAutoObjectMappingProvider<MappingContext2>>() |
|||
.ShouldBeOfType(typeof(Test2AutoObjectMappingProvider<MappingContext2>)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public class MappingContext1 |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public class MappingContext2 |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public class Test1AutoObjectMappingProvider<TContext> : Test1AutoObjectMappingProvider, IAutoObjectMappingProvider<TContext> |
|||
{ |
|||
|
|||
} |
|||
|
|||
public class Test1AutoObjectMappingProvider : IAutoObjectMappingProvider, ITransientDependency |
|||
{ |
|||
public TDestination Map<TSource, TDestination>(object source) |
|||
{ |
|||
return default; |
|||
} |
|||
|
|||
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination) |
|||
{ |
|||
return default; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public class Test2AutoObjectMappingProvider<TContext> : IAutoObjectMappingProvider<TContext> |
|||
{ |
|||
public TDestination Map<TSource, TDestination>(object source) |
|||
{ |
|||
return default; |
|||
} |
|||
|
|||
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination) |
|||
{ |
|||
return default; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.ObjectMapping |
|||
{ |
|||
public static class TestContextRegistrar |
|||
{ |
|||
public static IServiceCollection AddTest1AutoObjectMappingProvider<TContext>(this IServiceCollection services) |
|||
{ |
|||
return services |
|||
.AddTransient< |
|||
IAutoObjectMappingProvider<TContext>, |
|||
Test1AutoObjectMappingProvider<TContext> |
|||
>(); |
|||
} |
|||
|
|||
public static IServiceCollection AddTest2AutoObjectMappingProvider<TContext>(this IServiceCollection services) |
|||
{ |
|||
return services |
|||
.AddTransient< |
|||
IAutoObjectMappingProvider<TContext>, |
|||
Test2AutoObjectMappingProvider<TContext> |
|||
>(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue