Skip to content

Commit 8342057

Browse files
committed
🎨 refactor(AutoMapper): 重构 AutoMapper 组件
- 移除旧的 AutoMapper 依赖和引用 - 优化 AutoMapperObjectMapper 类的实现 - 删除不必要的测试类和样例类 - 更新解决方案结构,添加新的 Components 和 ObjectMapping 项目
1 parent f918b19 commit 8342057

23 files changed

+181
-86
lines changed

Bing.All.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Ddd.Domain.Extensions.
370370
EndProject
371371
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bing.Aop.AspectCore.Tests", "framework\tests\Bing.Aop.AspectCore.Tests\Bing.Aop.AspectCore.Tests.csproj", "{F43AD659-D8F2-4B30-AB21-85CD376CAA20}"
372372
EndProject
373+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Components", "Components", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
374+
EndProject
375+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ObjectMapping", "ObjectMapping", "{F339B49B-E2F8-44CA-AD66-5E6CAA1B6E0F}"
376+
EndProject
373377
Global
374378
GlobalSection(SolutionConfigurationPlatforms) = preSolution
375379
Debug|Any CPU = Debug|Any CPU
@@ -876,7 +880,7 @@ Global
876880
{D45DFEED-3A83-436D-ACBD-771DDDC4718F} = {DB31385F-49B0-49FB-946A-DBAD2A070B7A}
877881
{BDBABE46-FAAE-459D-BC78-B5BA6503EBBA} = {4A4E88DE-5CE7-433E-BED8-D269E385ECFD}
878882
{5C7976B4-C243-41B9-8303-8E8FAE099D31} = {A3E29077-2385-4F4F-B2BB-01975E24979C}
879-
{741EE94D-5F03-426E-9D34-CBDA41671742} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
883+
{741EE94D-5F03-426E-9D34-CBDA41671742} = {F339B49B-E2F8-44CA-AD66-5E6CAA1B6E0F}
880884
{A66F9341-005F-4FAC-BF93-D676329A34B4} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
881885
{58BE2219-6694-4630-85E6-7953A608A2D6} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
882886
{0F86BCCF-6A1D-41DD-A9C0-4B3BF8A840BF} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
@@ -1008,6 +1012,8 @@ Global
10081012
{0359A38B-4080-41A1-BB75-6B2EA48D8E2D} = {C6B2743E-B7EE-418C-9F9B-79976B71C46B}
10091013
{BE5E6E82-3CB6-4C1B-8E2D-1B7A2483B41B} = {96F03C7E-0959-4BE4-BE87-E987E1C4BEDA}
10101014
{F43AD659-D8F2-4B30-AB21-85CD376CAA20} = {793DF696-AA3D-4D44-9657-B66000DCEFFA}
1015+
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {5C7976B4-C243-41B9-8303-8E8FAE099D31}
1016+
{F339B49B-E2F8-44CA-AD66-5E6CAA1B6E0F} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
10111017
EndGlobalSection
10121018
GlobalSection(ExtensibilityGlobals) = postSolution
10131019
SolutionGuid = {C1202A0F-83CC-4602-BCE5-20CB640BCAD4}

framework/src/Bing.AutoMapper/Bing.AutoMapper.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
<Import Project="..\..\..\framework.props" />
99

10-
<Import Project="dependency.props" />
10+
<ItemGroup>
11+
<ProjectReference Include="..\Bing.ObjectMapping\Bing.ObjectMapping.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="AutoMapper" Version="10.1.1" />
16+
</ItemGroup>
1117

12-
<Import Project="references.props" />
1318
</Project>

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

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Linq.Expressions;
22
using AutoMapper;
3+
using AutoMapper.Configuration;
34
using AutoMapper.QueryableExtensions;
45
using Bing.ObjectMapping;
56

