Skip to content

Commit 823aa4d

Browse files
committed
refactoring
1 parent 8b759bf commit 823aa4d

File tree

6 files changed

+60
-14
lines changed

6 files changed

+60
-14
lines changed

src/Mapster.Tests/WhenRegisteringAndMappingRace.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public void TestCleanup()
1515
TypeAdapterConfigFactory.GlobalSettings.Clear();
1616
TypeAdapterConfigFactory.GlobalSettings.RequireExplicitMapping = false;
1717
TypeAdapterConfigFactory.GlobalSettings.RequireDestinationMemberSource = false;
18-
TypeAdapterConfigFactory.GlobalSettings.ConcurencyEnviroment = false;
18+
if(TypeAdapterConfigFactory.GlobalSettings is IConfigConcurrency config)
19+
config.ConcurrencyEnvironment = false;
1920
}
2021

2122

@@ -98,11 +99,10 @@ public void Explicit_Mapping_Requirementd()
9899

99100
TypeAdapterConfigFactory.GlobalSettings.RequireExplicitMapping = true;
100101
TypeAdapterConfigFactory.GlobalSettings.RequireDestinationMemberSource = true;
101-
TypeAdapterConfigFactory.GlobalSettings.ConcurencyEnviroment = true;
102102

103103
var simplePoco = new WhenAddingCustomMappings.SimplePoco { Id = Guid.NewGuid(), Name = "TestName" };
104104

105-
TypeAdapterConfig<WhenAddingCustomMappings.SimplePoco, WeirdPoco>.NewConfig()
105+
TypeAdapterConfigConcurrency<WhenAddingCustomMappings.SimplePoco, WeirdPoco>.NewConfig()
106106

