Skip to content

Commit 9f5474f

Browse files
committed
simplify
1 parent 1023a52 commit 9f5474f

File tree

9 files changed

+71
-94
lines changed

9 files changed

+71
-94
lines changed

src/AutoMapper/ApiCompatBaseline.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttri
2323
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TDestination' on member 'AutoMapper.IMapper.ProjectTo<TDestination>(System.Linq.IQueryable, System.Object, System.Linq.Expressions.Expression<System.Func<TDestination, System.Object>>[])' in the contract but not the implementation.
2424
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableContextAttribute' exists on 'AutoMapper.IMapperBase' in the contract but not the implementation.
2525
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on parameter 'destinationType' on member 'AutoMapper.IMapperBase.Map(System.Object, System.Type, System.Type)' in the contract but not the implementation.
26+
InterfacesShouldHaveSameMembers : Interface member 'public System.Action<AutoMapper.TypeMap, AutoMapper.IMappingExpression> AutoMapper.IProfileConfiguration.AllTypeMapActions.get()' is present in the implementation but not in the contract.
27+
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<System.Action<AutoMapper.TypeMap, AutoMapper.IMappingExpression>> AutoMapper.IProfileConfiguration.AllTypeMapActions.get()' is present in the contract but not in the implementation.
28+
MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyCollection<System.Action<AutoMapper.TypeMap, AutoMapper.IMappingExpression>> AutoMapper.IProfileConfiguration.AllTypeMapActions.get()' does not exist in the implementation but it does exist in the contract.
2629
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TDestination' on member 'AutoMapper.ResolutionContext.AutoMapper.IMapperBase.Map<TDestination>(System.Object)' in the contract but not the implementation.
2730
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TSource' on member 'AutoMapper.ResolutionContext.AutoMapper.IMapperBase.Map<TSource, TDestination>(TSource)' in the contract but not the implementation.
2831
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TDestination' on member 'AutoMapper.ResolutionContext.AutoMapper.IMapperBase.Map<TSource, TDestination>(TSource)' in the contract but not the implementation.
@@ -31,6 +34,7 @@ CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttri
3134
MembersMustExist : Member 'public void AutoMapper.Configuration.MappingExpression..ctor(AutoMapper.Internal.TypePair, AutoMapper.MemberList)' does not exist in the implementation but it does exist in the contract.
3235
MembersMustExist : Member 'public void AutoMapper.Configuration.MappingExpression<TSource, TDestination>..ctor(AutoMapper.MemberList, System.Type, System.Type)' does not exist in the implementation but it does exist in the contract.
3336
MembersMustExist : Member 'protected void AutoMapper.Configuration.MappingExpressionBase<TSource, TDestination, TMappingExpression>..ctor(AutoMapper.MemberList, System.Type, System.Type)' does not exist in the implementation but it does exist in the contract.
37+
MembersMustExist : Member 'protected System.Collections.Generic.List<System.Action<AutoMapper.PropertyMap>> AutoMapper.Configuration.MemberConfigurationExpression<TSource, TDestination, TMember>.PropertyMapActions.get()' does not exist in the implementation but it does exist in the contract.
3438
MembersMustExist : Member 'public void AutoMapper.Configuration.ValidationContext..ctor(AutoMapper.Internal.TypePair, AutoMapper.MemberMap, AutoMapper.TypeMap, AutoMapper.Internal.Mappers.IObjectMapper)' does not exist in the implementation but it does exist in the contract.
3539
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.IgnoreAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
3640
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.MapAtRuntimeAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
@@ -70,4 +74,4 @@ CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttri
7074
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableContextAttribute' exists on 'AutoMapper.QueryableExtensions.Extensions.ProjectTo<TDestination>(System.Linq.IQueryable, AutoMapper.IConfigurationProvider, System.Object, System.Linq.Expressions.Expression<System.Func<TDestination, System.Object>>[])' in the contract but not the implementation.
7175
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on parameter 'parameters' on member 'AutoMapper.QueryableExtensions.Extensions.ProjectTo<TDestination>(System.Linq.IQueryable, AutoMapper.IConfigurationProvider, System.Object, System.Linq.Expressions.Expression<System.Func<TDestination, System.Object>>[])' in the contract but not the implementation.
7276
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.NullableAttribute' exists on generic param 'TDestination' on member 'AutoMapper.QueryableExtensions.Extensions.ProjectTo<TDestination>(System.Linq.IQueryable, AutoMapper.IConfigurationProvider, System.Object, System.Linq.Expressions.Expression<System.Func<TDestination, System.Object>>[])' in the contract but not the implementation.
73-
Total Issues: 71
77+
Total Issues: 75