@@ -10,6 +11,11 @@ namespace Bing.AutoMapper;
1011
/// </summary>
1112
public class AutoMapperObjectMapper : Bing.ObjectMapping.IObjectMapper
1213
{
14+
/// <summary>
15+
/// 最大递归获取结果次数
16+
/// </summary>
17+
private const int MaxGetResultCount = 16;
18+
1319
/// <summary>
1420
/// 同步锁
1521
/// </summary>
@@ -63,20 +69,11 @@ public AutoMapperObjectMapper(IConfigurationProvider configuration, IReadOnlyCol
6369
/// <param name="destination">目标对象</param>
6470
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
6571
{
66-
try
67-
{
68-
if (source == null)
69-
return default;
70-
var sourceType = GetType(source);
71-
var destinationType = GetType(destination);
72-
return GetResult(sourceType, destinationType, source, destination);
73-
}
74-
catch (AutoMapperMappingException e)
75-
{
76-
if (e.InnerException != null && e.InnerException.Message.StartsWith("Missing type map configuration"))
77-
return GetResult(GetType(e.MemberMap.SourceType), GetType(e.MemberMap.DestinationType), source, destination);
78-
throw;
79-
}
72+
if (source == null)
73+
return default;
74+
var sourceType = GetType(source);
75+
var destinationType = GetType(destination);
76+
return GetResult(sourceType, destinationType, source, destination, 0);
8077
}
8178

8279
/// <summary>
@@ -100,17 +97,31 @@ public TDestination Map<TSource, TDestination>(TSource source, TDestination dest
10097
/// <param name="destinationType">目标类型</param>
10198
/// <param name="source">源对象</param>
10299
/// <param name="destination">目标对象</param>
103-
private TDestination GetResult<TDestination>(Type sourceType, Type destinationType, object source, TDestination destination)
100+
/// <param name="i">迭代次数</param>
101+
private TDestination GetResult<TDestination>(Type sourceType, Type destinationType, object source, TDestination destination, int i)
104102
{
105-
if (Exists(sourceType, destinationType))
106-
return GetResult(source, destination);
107-
lock (Sync)
103+
try
108104
{
105+
if (i >= MaxGetResultCount)
106+
return default;
107+
i += 1;
109108
if (Exists(sourceType, destinationType))
110109
return GetResult(source, destination);
111-
ConfigMap(sourceType, destinationType);
110+
lock (Sync)
111+
{
112+
if (Exists(sourceType, destinationType))
113+
return GetResult(source, destination);
114+
ConfigMap(sourceType, destinationType);
115+
}
116+
return GetResult(source, destination);
117+
}
118+
catch (AutoMapperMappingException ex)
119+
{
120+
if (ex.InnerException != null && ex.InnerException.Message.StartsWith("Missing type map configuration"))
121+
return GetResult(GetType(ex.MemberMap.SourceType), GetType(ex.MemberMap.DestinationType), source, destination, i);
122+
throw;
112123
}
113-
return GetResult(source, destination);
124+
114125
}
115126

116127
/// <summary>
@@ -160,4 +171,4 @@ private void ConfigMap(Type sourceType, Type destinationType)
160171
public IQueryable<TOutputDto> ToOutput<TOutputDto>(IQueryable source, params Expression<Func<TOutputDto, object>>[] membersToExpand) => source.ProjectTo(_configuration, membersToExpand);
161172

162173
#endregion
163-
}
174+
}

framework/src/Bing.AutoMapper/dependency.props

Lines changed: 0 additions & 5 deletions
This file was deleted.

framework/src/Bing.AutoMapper/references.props

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
<Import Project="..\..\..\common.tests.props" />
2+
<Import Project="..\..\..\framework.tests.props" />
33
<PropertyGroup>
44
<RootNamespace />
5-
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1</TargetFrameworks>
5+
<TargetFrameworks>net6.0;net5.0;netcoreapp3.1;</TargetFrameworks>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<ProjectReference Include="..\..\..\tests\Bing.Tests.Samples\Bing.Tests.Samples.csproj" />
109
<ProjectReference Include="..\..\src\Bing.AutoMapper\Bing.AutoMapper.csproj" />
11-
<ProjectReference Include="..\..\src\Bing.Core\Bing.Core.csproj" />
10+
<ProjectReference Include="..\..\src\Bing.Ddd.Application\Bing.Ddd.Application.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
14+
<PackageReference Include="AutoMapper" Version="13.0.1" />
15+
</ItemGroup>
16+
17+
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
18+
<PackageReference Include="AutoMapper" Version="13.0.1" />
19+
</ItemGroup>
20+
21+
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
22+
<PackageReference Include="AutoMapper" Version="13.0.1" />
1223
</ItemGroup>
1324
</Project>

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Linq;
1+
using System.Diagnostics;
52
using AutoMapper;
63
using Bing.Extensions;
7-
using Bing.Helpers;
84
using Bing.ObjectMapping;
95
using Bing.Reflection;
10-
using Bing.SampleClasses;
11-
using Xunit;
6+
using Thread = Bing.Helpers.Thread;
127

138
namespace Bing.AutoMapper;
149

@@ -32,7 +27,6 @@ public ObjectMapperExtensionsTest()
3227
{
3328
foreach (var instance in instances)
3429
{
35-
3630
Debug.WriteLine($"初始化AutoMapper配置:{instance.GetType().FullName}");
3731
instance.CreateMap();
3832
// ReSharper disable once SuspiciousTypeConversion.Global
@@ -272,4 +266,4 @@ public void Test_MapTo_Throw()
272266
var entity3 = dto.MapTo<TreeEntitySample>();
273267
Assert.Equal(parentId, entity3.ParentId.SafeString());
274268
}
275-
}
269+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
using System.Diagnostics;
2-
using System.Linq;
32
using AutoMapper;
43
using Bing.ObjectMapping;
54
using Bing.Reflection;
6-
using Bing.SampleClasses;
7-
using Xunit;
85

96
namespace Bing.AutoMapper;
107

@@ -96,4 +93,4 @@ public void Test_Map_4()
9693
var sample4 = _mapper.Map<Sample, Sample2>(sample);
9794
Assert.Equal("a", sample4.StringValue);
9895
}
99-
}
96+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using AutoMapper;
22
using Bing.ObjectMapping;
3-
using Bing.Tests.Samples;
43

54
namespace Bing.AutoMapper;
65

@@ -14,9 +13,12 @@ public class TestMapperConfiguration : Profile, IObjectMapperProfile
1413
/// </summary>
1514
public void CreateMap()
1615
{
17-
//CreateMap<Sample, Sample4>()
18-
// .ForMember(o => o.StringValue, o => o.MapFrom((s, d) => s.StringValue + "-1"));
16+
CreateMap<Sample, Sample4>()
17+
.ForMember(o => o.StringValue, o => o.MapFrom((s, d) => s.StringValue + "-1"));
1918
CreateMap<AutoMapperSourceSample, AutoMapperTargetSample>()
2019
.ForMember(x => x.TargetSampleValue, x => x.MapFrom(p => p.SourceStringValue + "-001"));
20+
21+
// 忽略 Castle 代理类
22+
RecognizeDestinationPrefixes("Castle.Proxies.");
2123
}
2224
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Bing.Tests.Samples;
2+
3+
public class AutoMapperSourceSample
4+
{
5+
public string SourceStringValue { get; set; }
6+
}

0 commit comments

Comments
 (0)