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
we don't need to create As type maps
  • Loading branch information
lbargaoanu committed Aug 11, 2022
commit 00d29ee6605f57a080ee20ff40eccb39a4da8026
20 changes: 4 additions & 16 deletions src/AutoMapper/Configuration/MapperConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@ public interface IConfigurationProvider
/// Dry run all configured type maps and throw <see cref="AutoMapperConfigurationException"/> for each problem
/// </summary>
void AssertConfigurationIsValid();

/// <summary>
/// Create a mapper instance based on this configuration. Mapper instances are lightweight and can be created as needed.
/// </summary>
/// <returns>The mapper instance</returns>
IMapper CreateMapper();

/// <summary>
/// Create a mapper instance with the specified service constructor to be used for resolvers and type converters.
/// </summary>
/// <param name="serviceCtor">Service factory to create services</param>
/// <returns>The mapper instance</returns>
IMapper CreateMapper(Func<Type, object> serviceCtor);

/// <summary>
/// Builds the execution plan used to map the source to destination.
/// Useful to understand what exactly is happening during mapping.
Expand All @@ -41,7 +38,6 @@ public interface IConfigurationProvider
/// <param name="destinationType">the runtime type of the destination object</param>
/// <returns>the execution plan</returns>
LambdaExpression BuildExecutionPlan(Type sourceType, Type destinationType);