src/AutoMapper/Configuration/CtorParamConfigurationExpression.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ public sealed class CtorParamConfigurationExpression<TSource, TDestination>(stri
3838
{
3939
public string CtorParamName { get; } = ctorParamName;
4040
public Type SourceType { get; } = sourceType;
41-
private readonly List<Action<ConstructorParameterMap>> _ctorParamActions = [];
41+
private Action<ConstructorParameterMap> _ctorParamActions;
4242
public void MapFrom<TMember>(Expression<Func<TSource, TMember>> sourceMember) =>
43-
_ctorParamActions.Add(cpm => cpm.MapFrom(sourceMember));
43+
_ctorParamActions += cpm => cpm.MapFrom(sourceMember);
4444
public void MapFrom<TMember>(Func<TSource, ResolutionContext, TMember> resolver)
4545
{
4646
Expression<Func<TSource, TDestination, TMember, ResolutionContext, TMember>> resolverExpression = (src, dest, destMember, ctxt) => resolver(src, ctxt);
47-
_ctorParamActions.Add(cpm => cpm.SetResolver(new FuncResolver(resolverExpression)));
47+
_ctorParamActions += cpm => cpm.SetResolver(new FuncResolver(resolverExpression));
4848
}
4949
public void MapFrom(string sourceMembersPath)
5050
{
5151
var sourceMembers = ReflectionHelper.GetMemberPath(SourceType, sourceMembersPath);
52-
_ctorParamActions.Add(cpm => cpm.MapFrom(sourceMembersPath, sourceMembers));
52+
_ctorParamActions += cpm => cpm.MapFrom(sourceMembersPath, sourceMembers);
5353
}
54-
public void ExplicitExpansion(bool value) => _ctorParamActions.Add(cpm => cpm.ExplicitExpansion = value);
54+
public void ExplicitExpansion(bool value) => _ctorParamActions += cpm => cpm.ExplicitExpansion = value;
5555
public void Configure(TypeMap typeMap)
5656
{
5757
var ctorMap = typeMap.ConstructorMap;
@@ -64,9 +64,6 @@ public void Configure(TypeMap typeMap)
6464
{
6565
throw new AutoMapperConfigurationException($"{typeMap.DestinationType.Name} does not have a matching constructor with a parameter named '{CtorParamName}'.\n{typeMap.DestinationType.FullName}.{typeMap.CheckRecord()}");
6666
}
67-
foreach (var action in _ctorParamActions)
68-
{
69-
action(parameter);
70-
}
67+
_ctorParamActions?.Invoke(parameter);
7168
}
7269
}

src/AutoMapper/Configuration/MappingExpression.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ public IMappingExpression IncludeMembers(params string[] memberNames)
2121
{
2222
SourceType.GetFieldOrProperty(memberName);
2323
}
24-
TypeMapActions.Add(tm => tm.IncludedMembersNames = memberNames);
24+
TypeMapActions += tm => tm.IncludedMembersNames = memberNames;
2525
return this;
2626
}
2727
public void ForAllMembers(Action<IMemberConfigurationExpression> memberOptions)
2828
{
29-
TypeMapActions.Add(typeMap =>
29+
TypeMapActions += typeMap =>
3030
{
3131
foreach (var accessor in typeMap.DestinationSetters)
3232
{
3333
ForMember(accessor, memberOptions);
3434
}
35-
});
35+
};
3636
}
3737
public IMappingExpression ForMember(string name, Action<IMemberConfigurationExpression> memberOptions)
3838
{
@@ -77,7 +77,7 @@ public IMappingExpression<TSource, TDestination> ForMember<TMember>(Expression<F
7777
var memberInfo = ReflectionHelper.FindProperty(destinationMember);
7878
return ForDestinationMember(memberInfo, memberOptions);
7979
}
80-
private void IncludeMembersCore(LambdaExpression[] memberExpressions) => TypeMapActions.Add(tm => tm.IncludedMembers = memberExpressions);
80+
private void IncludeMembersCore(LambdaExpression[] memberExpressions) => TypeMapActions += tm => tm.IncludedMembers = memberExpressions;
8181
public IMappingExpression<TSource, TDestination> IncludeMembers(params Expression<Func<TSource, object>>[] memberExpressions)
8282
{
8383
var memberExpressionsWithoutCastToObject = Array.ConvertAll(
@@ -97,13 +97,13 @@ public IMappingExpression<TSource, TDestination> ForMember(string name, Action<I
9797
}
9898
public void ForAllMembers(Action<IMemberConfigurationExpression<TSource, TDestination, object>> memberOptions)
9999
{
100-
TypeMapActions.Add(typeMap =>
100+
TypeMapActions += typeMap =>
101101
{
102102
foreach (var accessor in typeMap.DestinationSetters)
103103
{
104104
ForDestinationMember(accessor, memberOptions);
105105
}
106-
});
106+
};
107107
}
108108
public IMappingExpression<TSource, TDestination> Include<TOtherSource, TOtherDestination>() where TOtherSource : TSource where TOtherDestination : TDestination
109109
{

src/AutoMapper/Configuration/MemberConfigurationExpression.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public class MemberConfigurationExpression<TSource, TDestination, TMember>(Membe
1212
{
1313
private MemberInfo[] _sourceMembers;
1414
private readonly Type _sourceType = sourceType;
15-
protected List<Action<PropertyMap>> PropertyMapActions { get; } = [];
15+
protected Action<PropertyMap> PropertyMapActions { get; set; }
1616
public MemberInfo DestinationMember { get; } = destinationMember;
17-
public void MapAtRuntime() => PropertyMapActions.Add(pm => pm.Inline = false);
18-
public void NullSubstitute(object nullSubstitute) => PropertyMapActions.Add(pm => pm.NullSubstitute = nullSubstitute);
17+
public void MapAtRuntime() => PropertyMapActions += pm => pm.Inline = false;
18+
public void NullSubstitute(object nullSubstitute) => PropertyMapActions += pm => pm.NullSubstitute = nullSubstitute;
1919
public void MapFrom<TValueResolver>() where TValueResolver : IValueResolver<TSource, TDestination, TMember> =>
2020
MapFromCore(new(typeof(TValueResolver), typeof(IValueResolver<TSource, TDestination, TMember>)));
2121
protected void MapFromCore(ClassValueResolver config) => SetResolver(config);
22-
protected void SetResolver(IValueResolver config) => PropertyMapActions.Add(pm => pm.SetResolver(config));
22+
protected void SetResolver(IValueResolver config) => PropertyMapActions += pm => pm.SetResolver(config);
2323
public void MapFrom<TValueResolver, TSourceMember>(Expression<Func<TSource, TSourceMember>> sourceMember)
2424
where TValueResolver : IMemberValueResolver<TSource, TDestination, TSourceMember, TMember> =>
2525
MapFromCore<TValueResolver, TSourceMember>(sourceMember);
@@ -49,12 +49,12 @@ private void MapFromFunc<TResult>(Expression<Func<TSource, TDestination, TMember
4949
internal void MapFromExpression(LambdaExpression sourceExpression)
5050
{
5151
SourceExpression = sourceExpression;
52-
PropertyMapActions.Add(pm => pm.MapFrom(sourceExpression));
52+
PropertyMapActions += pm => pm.MapFrom(sourceExpression);
5353
}
5454
public void MapFrom(string sourceMembersPath)
5555
{
5656
_sourceMembers = ReflectionHelper.GetMemberPath(_sourceType, sourceMembersPath);
57-
PropertyMapActions.Add(pm => pm.MapFrom(sourceMembersPath, _sourceMembers));
57+
PropertyMapActions += pm => pm.MapFrom(sourceMembersPath, _sourceMembers);
5858
}
5959
public void Condition(Func<TSource, TDestination, TMember, TMember, ResolutionContext, bool> condition) =>
6060
ConditionCore((src, dest, srcMember, destMember, ctxt) => condition(src, dest, srcMember, destMember, ctxt));
@@ -65,35 +65,35 @@ public void Condition(Func<TSource, TDestination, TMember, bool> condition) =>
6565
public void Condition(Func<TSource, TDestination, bool> condition) => ConditionCore((src, dest, srcMember, destMember, ctxt) => condition(src, dest));
6666
public void Condition(Func<TSource, bool> condition) => ConditionCore((src, dest, srcMember, destMember, ctxt) => condition(src));
6767
private void ConditionCore(Expression<Func<TSource, TDestination, TMember, TMember, ResolutionContext, bool>> expr) =>
68-
PropertyMapActions.Add(pm => pm.Condition = expr);
68+
PropertyMapActions += pm => pm.Condition = expr;
6969
public void PreCondition(Func<TSource, bool> condition) => PreConditionCore((src, dest, ctxt) => condition(src));
7070
public void PreCondition(Func<ResolutionContext, bool> condition) => PreConditionCore((src, dest, ctxt) => condition(ctxt));
7171
public void PreCondition(Func<TSource, ResolutionContext, bool> condition) => PreConditionCore((src, dest, ctxt) => condition(src, ctxt));
7272
public void PreCondition(Func<TSource, TDestination, ResolutionContext, bool> condition) => PreConditionCore((src, dest, ctxt) => condition(src, dest, ctxt));
7373
private void PreConditionCore(Expression<Func<TSource, TDestination, ResolutionContext, bool>> expr) =>
74-
PropertyMapActions.Add(pm => pm.PreCondition = expr);
74+
PropertyMapActions += pm => pm.PreCondition = expr;
7575
public void AddTransform(Expression<Func<TMember, TMember>> transformer) =>
76-
PropertyMapActions.Add(pm => pm.AddValueTransformation(new ValueTransformerConfiguration(pm.DestinationType, transformer)));
77-
public void ExplicitExpansion(bool value) => PropertyMapActions.Add(pm => pm.ExplicitExpansion = value);
76+
PropertyMapActions += pm => pm.AddValueTransformation(new ValueTransformerConfiguration(pm.DestinationType, transformer));
77+
public void ExplicitExpansion(bool value) => PropertyMapActions += pm => pm.ExplicitExpansion = value;
7878
public void Ignore() => Ignore(ignorePaths: true);
7979
public void Ignore(bool ignorePaths)
8080
{
8181
Ignored = true;
82-
PropertyMapActions.Add(pm =>
82+
PropertyMapActions += pm =>
8383
{
8484
pm.Ignored = true;
8585
if (ignorePaths && pm.TypeMap.PathMaps.Count > 0)
8686
{
8787
pm.TypeMap.IgnorePaths(DestinationMember);
8888
}
89-
});
89+
};
9090
}
9191
public void AllowNull() => SetAllowNull(true);
9292
public void DoNotAllowNull() => SetAllowNull(false);
93-
private void SetAllowNull(bool value) => PropertyMapActions.Add(pm => pm.AllowNull = value);
93+
private void SetAllowNull(bool value) => PropertyMapActions += pm => pm.AllowNull = value;
9494
public void UseDestinationValue() => SetUseDestinationValue(true);
95-
private void SetUseDestinationValue(bool value) => PropertyMapActions.Add(pm => pm.UseDestinationValue = value);
96-
public void SetMappingOrder(int mappingOrder) => PropertyMapActions.Add(pm => pm.MappingOrder = mappingOrder);
95+
private void SetUseDestinationValue(bool value) => PropertyMapActions += pm => pm.UseDestinationValue = value;
96+
public void SetMappingOrder(int mappingOrder) => PropertyMapActions += pm => pm.MappingOrder = mappingOrder;
9797
public void ConvertUsing<TValueConverter, TSourceMember>() where TValueConverter : IValueConverter<TSourceMember, TMember> =>
9898
ConvertUsingCore<TValueConverter, TSourceMember>();
9999
public void ConvertUsing<TValueConverter, TSourceMember>(Expression<Func<TSource, TSourceMember>> sourceMember) where TValueConverter : IValueConverter<TSourceMember, TMember> =>
@@ -127,13 +127,7 @@ public void Configure(TypeMap typeMap)
127127
var propertyMap = typeMap.FindOrCreatePropertyMapFor(destMember, typeof(TMember) == typeof(object) ? destMember.GetMemberType() : typeof(TMember));
128128
Apply(propertyMap);
129129
}
130-
private void Apply(PropertyMap propertyMap)
131-
{
132-
foreach(var action in PropertyMapActions)
133-
{
134-
action(propertyMap);
135-
}
136-
}
130+
private void Apply(PropertyMap propertyMap) => PropertyMapActions?.Invoke(propertyMap);
137131
public LambdaExpression SourceExpression { get; private set; }
138132
public bool Ignored { get; private set; }
139133
public LambdaExpression GetDestinationExpression() => DestinationMember.Lambda();

src/AutoMapper/Configuration/PathConfigurationExpression.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,22 @@ public sealed class PathConfigurationExpression<TSource, TDestination, TMember>(
2525
{
2626
private readonly LambdaExpression _destinationExpression = destinationExpression;
2727
private LambdaExpression _sourceExpression;
28-
List<Action<PathMap>> PathMapActions { get; } = [];
28+
Action<PathMap> PathMapActions { get; set; }
2929
public MemberPath MemberPath { get; } = new(chain);
3030
public MemberInfo DestinationMember => MemberPath.Last;
3131
public void MapFrom<TSourceMember>(Expression<Func<TSource, TSourceMember>> sourceExpression) => MapFromUntyped(sourceExpression);
32-
public void Ignore() => PathMapActions.Add(pm => pm.Ignored = true);
32+
public void Ignore() => PathMapActions += pm => pm.Ignored = true;
3333
public void MapFromUntyped(LambdaExpression sourceExpression)
3434
{
3535
_sourceExpression = sourceExpression ?? throw new ArgumentNullException(nameof(sourceExpression), $"{nameof(sourceExpression)} may not be null when mapping {DestinationMember.Name} from {typeof(TSource)} to {typeof(TDestination)}.");
36-
PathMapActions.Add(pm => pm.MapFrom(sourceExpression));
36+
PathMapActions += pm => pm.MapFrom(sourceExpression);
3737
}
3838
public void Configure(TypeMap typeMap)
3939
{
4040
var pathMap = typeMap.FindOrCreatePathMapFor(_destinationExpression, MemberPath, typeMap);
4141
Apply(pathMap);
4242
}
43-
private void Apply(PathMap pathMap)
44-
{
45-
foreach (var action in PathMapActions)
46-
{
47-
action(pathMap);
48-
}
49-
}
43+
private void Apply(PathMap pathMap) => PathMapActions?.Invoke(pathMap);
5044
internal static IPropertyMapConfiguration Create(LambdaExpression destination, LambdaExpression source)
5145
{
5246
if (destination == null || !destination.IsMemberPath(out var chain))
@@ -67,10 +61,10 @@ internal static IPropertyMapConfiguration Create(LambdaExpression destination, L
6761
public LambdaExpression GetDestinationExpression() => _destinationExpression;
6862
public IPropertyMapConfiguration Reverse() => Create(_sourceExpression, _destinationExpression);
6963
public void Condition(Func<ConditionParameters<TSource, TDestination, TMember>, bool> condition) =>
70-
PathMapActions.Add(pm =>
64+
PathMapActions += pm =>
7165
{
7266
Expression<Func<TSource, TDestination, TMember, TMember, ResolutionContext, bool>> expr =
7367
(src, dest, srcMember, destMember, ctxt) => condition(new(src, dest, srcMember, destMember, ctxt));
7468
pm.Condition = expr;
75-
});
69+
};
7670
}

0 commit comments

Comments
 (0)