Skip to content

Commit 1023a52

Browse files
committed
no need to throw to report errors
1 parent 956975b commit 1023a52

File tree

7 files changed

+115
-123
lines changed

7 files changed

+115
-123
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ dotnet_style_namespace_match_folder = true:silent
8686
dotnet_style_explicit_tuple_names = true:suggestion
8787
dotnet_style_prefer_inferred_tuple_names = true:suggestion
8888
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
89+
csharp_style_prefer_primary_constructors = true:suggestion
90+
csharp_prefer_system_threading_lock = true:suggestion
91+
csharp_space_after_keywords_in_control_flow_statements = true
8992

9093
[*.xml]
9194
indent_size = 2

docs/source/Configuration-validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ To skip validation altogether for this map, use `MemberList.None`. That's the de
5555

5656
## Custom validations
5757

58-
You can add custom validations through an extension point. See [here](https://github.com/AutoMapper/AutoMapper/blob/bdc0120497d192a2741183415543f6119f50a982/src/UnitTests/CustomValidations.cs#L42).
58+
You can add custom validations through an extension point. See [here](https://github.com/AutoMapper/AutoMapper/blob/master/src/UnitTests/CustomValidations.cs).

src/AutoMapper/Configuration/ConfigurationValidator.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void AssertConfigurationExpressionIsValid(TypeMap[] typeMaps)
1313
.Select(g => (TypePair: g.Key, ProfileNames: g.Select(tmc => tmc.profile.ProfileName).ToArray()))
1414
.Select(g => new DuplicateTypeMapConfigurationException.TypeMapConfigErrors(g.TypePair, g.ProfileNames))
1515
.ToArray();
16-
if(duplicateTypeMapConfigs.Length != 0)
16+
if (duplicateTypeMapConfigs.Length != 0)
1717
{
1818
throw new DuplicateTypeMapConfigurationException(duplicateTypeMapConfigs);
1919
}
@@ -29,49 +29,49 @@ where typeMap.ShouldCheckForValid
2929
let canConstruct = typeMap.PassesCtorValidation
3030
where unmappedPropertyNames.Length > 0 || !canConstruct
3131
select new AutoMapperConfigurationException.TypeMapConfigErrors(typeMap, unmappedPropertyNames, canConstruct)).ToArray();
32-
if(badTypeMaps.Length > 0)
32+
if (badTypeMaps.Length > 0)
3333
{
3434
configExceptions.Add(new AutoMapperConfigurationException(badTypeMaps));
3535
}
3636
HashSet<TypeMap> typeMapsChecked = [];
37-
foreach(var typeMap in typeMaps)
37+
foreach (var typeMap in typeMaps)
3838
{
3939
DryRunTypeMap(typeMap.Types, typeMap, null);
4040
}
41-
if(configExceptions.Count > 1)
41+
if (configExceptions.Count > 1)
4242
{
4343
throw new AggregateException(configExceptions);
4444
}
45-
if(configExceptions.Count > 0)
45+
if (configExceptions.Count > 0)
4646
{
4747
throw configExceptions[0];
4848
}
4949
void DryRunTypeMap(TypePair types, TypeMap typeMap, MemberMap memberMap)
5050
{
51-
if(typeMap == null)
51+
if (typeMap == null)
5252
{
53-
if(types.ContainsGenericParameters)
53+
if (types.ContainsGenericParameters)
5454
{
5555
return;
5656
}
5757
typeMap = config.ResolveTypeMap(types.SourceType, types.DestinationType);
5858
}
59-
if(typeMap != null)
59+
if (typeMap != null)
6060
{
61-
if(typeMapsChecked.Add(typeMap) && Validate(new(types, memberMap, configExceptions, typeMap)) && typeMap.ShouldCheckForValid)
61+
if (typeMapsChecked.Add(typeMap) && Validate(new(types, memberMap, configExceptions, typeMap)) && typeMap.ShouldCheckForValid)
6262
{
6363
CheckPropertyMaps(typeMap);
6464
}
6565
}
6666
else
6767
{
6868
var mapperToUse = config.FindMapper(types);
69-
if(mapperToUse == null)
69+
if (mapperToUse == null)
7070
{
7171
configExceptions.Add(new AutoMapperConfigurationException(memberMap.TypeMap.Types) { MemberMap = memberMap });
7272
return;
7373
}
74-
if(Validate(new(types, memberMap, configExceptions, ObjectMapper: mapperToUse)) && mapperToUse.GetAssociatedTypes(types) is TypePair newTypes &&
74+
if (Validate(new(types, memberMap, configExceptions, ObjectMapper: mapperToUse)) && mapperToUse.GetAssociatedTypes(types) is TypePair newTypes &&
7575
newTypes != types)
7676
{
7777
DryRunTypeMap(newTypes, null, memberMap);
@@ -80,15 +80,15 @@ void DryRunTypeMap(TypePair types, TypeMap typeMap, MemberMap memberMap)
8080
}
8181
void CheckPropertyMaps(TypeMap typeMap)
8282
{
83-
foreach(var memberMap in typeMap.MemberMaps)
83+
foreach (var memberMap in typeMap.MemberMaps)
8484
{
85-
if(memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName)))
85+
if (memberMap.Ignored || (memberMap is PropertyMap && typeMap.ConstructorParameterMatches(memberMap.DestinationName)))
8686
{
8787
continue;
8888
}
8989
var sourceType = memberMap.SourceType;
9090
// when we don't know what the source type is, bail
91-
if(sourceType.IsGenericParameter || sourceType == typeof(object))
91+
if (sourceType.IsGenericParameter || sourceType == typeof(object))
9292
{
9393
continue;
9494
}
@@ -97,17 +97,14 @@ void CheckPropertyMaps(TypeMap typeMap)
9797
}
9898
bool Validate(ValidationContext context)
9999
{
100-
foreach(var validator in Expression.Validators)
100+
try
101101
{
102-
try
103-
{
104-
validator(context);
105-
}
106-
catch(Exception e)
107-
{
108-
configExceptions.Add(e);
109-
return false;
110-
}
102+
Expression.Validators?.Invoke(context);
103+
}
104+
catch (Exception e)
105+
{
106+
configExceptions.Add(e);
107+
return false;
111108
}
112109
return true;
113110
}

src/AutoMapper/Configuration/MapperConfigurationExpression.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,22 @@ public interface IMapperConfigurationExpression : IProfileExpression
8888
public sealed class MapperConfigurationExpression : Profile, IGlobalConfigurationExpression
8989
{
9090
private readonly List<Profile> _profiles = [];
91-
private readonly List<Validator> _validators = [];
91+
private Validator _validators;
9292
private Func<Type, object> _serviceCtor = Activator.CreateInstance;
9393
private List<IProjectionMapper> _projectionMappers;
9494
/// <summary>
9595
/// Add an action to be called when validating the configuration.
9696
/// </summary>
9797
/// <param name="validator">the validation callback</param>
9898
void IGlobalConfigurationExpression.Validator(Validator validator) =>
99-
_validators.Add(validator ?? throw new ArgumentNullException(nameof(validator)));
99+
_validators += validator ?? throw new ArgumentNullException(nameof(validator));
100100
/// <summary>
101101
/// How many levels deep should AutoMapper try to inline the execution plan for child classes.
102102
/// See <a href="https://automapper.readthedocs.io/en/latest/Understanding-your-mapping.html">the docs</a> for details.
103103
/// </summary>
104104
int IGlobalConfigurationExpression.MaxExecutionPlanDepth { get; set; } = 1;
105105

106-
List<Validator> IGlobalConfigurationExpression.Validators => _validators;
106+
Validator IGlobalConfigurationExpression.Validators => _validators;
107107

108108
List<IProjectionMapper> IGlobalConfigurationExpression.ProjectionMappers => _projectionMappers ??= ProjectionBuilder.DefaultProjectionMappers();
109109

src/AutoMapper/Internal/InternalApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface IGlobalConfigurationExpression : IMapperConfigurationExpression
3636
/// See <a href="https://automapper.readthedocs.io/en/latest/Understanding-your-mapping.html">the docs</a> for details.
3737
/// </summary>
3838
int MaxExecutionPlanDepth { get; set; }
39-
List<Validator> Validators { get; }
39+
Validator Validators { get; }
4040
List<IProjectionMapper> ProjectionMappers { get; }
4141
/// <summary>
4242
/// How many levels deep should recursive queries be expanded.

0 commit comments

Comments
 (0)