Skip to content

Commit 4031593

Browse files
committed
fix: 修复 AutoMapper 组件动态构建映射配置丢失原有映射器问题
1 parent 0ae04f7 commit 4031593

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

framework/src/Bing.AutoMapper/Bing/AutoMapper/AutoMapperObjectMapper.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Linq.Expressions;
45
using AutoMapper;
56
using AutoMapper.QueryableExtensions;
7+
using Bing.ObjectMapping;
68

79
namespace Bing.AutoMapper
810
{
@@ -27,14 +29,22 @@ public class AutoMapperObjectMapper : Bing.ObjectMapping.IObjectMapper
2729
/// </summary>
2830
private IMapper _mapper;
2931

32+
/// <summary>
33+
/// 映射器
34+
/// </summary>
35+
private readonly IReadOnlyCollection<IObjectMapperProfile> _profiles;
36+
3037
/// <summary>
3138
/// 初始化一个<see cref="AutoMapperObjectMapper"/>类型的实例
3239
/// </summary>
3340
/// <param name="configuration">AutoMapper配置提供程序</param>
34-
public AutoMapperObjectMapper(IConfigurationProvider configuration)
41+
/// <param name="profiles">映射器</param>
42+
public AutoMapperObjectMapper(IConfigurationProvider configuration, IReadOnlyCollection<IObjectMapperProfile> profiles)
3543
{
3644
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
45+
_profiles = profiles;
3746
_mapper = _configuration.CreateMapper();
47+
3848
}
3949

4050
#region Map(将源对象映射到目标对象)
@@ -127,11 +137,13 @@ private TDestination GetResult<TDestination>(Type sourceType, Type destinationTy
127137
private void ConfigMap(Type sourceType, Type destinationType)
128138
{
129139
var maps = _configuration.GetAllTypeMaps();
130-
_configuration=new MapperConfiguration(t =>
140+
_configuration = new MapperConfiguration(t =>
131141
{
132142
t.CreateMap(sourceType, destinationType);
133143
foreach (var map in maps)
134144
t.CreateMap(map.SourceType, map.DestinationType);
145+
foreach (var profile in _profiles)
146+
t.AddProfile(profile as Profile);
135147
});
136148
_mapper = _configuration.CreateMapper();
137149
}

framework/src/Bing.AutoMapper/Bing/AutoMapper/Extensions.Service.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void AddAutoMapper(this IServiceCollection services)
3636
cfg.AddProfile(instance as Profile);
3737
}
3838
});
39-
var mapper = new AutoMapperObjectMapper(configuration);
39+
var mapper = new AutoMapperObjectMapper(configuration, instances);
4040
ObjectMapperExtensions.SetMapper(mapper);
4141
services.TryAddSingleton<IObjectMapper>(mapper);
4242
}

framework/tests/Bing.AutoMapper.Tests/MapTest.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public MapTest()
3434
cfg.AddProfile(instance as Profile);
3535
}
3636
});
37-
var mapper = new AutoMapperObjectMapper(configuration);
37+
var mapper = new AutoMapperObjectMapper(configuration, instances);
3838
ObjectMapperExtensions.SetMapper(mapper);
3939
}
4040

@@ -224,7 +224,28 @@ public void Test_MapTo_CustomProfile()
224224
{
225225
var source = new AutoMapperSourceSample { SourceStringValue = "666" };
226226
var target = source.MapTo<AutoMapperTargetSample>();
227-
Assert.Equal("666", target.TargetSampleValue);
227+
Assert.Equal("666-001", target.TargetSampleValue);
228+
}
229+
230+
/// <summary>
231+
/// 测试自定义配置及非自定义规则映射
232+
/// </summary>
233+
[Fact]
234+
public void Test_MapTo_CustomProfile_WithMoreRule()
235+
{
236+
var source = new AutoMapperSourceSample { SourceStringValue = "666" };
237+
var target = source.MapTo<AutoMapperTargetSample>();
238+
Assert.Equal("666-001", target.TargetSampleValue);
239+
240+
var sample = new Sample();
241+
var sample2 = new Sample2() { StringValue = "a" };
242+
sample2.MapTo(sample);
243+
244+
Assert.Equal("a", sample.StringValue);
245+
246+
source = new AutoMapperSourceSample { SourceStringValue = "666" };
247+
target = source.MapTo<AutoMapperTargetSample>();
248+
Assert.Equal("666-001", target.TargetSampleValue);
228249
}
229250

230251
[Fact]

framework/tests/Bing.AutoMapper.Tests/TestMapperConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class TestMapperConfiguration : Profile, IObjectMapperProfile
1515
public void CreateMap()
1616
{
1717
CreateMap<AutoMapperSourceSample, AutoMapperTargetSample>()
18-
.ForMember(x => x.TargetSampleValue, x => x.MapFrom(p => p.SourceStringValue));
18+
.ForMember(x => x.TargetSampleValue, x => x.MapFrom(p => p.SourceStringValue + "-001"));
1919
}
2020
}
2121
}

framework/tests/Bing.Tests/Applications/CrudServiceTest.Save.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public CrudServiceTest()
9696
cfg.AddProfile(instance as Profile);
9797
}
9898
});
99-
var mapper = new AutoMapperObjectMapper(configuration);
99+
var mapper = new AutoMapperObjectMapper(configuration, instances);
100100
ObjectMapperExtensions.SetMapper(mapper);
101101
}
102102

modules/admin/src/Bing.Admin/Modules/AuthenticationModule.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public override IServiceCollection AddServices(IServiceCollection services)
4444
{
4545
o.Store.StoreOriginalPassword = true;
4646
o.Password.MinLength = 6;
47+
o.Password.Uppercase = true;
48+
o.Password.Lowercase = true;
49+
o.Password.Digit = true;
4750
});
4851
// 添加Jwt认证
4952
services.AddJwt(configuration);

0 commit comments

Comments
 (0)