/// <summary>
/// Compile all underlying mapping expressions to cached delegates.
/// Use if you want AutoMapper to compile all mappings up front instead of deferring expression compilation for each first map.
Expand All @@ -51,7 +47,6 @@ public interface IConfigurationProvider
public class MapperConfiguration : IGlobalConfiguration
{
private static readonly MethodInfo MappingError = typeof(MapperConfiguration).GetMethod(nameof(GetMappingError));

private readonly IObjectMapper[] _mappers;
private readonly Dictionary<TypePair, TypeMap> _configuredMaps;
private readonly Dictionary<TypePair, TypeMap> _resolvedMaps;
Expand All @@ -68,7 +63,6 @@ public class MapperConfiguration : IGlobalConfiguration
private readonly Func<Type, object> _serviceCtor;
private readonly bool _sealed;
private readonly bool _hasOpenMaps;

public MapperConfiguration(MapperConfigurationExpression configurationExpression)
{
var configuration = (IGlobalConfigurationExpression)configurationExpression;
Expand Down Expand Up @@ -129,15 +123,7 @@ void Seal()
var derivedMaps = new List<TypeMap>();
foreach (var typeMap in _configuredMaps.Values)
{
if (typeMap.DestinationTypeOverride != null)
{
var derivedMap = globalConfiguration.GetIncludedTypeMap(typeMap.AsPair());
_resolvedMaps[typeMap.Types] = derivedMap;
}
else
{
_resolvedMaps[typeMap.Types] = typeMap;
}
_resolvedMaps[typeMap.Types] = typeMap;
derivedMaps.Clear();
GetDerivedTypeMaps(typeMap, derivedMaps);
foreach (var derivedMap in derivedMaps)
Expand Down Expand Up @@ -387,7 +373,7 @@ TypeMap FindClosedGenericMap(TypePair typePair)
ProfileMap profile;
TypeMap cachedMap;
TypePair closedTypes;
if (userMap != null && userMap.DestinationTypeOverride == null)
if (userMap != null)
{
genericMapConfig = userMap.Profile.GetGenericMap(userMap.Types);
profile = userMap.Profile;
Expand Down Expand Up @@ -475,5 +461,7 @@ void IGlobalConfiguration.AssertConfigurationIsValid(string profileName)
}
void IGlobalConfiguration.AssertConfigurationIsValid<TProfile>() => this.Internal().AssertConfigurationIsValid(typeof(TProfile).FullName);
void IGlobalConfiguration.Seal(TypeMap typeMap) => typeMap.Seal(this, _typeMapsPath);
void IGlobalConfiguration.RegisterAsMap(ITypeMapConfiguration typeMapConfiguration) =>
_resolvedMaps[typeMapConfiguration.Types] = GetIncludedTypeMap(new(typeMapConfiguration.SourceType, typeMapConfiguration.DestinationTypeOverride));
}
}
21 changes: 11 additions & 10 deletions src/AutoMapper/Configuration/MappingExpressionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface ITypeMapConfiguration
void Configure(TypeMap typeMap, List<MemberInfo> sourceMembers);
Type SourceType { get; }
Type DestinationType { get; }
Type DestinationTypeOverride { get; }
bool IsReverseMap { get; }
TypePair Types { get; }
ITypeMapConfiguration ReverseTypeMap { get; }
Expand All @@ -41,6 +42,7 @@ protected MappingExpressionBase(MemberList memberList, TypePair types)
_memberList = memberList;
_types = types;
}
public Type DestinationTypeOverride { get; private set; }
protected bool Projection { get; set; }
public TypePair Types => _types;
public bool IsReverseMap { get; set; }
Expand All @@ -56,6 +58,15 @@ protected MappingExpressionBase(MemberList memberList, TypePair types)
protected List<IPropertyMapConfiguration> MemberConfigurations => _memberConfigurations ??= new();
protected List<ISourceMemberConfiguration> SourceMemberConfigurations => _sourceMemberConfigurations ??= new();
protected List<ICtorParameterConfiguration> CtorParamConfigurations => _ctorParamConfigurations ??= new();
public void As(Type typeOverride)
{
if (typeOverride == DestinationType)
{
throw new InvalidOperationException("As must specify a derived type, not " + DestinationType);
}
typeOverride.CheckIsDerivedFrom(DestinationType);
DestinationTypeOverride = typeOverride;
}
public void Configure(TypeMap typeMap, List<MemberInfo> sourceMembers)
{
TypeMap = typeMap;
Expand Down Expand Up @@ -369,16 +380,6 @@ public TMappingExpression ForSourceMember(string sourceMemberName, Action<ISourc
return this as TMappingExpression;
}

public void As(Type typeOverride)
{
if (typeOverride == DestinationType)
{
throw new InvalidOperationException("As must specify a derived type, not " + DestinationType);
}
typeOverride.CheckIsDerivedFrom(DestinationType);
TypeMapActions.Add(tm => tm.DestinationTypeOverride = typeOverride);
}

public TMappingExpression ConstructUsing(Expression<Func<TSource, TDestination>> ctor) => ConstructUsingCore(ctor);

private TMappingExpression ConstructUsingCore(LambdaExpression ctor)
Expand Down
1 change: 1 addition & 0 deletions src/AutoMapper/Internal/InternalApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public interface IGlobalConfiguration : IConfigurationProvider
TypeMap GetIncludedTypeMap(Type sourceType, Type destinationType);
TypeMap[] GetIncludedTypeMaps(IReadOnlyCollection<TypePair> includedTypes);
void Seal(TypeMap typeMap);
void RegisterAsMap(ITypeMapConfiguration typeMapConfiguration);
List<MemberInfo> SourceMembers { get; }
}
[EditorBrowsable(EditorBrowsableState.Never)]
Expand Down
38 changes: 24 additions & 14 deletions src/AutoMapper/ProfileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,42 @@ public TypeDetails CreateTypeDetails(Type type)
public void Register(IGlobalConfiguration configurationProvider)
{
foreach (var config in _typeMapConfigs)
{
BuildTypeMap(configurationProvider, config);
if (config.ReverseTypeMap != null)
{
if (config.DestinationTypeOverride == null)
{
BuildTypeMap(configurationProvider, config.ReverseTypeMap);
BuildTypeMap(configurationProvider, config);
if (config.ReverseTypeMap != null)
{
BuildTypeMap(configurationProvider, config.ReverseTypeMap);
}
}
}
}
private void BuildTypeMap(IGlobalConfiguration configurationProvider, ITypeMapConfiguration config)
{
var sourceMembers = configurationProvider.SourceMembers;
var typeMap = new TypeMap(config.SourceType, config.DestinationType, this, config, sourceMembers);
config.Configure(typeMap, sourceMembers);
configurationProvider.RegisterTypeMap(typeMap);
}
public void Configure(IGlobalConfiguration configurationProvider)
{
foreach (var typeMapConfiguration in _typeMapConfigs)
{
Configure(typeMapConfiguration, configurationProvider);
if (typeMapConfiguration.ReverseTypeMap != null)
if (typeMapConfiguration.DestinationTypeOverride == null)
{
Configure(typeMapConfiguration.ReverseTypeMap, configurationProvider);
Configure(typeMapConfiguration, configurationProvider);
if (typeMapConfiguration.ReverseTypeMap != null)
{
Configure(typeMapConfiguration.ReverseTypeMap, configurationProvider);
}
}
else
{
configurationProvider.RegisterAsMap(typeMapConfiguration);
}
}
}
private void BuildTypeMap(IGlobalConfiguration configurationProvider, ITypeMapConfiguration config)
{
var sourceMembers = configurationProvider.SourceMembers;
var typeMap = new TypeMap(config.SourceType, config.DestinationType, this, config, sourceMembers);
config.Configure(typeMap, sourceMembers);
configurationProvider.RegisterTypeMap(typeMap);
}
private void Configure(ITypeMapConfiguration typeMapConfiguration, IGlobalConfiguration configurationProvider)
{
var typeMap = typeMapConfiguration.TypeMap;
Expand Down
23 changes: 2 additions & 21 deletions src/AutoMapper/TypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,6 @@ internal bool CanConstructorMap() => Profile.ConstructorMappingEnabled && !Desti
public LambdaExpression CustomMapExpression => TypeConverter?.ProjectToExpression;
public LambdaExpression CustomCtorFunction { get => _details?.CustomCtorFunction; set => Details.CustomCtorFunction = value; }
public LambdaExpression CustomCtorExpression => CustomCtorFunction?.Parameters.Count == 1 ? CustomCtorFunction : null;
public Type DestinationTypeOverride
{
get => _details?.DestinationTypeOverride;
set
{
Details.DestinationTypeOverride = value;
_sealed = true;
}
}
public bool IncludeAllDerivedTypes { get => (_details?.IncludeAllDerivedTypes).GetValueOrDefault(); set => Details.IncludeAllDerivedTypes = value; }
public MemberList ConfiguredMemberList
{
Expand Down Expand Up @@ -138,7 +129,7 @@ public IEnumerable<MemberMap> MemberMaps
public bool CustomConstruction => CustomCtorFunction != null;
public bool HasTypeConverter => TypeConverter != null;
public TypeConverter TypeConverter { get; set; }
public bool ShouldCheckForValid => !HasTypeConverter && DestinationTypeOverride == null && ConfiguredMemberList != MemberList.None;
public bool ShouldCheckForValid => ConfiguredMemberList != MemberList.None && !HasTypeConverter;
public LambdaExpression[] IncludedMembers { get => _details?.IncludedMembers ?? Array.Empty<LambdaExpression>(); set => Details.IncludedMembers = value; }
public string[] IncludedMembersNames { get => _details?.IncludedMembersNames ?? Array.Empty<string>(); set => Details.IncludedMembersNames = value; }
public IReadOnlyCollection<IncludedMember> IncludedMembersTypeMaps => (_details?.IncludedMembersTypeMaps).NullCheck();
Expand Down Expand Up @@ -196,7 +187,6 @@ public PropertyMap FindOrCreatePropertyMapFor(MemberInfo destinationProperty, Ty
AddPropertyMap(propertyMap);
return propertyMap;
}
public TypePair AsPair() => new(SourceType, DestinationTypeOverride);
private void CheckDifferent(TypePair types)
{
if (types == Types)
Expand Down Expand Up @@ -280,15 +270,7 @@ internal void CopyInheritedMapsTo(TypeMap typeMap)
}
_details.CopyInheritedMapsTo(typeMap);
}
public void CloseGenerics(ITypeMapConfiguration openMapConfig, TypePair closedTypes)
{
TypeConverter?.CloseGenerics(openMapConfig, closedTypes);
if (DestinationTypeOverride is { IsGenericTypeDefinition: true })
{
var neededParameters = DestinationTypeOverride.GenericParametersCount();
DestinationTypeOverride = DestinationTypeOverride.MakeGenericType(closedTypes.DestinationType.GenericTypeArguments.Take(neededParameters).ToArray());
}
}
public void CloseGenerics(ITypeMapConfiguration openMapConfig, TypePair closedTypes) => TypeConverter?.CloseGenerics(openMapConfig, closedTypes);
public bool AddMemberMap(IncludedMember includedMember) => Details.AddMemberMap(includedMember);
public PathMap FindOrCreatePathMapFor(LambdaExpression destinationExpression, MemberPath path, TypeMap typeMap) =>
Details.FindOrCreatePathMapFor(destinationExpression, path, typeMap);
Expand All @@ -305,7 +287,6 @@ class TypeMapDetails
public bool IncludeAllDerivedTypes;
public LambdaExpression CustomCtorFunction;
public MemberList ConfiguredMemberList;
public Type DestinationTypeOverride;
public HashSet<LambdaExpression> AfterMapActions { get; private set; }
public HashSet<LambdaExpression> BeforeMapActions { get; private set; }
public HashSet<TypePair> IncludedDerivedTypes { get; private set; }
Expand Down