107107
.Map(dest => dest.IHaveADifferentId, src => src.Id)
108108
.Map(dest => dest.MyNamePropertyIsDifferent, src => src.Name)
@@ -116,8 +116,7 @@ public void Explicit_Mapping_Requirementd()
116116
Parallel.Invoke(
117117
() =>
118118
{
119-
TypeAdapterConfig<WhenAddingCustomMappings.SimplePoco, WeirdPoco>.NewConfig()
120-
//.Ignore(dest => dest.Children)
119+
TypeAdapterConfigConcurrency<WhenAddingCustomMappings.SimplePoco, WeirdPoco>.NewConfig()
121120
.Map(dest => dest.IHaveADifferentId, src => src.Id)
122121
.Map(dest => dest.MyNamePropertyIsDifferent, src => src.Name)
123122
.Ignore(dest => dest.Children)

src/Mapster/TypeAdapterConfig/BaseTypeAdapterConfigDecorator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public BaseTypeAdapterConfigDecorator(ITypeAdapterConfig config, bool IsGlobal =
3939

4040
public bool SelfContainedCodeGeneration { get => _Config.SelfContainedCodeGeneration; set => _Config.SelfContainedCodeGeneration = value; }
4141
public Func<LambdaExpression, Delegate> Compiler { get => _Config.Compiler; set => _Config.Compiler = value; }
42-
public bool ConcurencyEnviroment { get => _Config.ConcurencyEnviroment; set => _Config.ConcurencyEnviroment = value; }
42+
public bool ConcurrencyEnvironment { get => _Config.ConcurrencyEnvironment; }
4343

4444
public AutoResetEvent Configure => _Config.Configure;
4545

src/Mapster/TypeAdapterConfig/ITypeAdapterConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface ITypeAdapterConfig
2525
public IEnumerable<TypeAdapterRule> GetRules(Func<TypeAdapterRule, bool> predicate);
2626
bool SelfContainedCodeGeneration { get; set; }
2727

28-
bool ConcurencyEnviroment { get; set; }
28+
bool ConcurrencyEnvironment { get; }
2929
AutoResetEvent Configure { get;}
3030
AutoResetEvent AdaptMutex { get; }
3131

src/Mapster/TypeAdapterConfig/TypeAdapterConfig.cs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Mapster
1212
{
13-
public class TypeAdapterConfig : ITypeAdapterConfig
13+
public class TypeAdapterConfig : ITypeAdapterConfig, IConfigConcurrency
1414
{
1515
[AdaptIgnore]
1616
public bool IsGlobalSettings { get; private set; }
@@ -29,7 +29,7 @@ public class TypeAdapterConfig : ITypeAdapterConfig
2929
[AdaptIgnore]
3030
public ConfigCompileStorage ConfigCompile { get; private set; }
3131

32-
public bool ConcurencyEnviroment { get; set; }
32+
public bool ConcurrencyEnvironment { get; set; }
3333

3434
[AdaptIgnore]
3535
public AutoResetEvent Configure { get; private set; }
@@ -86,7 +86,7 @@ public TypeAdapterSetter<TSource, TDestination> ForType<TSource, TDestination>()
8686

8787
public LambdaExpression CreateMapExpression(TypeTuple tuple, MapType mapType)
8888
{
89-
if (ConcurencyEnviroment)
89+
if (ConcurrencyEnvironment)
9090
{
9191
Configure.WaitOne(-1);
9292
/// AdaptMutex.WaitOne(-1, false);
@@ -110,7 +110,7 @@ public LambdaExpression CreateMapExpression(TypeTuple tuple, MapType mapType)
110110
}
111111
finally
112112
{
113-
if (ConcurencyEnviroment)
113+
if (ConcurrencyEnvironment)
114114
{
115115
AdaptMutex.Set();
116116
Configure.Set();
@@ -325,6 +325,9 @@ public static class TypeAdapterConfig<TSource, TDestination>
325325
/// <returns></returns>
326326
public static TypeAdapterSetter<TSource, TDestination> NewConfig()
327327
{
328+
if (TypeAdapterConfigFactory.GlobalSettings is IConfigConcurrency config)
329+
config.ConcurrencyEnvironment = false;
330+
328331
return TypeAdapterConfigFactory.GlobalSettings.NewConfig<TSource, TDestination>();
329332
}
330333

@@ -335,6 +338,9 @@ public static TypeAdapterSetter<TSource, TDestination> NewConfig()
335338
/// <returns></returns>
336339
public static TypeAdapterSetter<TSource, TDestination> ForType()
337340
{
341+
if (TypeAdapterConfigFactory.GlobalSettings is IConfigConcurrency config)
342+
config.ConcurrencyEnvironment = false;
343+
338344
return TypeAdapterConfigFactory.GlobalSettings.ForType<TSource, TDestination>();
339345
}
340346

@@ -347,4 +353,45 @@ public static void Clear()
347353
TypeAdapterConfigFactory.GlobalSettings.Remove(typeof(TSource), typeof(TDestination));
348354
}
349355
}
356+
357+
public static class TypeAdapterConfigConcurrency<TSource, TDestination>
358+
{
359+
/// <summary>
360+
/// Creates a new configuration for mapping between the source and destination types.
361+
/// </summary>
362+
/// <returns></returns>
363+
public static TypeAdapterSetter<TSource, TDestination> NewConfig()
364+
{
365+
if (TypeAdapterConfigFactory.GlobalSettings is IConfigConcurrency config)
366+
config.ConcurrencyEnvironment = true;
367+
368+
return TypeAdapterConfigFactory.GlobalSettings.NewConfig<TSource, TDestination>();
369+
}
370+
371+
372+
/// <summary>
373+
/// Creates a configuration for mapping between the source and destination types.
374+
/// </summary>
375+
/// <returns></returns>
376+
public static TypeAdapterSetter<TSource, TDestination> ForType()
377+
{
378+
if (TypeAdapterConfigFactory.GlobalSettings is IConfigConcurrency config)
379+
config.ConcurrencyEnvironment = true;
380+
return TypeAdapterConfigFactory.GlobalSettings.ForType<TSource, TDestination>();
381+
}
382+
383+
384+
/// <summary>
385+
/// Clears the type mapping configuration for the specified source and destination types.
386+
/// </summary>
387+
public static void Clear()
388+
{
389+
TypeAdapterConfigFactory.GlobalSettings.Remove(typeof(TSource), typeof(TDestination));
390+
}
391+
}
392+
393+
internal interface IConfigConcurrency
394+
{
395+
public bool ConcurrencyEnvironment { get; set; }
396+
}
350397
}

src/Mapster/TypeAdapterConfig/TypeAdapterConfigTSetterExtentions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static class TypeAdapterConfigTSetterExtentions
1515
/// <returns></returns>
1616
public static TypeAdapterSetter NewConfig(this ITypeAdapterConfig config, Type sourceType, Type destinationType)
1717
{
18-
if (config.ConcurencyEnviroment)
18+
if (config.ConcurrencyEnvironment)
1919
{
2020
// config.AdaptMutex.WaitOne(-1);
2121
config.Configure.WaitOne(-1, false);
@@ -33,7 +33,7 @@ public static TypeAdapterSetter NewConfig(this ITypeAdapterConfig config, Type s
3333
/// <returns></returns>
3434
public static TypeAdapterSetter<TSource, TDestination> NewConfig<TSource, TDestination>(this ITypeAdapterConfig config)
3535
{
36-
if (config.ConcurencyEnviroment)
36+
if (config.ConcurrencyEnvironment)
3737
{
3838
// config.AdaptMutex.WaitOne(-1);
3939
config.Configure.WaitOne(-1, false);

src/Mapster/TypeAdapterSetter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static TSetter BreakConfig<TSetter>(this TSetter setter) where TSetter :
4141
}
4242
finally
4343
{
44-
if (setter.Config.ConcurencyEnviroment)
44+
if (setter.Config.ConcurrencyEnvironment)
4545
{
4646
setter.Config.Configure.Set();
4747
setter.Config.AdaptMutex.Set();

0 commit comments

Comments
 (0)