mirror of https://github.com/abpframework/abp.git
9 changed files with 227 additions and 217 deletions
@ -1,41 +0,0 @@ |
|||
using Volo.Abp.MultiLingualObject; |
|||
using Volo.Abp.Settings; |
|||
|
|||
namespace AutoMapper |
|||
{ |
|||
public static class AbpAutoMapperMultiLingualDtoExtensions |
|||
{ |
|||
public static CreateMultiLingualMapResult<TSource, TTranslation, TDestination> MapMultiLingual<TSource, |
|||
TTranslation, TDestination>( |
|||
this IMappingExpression<TSource, TDestination> mappingExpression, |
|||
Profile profile, |
|||
ISettingProvider provider, |
|||
bool fallbackToParentCultures = false) |
|||
where TDestination : class |
|||
where TSource : IHasMultiLingual<TTranslation> |
|||
where TTranslation : class, IMultiLingualTranslation |
|||
{ |
|||
return new CreateMultiLingualMapResult<TSource, TTranslation, TDestination> |
|||
{ |
|||
TranslationMap = profile.CreateMap<TTranslation, TDestination>(), |
|||
|
|||
EntityMap = mappingExpression.BeforeMap((source, destination, context) => |
|||
{ |
|||
var translation = source.GetMultiLingualTranslation(provider, fallbackToParentCultures); |
|||
|
|||
if (translation != null) |
|||
{ |
|||
context.Mapper.Map(translation, destination); |
|||
} |
|||
}) |
|||
}; |
|||
} |
|||
} |
|||
|
|||
public class CreateMultiLingualMapResult<TMultiLingual, TTranslation, TDestination> |
|||
{ |
|||
public IMappingExpression<TTranslation, TDestination> TranslationMap { get; set; } |
|||
|
|||
public IMappingExpression<TMultiLingual, TDestination> EntityMap { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.MultiLingualObject |
|||
{ |
|||
public interface IMultiLingualObjectManager |
|||
{ |
|||
Task<TTranslation> GetTranslationAsync<TMultiLingual, TTranslation>(TMultiLingual multiLingual, |
|||
bool fallbackToParentCultures = true, string culture = null) |
|||
where TMultiLingual : IHasMultiLingual<TTranslation> |
|||
where TTranslation : class, IMultiLingualTranslation; |
|||
} |
|||
} |
|||
@ -1,107 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp; |
|||
using Volo.Abp.AutoMapper; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.MultiLingualObject.TestObjects; |
|||
using Volo.Abp.Settings; |
|||
using Volo.Abp.Testing; |
|||
using Xunit; |
|||
|
|||
namespace AutoMapper |
|||
{ |
|||
public class AbpAutoMapperMultiLingualDtoExtensions_Tests : AbpIntegratedTest<AutoMapperTestModule> |
|||
{ |
|||
private readonly Volo.Abp.ObjectMapping.IObjectMapper _objectMapper; |
|||
private readonly MultiLingualBook _book; |
|||
|
|||
public AbpAutoMapperMultiLingualDtoExtensions_Tests() |
|||
{ |
|||
_objectMapper = ServiceProvider.GetRequiredService<Volo.Abp.ObjectMapping.IObjectMapper>(); |
|||
|
|||
var id = Guid.NewGuid(); |
|||
_book = new MultiLingualBook(id, 100) |
|||
{ |
|||
Translations = new List<MultiLingualBookTranslation> |
|||
{ |
|||
new MultiLingualBookTranslation |
|||
{ |
|||
CoreId = id, |
|||
Language = "en", |
|||
Name = "C# in Depth" |
|||
}, |
|||
new MultiLingualBookTranslation |
|||
{ |
|||
CoreId = id, |
|||
Language = "zh-Hans", |
|||
Name = "深入理解C#" |
|||
} |
|||
} |
|||
}; |
|||
} |
|||
|
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Map_Current_UI_Culture() |
|||
{ |
|||
using (CultureHelper.Use("zh-Hans")) |
|||
{ |
|||
var bookDto = _objectMapper.Map<MultiLingualBook, MultiLingualBookDto>(_book); |
|||
|
|||
bookDto.Name.ShouldBe("深入理解C#"); |
|||
bookDto.Price.ShouldBe(_book.Price); |
|||
bookDto.Id.ShouldBe(_book.Id); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Map_Fallback_UI_Culture() |
|||
{ |
|||
using (CultureHelper.Use("en-us")) |
|||
{ |
|||
var bookDto = _objectMapper.Map<MultiLingualBook, MultiLingualBookDto>(_book); |
|||
|
|||
bookDto.Name.ShouldBe("C# in Depth"); |
|||
bookDto.Price.ShouldBe(_book.Price); |
|||
bookDto.Id.ShouldBe(_book.Id); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Map_Default_Language() |
|||
{ |
|||
using (CultureHelper.Use("tr")) |
|||
{ |
|||
var bookDto = _objectMapper.Map<MultiLingualBook, MultiLingualBookDto>(_book); |
|||
|
|||
bookDto.Name.ShouldBe("C# in Depth"); |
|||
bookDto.Price.ShouldBe(_book.Price); |
|||
bookDto.Id.ShouldBe(_book.Id); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public class BookProfile : Profile, ITransientDependency |
|||
{ |
|||
public BookProfile() |
|||
{ |
|||
} |
|||
|
|||
public BookProfile(ISettingProvider settingProvider) |
|||
{ |
|||
CreateMap<MultiLingualBook, MultiLingualBookDto>() |
|||
.MapMultiLingual<MultiLingualBook, MultiLingualBookTranslation, MultiLingualBookDto>(this, |
|||
settingProvider, true) |
|||
.TranslationMap |
|||
.ForMember(d => d.Id, m => m.MapFrom(s => s.CoreId)); |
|||
; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,132 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Volo.Abp; |
|||
using Volo.Abp.AutoMapper; |
|||
using Volo.Abp.Localization; |
|||
using Volo.Abp.MultiLingualObject; |
|||
using Volo.Abp.MultiLingualObject.TestObjects; |
|||
using Volo.Abp.Testing; |
|||
using Xunit; |
|||
|
|||
namespace AutoMapper |
|||
{ |
|||
public class AbpAutoMapperMultiLingualDto_Tests : AbpIntegratedTest<AutoMapperTestModule> |
|||
{ |
|||
private readonly Volo.Abp.ObjectMapping.IObjectMapper _objectMapper; |
|||
private readonly IMultiLingualObjectManager _multiLingualObjectManager; |
|||
private readonly MultiLingualBook _book; |
|||
|
|||
public AbpAutoMapperMultiLingualDto_Tests() |
|||
{ |
|||
_multiLingualObjectManager = ServiceProvider.GetRequiredService<IMultiLingualObjectManager>(); |
|||
_objectMapper = ServiceProvider.GetRequiredService<Volo.Abp.ObjectMapping.IObjectMapper>(); |
|||
|
|||
var id = Guid.NewGuid(); |
|||
_book = new MultiLingualBook(id, 100) |
|||
{ |
|||
Translations = new List<MultiLingualBookTranslation>() |
|||
}; |
|||
|
|||
var en = new MultiLingualBookTranslation |
|||
{ |
|||
CoreId = id, |
|||
Language = "en", |
|||
Name = "C# in Depth", |
|||
Core = _book |
|||
}; |
|||
var zh = new MultiLingualBookTranslation |
|||
{ |
|||
CoreId = id, |
|||
Language = "zh-Hans", |
|||
Name = "深入理解C#", |
|||
Core = _book |
|||
}; |
|||
|
|||
_book.Translations.Add(en); |
|||
_book.Translations.Add(zh); |
|||
} |
|||
|
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Map_Current_UI_Culture() |
|||
{ |
|||
using (CultureHelper.Use("zh-Hans")) |
|||
{ |
|||
var translation = |
|||
await _multiLingualObjectManager |
|||
.GetTranslationAsync<MultiLingualBook, MultiLingualBookTranslation>(_book); |
|||
|
|||
var bookDto = _objectMapper.Map<MultiLingualBookTranslation, MultiLingualBookDto>(translation); |
|||
|
|||
bookDto.Name.ShouldBe("深入理解C#"); |
|||
bookDto.Price.ShouldBe(_book.Price); |
|||
bookDto.Id.ShouldBe(_book.Id); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Map_Fallback_UI_Culture() |
|||
{ |
|||
using (CultureHelper.Use("en-us")) |
|||
{ |
|||
var translation = |
|||
await _multiLingualObjectManager |
|||
.GetTranslationAsync<MultiLingualBook, MultiLingualBookTranslation>(_book); |
|||
|
|||
var bookDto = _objectMapper.Map<MultiLingualBookTranslation, MultiLingualBookDto>(translation); |
|||
|
|||
bookDto.Name.ShouldBe("C# in Depth"); |
|||
bookDto.Price.ShouldBe(_book.Price); |
|||
bookDto.Id.ShouldBe(_book.Id); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Map_Default_Language() |
|||
{ |
|||
using (CultureHelper.Use("tr")) |
|||
{ |
|||
var translation = |
|||
await _multiLingualObjectManager |
|||
.GetTranslationAsync<MultiLingualBook, MultiLingualBookTranslation>(_book); |
|||
|
|||
var bookDto = _objectMapper.Map<MultiLingualBookTranslation, MultiLingualBookDto>(translation); |
|||
|
|||
bookDto.Name.ShouldBe("C# in Depth"); |
|||
bookDto.Price.ShouldBe(_book.Price); |
|||
bookDto.Id.ShouldBe(_book.Id); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_Map_Specified_Language() |
|||
{ |
|||
using (CultureHelper.Use("zh-Hans")) |
|||
{ |
|||
var translation = await _multiLingualObjectManager.GetTranslationAsync<MultiLingualBook, MultiLingualBookTranslation>(_book, culture:"en"); |
|||
var bookDto = _objectMapper.Map<MultiLingualBookTranslation,MultiLingualBookDto>(translation); |
|||
|
|||
bookDto.Name.ShouldBe("C# in Depth"); |
|||
bookDto.Price.ShouldBe(_book.Price); |
|||
bookDto.Id.ShouldBe(_book.Id); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public class BookProfile : Profile |
|||
{ |
|||
public BookProfile() |
|||
{ |
|||
CreateMap<MultiLingualBookTranslation, MultiLingualBookDto>() |
|||
.ForMember(d => d.Price, m => m.MapFrom(s => s.Core.Price)) |
|||
.ForMember(d => d.Id, m => m.MapFrom(s => s.CoreId)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue