Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
667a3d5
use Enum.Parse<TEnum>
lbargaoanu Jul 30, 2022
77086e8
less allocations
lbargaoanu Jul 31, 2022
e3aecd3
reuse the execution plan between user Map calls and internal Map calls
lbargaoanu Aug 1, 2022
6581906
remove IObjectMapperInfo
lbargaoanu Aug 2, 2022
5f0cebd
use the more efficient Expression.Invoke instead of ConvertReplacePar…
lbargaoanu Aug 3, 2022
9fc7cdf
less allocations
lbargaoanu Aug 3, 2022
7fdf915
less null checks
lbargaoanu Aug 4, 2022
4b507ee
unreachable, As maps are sealed by default
lbargaoanu Aug 6, 2022
84e3fff
less allocations
lbargaoanu Aug 6, 2022
b5cc0a6
don't try to match ignored members
lbargaoanu Aug 7, 2022
df48e72
more efficient
lbargaoanu Aug 7, 2022
12eaef9
drop RegEx
lbargaoanu Aug 8, 2022
06b0713
docs
lbargaoanu Aug 8, 2022
dc7e58f
cosmetic
lbargaoanu Aug 8, 2022
9356388
less allocations
lbargaoanu Aug 8, 2022
39fccd9
cosmetic
lbargaoanu Aug 9, 2022
a3ae964
less allocations
lbargaoanu Aug 9, 2022
00d29ee
we don't need to create As type maps
lbargaoanu Aug 11, 2022
c9bceef
remove ITypeMapConfiguration
lbargaoanu Aug 11, 2022
a0988d7
cosmetic
lbargaoanu Aug 11, 2022
0b9cf8c
try to show a better error message when mapping records through const…
lbargaoanu Aug 11, 2022
b9699dd
less allocations
lbargaoanu Aug 13, 2022
ed9097f
special case the defaults for naming conventions
lbargaoanu Aug 13, 2022
93576ef
make TypeMapPlanBuilder a struct
lbargaoanu Aug 15, 2022
3adb02f
use readonly record struct
lbargaoanu Aug 15, 2022
7720ac8
assume that a protected constructor with CompilerGeneratedAttribute m…
lbargaoanu Aug 15, 2022
4e1117f
less allocations
lbargaoanu Aug 15, 2022
dac3062
rename configurationProvider to the shorter configuration in most places
lbargaoanu Aug 17, 2022
b24560f
less allocations
lbargaoanu Aug 17, 2022
953d66d
converters with no source should throw
lbargaoanu Aug 17, 2022
ebd2c43
less allocations
lbargaoanu Aug 18, 2022
c5d7c36
cosmetic
lbargaoanu Aug 18, 2022
12676dd
less allocations
lbargaoanu Aug 19, 2022
85ff705
make MapFrom name more like the built-in mapping
lbargaoanu Aug 19, 2022
c71f168
less allocations
lbargaoanu Aug 20, 2022
3de255f
cosmetic
lbargaoanu Aug 21, 2022
6925737
use profiles in the benchmark
lbargaoanu Aug 21, 2022
08ec72a
simplify conventions
lbargaoanu Aug 21, 2022
e9287a2
merge the profile member configuration with the global one
lbargaoanu Aug 21, 2022
0d2aa50
cosmetic
lbargaoanu Aug 22, 2022
ec0a832
test reusing the execution plan
lbargaoanu Aug 22, 2022
593ad53
refactor test base classes
lbargaoanu Aug 22, 2022
806e73f
cosmetic
lbargaoanu Aug 23, 2022
d0e0e24
avoid changing the resolver configuration objects because they're reu…
lbargaoanu Aug 31, 2022
77e0015
cosmetic
lbargaoanu Aug 31, 2022
20c283a
less allocations
lbargaoanu Sep 3, 2022
66f0020
cosmetic
lbargaoanu Sep 3, 2022
f1655e1
file scoped namespaces
lbargaoanu Sep 3, 2022
288c4ad
global usings
lbargaoanu Sep 3, 2022
e6f2ca3
cosmetic
lbargaoanu Sep 4, 2022
daacd80
less allocations
lbargaoanu Sep 10, 2022
c608787
NullMappingOrderComesFirst
lbargaoanu Sep 10, 2022
434703c
less allocations
lbargaoanu Sep 10, 2022
4ba0e5f
we could include a different constructor
lbargaoanu Sep 10, 2022
b98273e
less allocations
lbargaoanu Sep 10, 2022
7eeaf59
cosmetic
lbargaoanu Sep 11, 2022
efd1df9
allocate _runtimeTypeDetails lazily
lbargaoanu Sep 12, 2022
3a491a3
create the projection builder lazily
lbargaoanu Sep 12, 2022
7da36f6
cosmetic
lbargaoanu Sep 12, 2022
970ab2d
an adhoc fix for including open generic config
lbargaoanu Sep 14, 2022
e7ba023
cosmetic
lbargaoanu Sep 18, 2022
2b99e96
MapFrom should override ignore
lbargaoanu Sep 18, 2022
3f8448a
UseDestinationValueNullable
lbargaoanu Sep 19, 2022
8b83aa7
cosmetic
lbargaoanu Sep 21, 2022
5e0066e
avoid LINQ
lbargaoanu Sep 23, 2022
7d65b78
the destination type name is already in the error message
lbargaoanu Sep 24, 2022
3c2d8cd
cosmetic
lbargaoanu Sep 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify conventions
  • Loading branch information
lbargaoanu committed Aug 21, 2022
commit 08ec72aec1c0008bd05982e35a898cddba7156cf
17 changes: 16 additions & 1 deletion src/AutoMapper/ApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ MembersMustExist : Member 'public System.Object System.Object AutoMapper.Context
MembersMustExist : Member 'public System.Boolean AutoMapper.ContextCacheKey.Equals(AutoMapper.ContextCacheKey)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Boolean AutoMapper.ContextCacheKey.op_Equality(AutoMapper.ContextCacheKey, AutoMapper.ContextCacheKey)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Boolean AutoMapper.ContextCacheKey.op_Inequality(AutoMapper.ContextCacheKey, AutoMapper.ContextCacheKey)' does not exist in the implementation but it does exist in the contract.
CannotSealType : Type 'AutoMapper.ExactMatchNamingConvention' is actually (has the sealed modifier) sealed in the implementation but not sealed in the contract.
MembersMustExist : Member 'public System.String AutoMapper.ExactMatchNamingConvention.ReplaceValue(System.Text.RegularExpressions.Match)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Text.RegularExpressions.Regex AutoMapper.ExactMatchNamingConvention.SplittingExpression.get()' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public TMappingExpression AutoMapper.IMappingExpressionBase<TSource, TDestination, TMappingExpression>.AsProxy()' is present in the implementation but not in the contract.
Expand All @@ -15,14 +16,19 @@ MembersMustExist : Member 'public System.String AutoMapper.INamingConvention.Rep
InterfacesShouldHaveSameMembers : Interface member 'public System.String[] AutoMapper.INamingConvention.Split(System.String)' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Text.RegularExpressions.Regex AutoMapper.INamingConvention.SplittingExpression.get()' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Text.RegularExpressions.Regex AutoMapper.INamingConvention.SplittingExpression.get()' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.Conventions.IMemberConfiguration> AutoMapper.IProfileConfiguration.MemberConfigurations' is present in the contract but not in the implementation.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.Conventions.IMemberConfiguration> AutoMapper.IProfileConfiguration.MemberConfigurations.get()' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.Conventions.IMemberConfiguration> AutoMapper.IProfileConfiguration.MemberConfigurations.get()' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.TypeMapConfiguration> AutoMapper.IProfileConfiguration.OpenTypeMapConfigs.get()' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.ITypeMapConfiguration> AutoMapper.IProfileConfiguration.OpenTypeMapConfigs.get()' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.ITypeMapConfiguration> AutoMapper.IProfileConfiguration.OpenTypeMapConfigs.get()' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.TypeMapConfiguration> AutoMapper.IProfileConfiguration.TypeMapConfigs.get()' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.ITypeMapConfiguration> AutoMapper.IProfileConfiguration.TypeMapConfigs.get()' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.ITypeMapConfiguration> AutoMapper.IProfileConfiguration.TypeMapConfigs.get()' does not exist in the implementation but it does exist in the contract.
CannotSealType : Type 'AutoMapper.LowerUnderscoreNamingConvention' is actually (has the sealed modifier) sealed in the implementation but not sealed in the contract.
MembersMustExist : Member 'public System.String AutoMapper.LowerUnderscoreNamingConvention.ReplaceValue(System.Text.RegularExpressions.Match)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Text.RegularExpressions.Regex AutoMapper.LowerUnderscoreNamingConvention.SplittingExpression.get()' does not exist in the implementation but it does exist in the contract.
CannotSealType : Type 'AutoMapper.PascalCaseNamingConvention' is actually (has the sealed modifier) sealed in the implementation but not sealed in the contract.
MembersMustExist : Member 'public System.String AutoMapper.PascalCaseNamingConvention.ReplaceValue(System.Text.RegularExpressions.Match)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Text.RegularExpressions.Regex AutoMapper.PascalCaseNamingConvention.SplittingExpression.get()' does not exist in the implementation but it does exist in the contract.
CannotRemoveAttribute : Attribute 'System.Runtime.CompilerServices.CompilerGeneratedAttribute' exists on 'AutoMapper.Profile.DestinationMemberNamingConvention.get()' in the contract but not the implementation.
Expand Down Expand Up @@ -50,8 +56,17 @@ CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMappe
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.UseExistingValueAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.ValueConverterAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.ValueResolverAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
TypesMustExist : Type 'AutoMapper.Configuration.Conventions.DefaultName' does not exist in the implementation but it does exist in the contract.
TypesMustExist : Type 'AutoMapper.Configuration.Conventions.IParentSourceToDestinationNameMapper' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Reflection.MemberInfo AutoMapper.Configuration.Conventions.ISourceToDestinationNameMapper.GetMatchingMemberInfo(AutoMapper.Internal.TypeDetails, System.Type, System.Type, System.String)' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Reflection.MemberInfo AutoMapper.Configuration.Conventions.ISourceToDestinationNameMapper.GetMatchingMemberInfo(AutoMapper.Internal.TypeDetails, System.Type, System.Type, System.String)' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Reflection.MemberInfo AutoMapper.Configuration.Conventions.ISourceToDestinationNameMapper.GetSourceMember(AutoMapper.Internal.TypeDetails, System.Type, System.Type, System.String)' is present in the implementation but not in the contract.
TypesMustExist : Type 'AutoMapper.Configuration.Conventions.ParentSourceToDestinationNameMapper' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Reflection.MemberInfo AutoMapper.Configuration.Conventions.PrePostfixName.GetMatchingMemberInfo(AutoMapper.Internal.TypeDetails, System.Type, System.Type, System.String)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Collections.Generic.List<System.String> AutoMapper.Configuration.Conventions.PrePostfixName.Postfixes.get()' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Collections.Generic.List<System.String> AutoMapper.Configuration.Conventions.PrePostfixName.Prefixes.get()' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public AutoMapper.Configuration.Conventions.ReplaceName AutoMapper.Configuration.Conventions.ReplaceName.AddReplace(System.String, System.String)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Reflection.MemberInfo AutoMapper.Configuration.Conventions.ReplaceName.GetMatchingMemberInfo(AutoMapper.Internal.TypeDetails, System.Type, System.Type, System.String)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Linq.Expressions.Expression System.Linq.Expressions.Expression AutoMapper.Execution.Member.Expression' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Linq.Expressions.Expression System.Linq.Expressions.Expression AutoMapper.Execution.Member.Target' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Reflection.MemberInfo System.Reflection.MemberInfo AutoMapper.Execution.Member.MemberInfo' does not exist in the implementation but it does exist in the contract.
Expand Down Expand Up @@ -94,4 +109,4 @@ MembersMustExist : Member 'public AutoMapper.Internal.TypePair AutoMapper.Intern
MembersMustExist : Member 'public System.Linq.IQueryable<TDestination> AutoMapper.QueryableExtensions.Extensions.Map<TSource, TDestination>(System.Linq.IQueryable<TSource>, System.Linq.IQueryable<TDestination>, AutoMapper.IConfigurationProvider)' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyCollection<System.Reflection.MemberInfo> AutoMapper.QueryableExtensions.MemberVisitor.MemberPath.get()' does not exist in the implementation but it does exist in the contract.
TypesMustExist : Type 'AutoMapper.QueryableExtensions.Impl.MemberAccessQueryMapperVisitor' does not exist in the implementation but it does exist in the contract.
Total Issues: 95
Total Issues: 110
174 changes: 174 additions & 0 deletions src/AutoMapper/Configuration/Conventions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
using AutoMapper.Internal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace AutoMapper.Configuration.Conventions
{
public interface ISourceToDestinationNameMapper
{
MemberInfo GetSourceMember(TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public class MemberConfiguration
{
NameSplitMember _nameSplitMember;
public INamingConvention SourceMemberNamingConvention { get; set; } = PascalCaseNamingConvention.Instance;
public INamingConvention DestinationMemberNamingConvention { get; set; } = PascalCaseNamingConvention.Instance;
public List<ISourceToDestinationNameMapper> NameToMemberMappers { get; } = new();
public bool IsMatch(ProfileMap options, TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch, List<MemberInfo> resolvers, bool isReverseMap)
{
var matchingMemberInfo = GetSourceMember(sourceTypeDetails, destType, destMemberType, nameToSearch);
if (matchingMemberInfo != null)
{
resolvers.Add(matchingMemberInfo);
return true;
}
return nameToSearch.Length == 0 || _nameSplitMember.IsMatch(options, sourceTypeDetails, destType, destMemberType, nameToSearch, resolvers, isReverseMap);
}
public MemberInfo GetSourceMember(TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch)
{
var sourceMember = sourceTypeDetails.GetMember(nameToSearch);
if (sourceMember != null)
{
return sourceMember;
}
foreach (var namedMapper in NameToMemberMappers)
{
if ((sourceMember = namedMapper.GetSourceMember(sourceTypeDetails, destType, destMemberType, nameToSearch)) != null)
{
return sourceMember;
}
}
return null;
}
public void Seal()
{
var isDefault = SourceMemberNamingConvention == PascalCaseNamingConvention.Instance && DestinationMemberNamingConvention == PascalCaseNamingConvention.Instance;
_nameSplitMember = isDefault ? new DefaultNameSplitMember() : new ConventionsNameSplitMember();
_nameSplitMember.Parent = this;
}
}
public class PrePostfixName : ISourceToDestinationNameMapper
{
public List<string> DestinationPrefixes { get; } = new();
public List<string> DestinationPostfixes { get; } = new();
public MemberInfo GetSourceMember(TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch)
{
MemberInfo member;
foreach (var possibleSourceName in TypeDetails.PossibleNames(nameToSearch, DestinationPrefixes, DestinationPostfixes))
{
if ((member = sourceTypeDetails.GetMember(possibleSourceName)) != null)
{
return member;
}
}
return null;
}
}
public class ReplaceName : ISourceToDestinationNameMapper
{
public List<MemberNameReplacer> MemberNameReplacers { get; } = new();
public MemberInfo GetSourceMember(TypeDetails sourceTypeDetails, Type destType, Type destMemberType, string nameToSearch)
{
var possibleSourceNames = PossibleNames(nameToSearch);
if (possibleSourceNames.Length == 0)
{
return null;
}
var possibleDestNames = sourceTypeDetails.ReadAccessors.Select(mi => (mi, possibles : PossibleNames(mi.Name))).ToArray();
foreach (var sourceName in possibleSourceNames)
{
foreach (var (mi, possibles) in possibleDestNames)
{
if (possibles.Contains(sourceName, StringComparer.OrdinalIgnoreCase))
{
return mi;
}
}
}
return null;
}
private string[] PossibleNames(string nameToSearch) =>
MemberNameReplacers.Select(r => nameToSearch.Replace(r.OriginalValue, r.NewValue))
.Concat(new[] { MemberNameReplacers.Aggregate(nameToSearch, (s, r) => s.Replace(r.OriginalValue, r.NewValue)), nameToSearch })
.ToArray();
}
[EditorBrowsable(EditorBrowsableState.Never)]
public readonly record struct MemberNameReplacer(string OriginalValue, string NewValue);
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract class NameSplitMember
{
public MemberConfiguration Parent { get; set; }
public abstract bool IsMatch(ProfileMap options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, List<MemberInfo> resolvers, bool isReverseMap);
}
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class DefaultNameSplitMember : NameSplitMember
{
public sealed override bool IsMatch(ProfileMap options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, List<MemberInfo> resolvers, bool isReverseMap)
{
MemberInfo matchingMemberInfo = null;
int index = 1;
for (; index < nameToSearch.Length; index++)
{
if (char.IsUpper(nameToSearch[index]) && Found())
{
return true;
}
}
return matchingMemberInfo != null && Found();
bool Found()
{
var first = nameToSearch[..index];
matchingMemberInfo = Parent.GetSourceMember(sourceType, destType, destMemberType, first);
if (matchingMemberInfo == null)
{
return false;
}
resolvers.Add(matchingMemberInfo);
var second = nameToSearch[index..];
var details = options.CreateTypeDetails(matchingMemberInfo.GetMemberType());
if (Parent.IsMatch(options, details, destType, destMemberType, second, resolvers, isReverseMap))
{
return true;
}
resolvers.RemoveAt(resolvers.Count - 1);
return false;
}
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class ConventionsNameSplitMember : NameSplitMember
{
public sealed override bool IsMatch(ProfileMap options, TypeDetails sourceType, Type destType, Type destMemberType, string nameToSearch, List<MemberInfo> resolvers, bool isReverseMap)
{
var destinationMemberNamingConvention = isReverseMap ? Parent.SourceMemberNamingConvention : Parent.DestinationMemberNamingConvention;
var matches = destinationMemberNamingConvention.Split(nameToSearch);
var length = matches.Length;
if (length < 2)
{
return false;
}
var sourceMemberNamingConvention = isReverseMap ? Parent.DestinationMemberNamingConvention : Parent.SourceMemberNamingConvention;
var separator = sourceMemberNamingConvention.SeparatorCharacter;
for (var index = 1; index <= length; index++)
{
var first = string.Join(separator, matches, 0, index);
var matchingMemberInfo = Parent.GetSourceMember(sourceType, destType, destMemberType, first);
if (matchingMemberInfo != null)
{
resolvers.Add(matchingMemberInfo);
var second = string.Join(separator, matches, index, length - index);
var details = options.CreateTypeDetails(matchingMemberInfo.GetMemberType());
if (Parent.IsMatch(options, details, destType, destMemberType, second, resolvers, isReverseMap))
{
return true;
}
resolvers.RemoveAt(resolvers.Count - 1);
}
}
return false;
}
}
}
Loading