Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions AgileMapper.UnitTests/WhenMappingToConstructors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ public void ShouldIgnoreACopyConstructor()
result.StringValue.ShouldBe("Copy!");
}

// See https://github.com/agileobjects/AgileMapper/issues/139
[Fact]
public void ShouldPopulateMembersMatchingUnusedConstructorParameters()
{
var source = new { Value1 = 123 };
var result = Mapper.Map(source).ToANew<MultipleUnusedConstructors<int, int>>();

result.Value1.ShouldBe(123);
}

#region Helper Classes

// ReSharper disable ClassNeverInstantiated.Local
Expand All @@ -124,6 +134,23 @@ public MultipleConstructors(T1 value1, T2 value2)
public T2 Value2 { get; }
}

private class MultipleUnusedConstructors<T1, T2>
{
public MultipleUnusedConstructors()
{
}

public MultipleUnusedConstructors(T1 value1, T2 value2)
{
Value1 = value1;
Value2 = value2;
}

public T1 Value1 { get; set; }

public T2 Value2 { get; set; }
}

private class CopyConstructor
{
public CopyConstructor()
Expand Down
410 changes: 262 additions & 148 deletions AgileMapper.UnitTests/WhenValidatingMappings.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ private void ThrowIfRedundantSourceMember(ConfiguredLambdaInfo valueLambdaInfo,
return;
}

var targetMemberType = (targetMember.LeafMember.MemberType == MemberType.ConstructorParameter)
? "constructor parameter"
: "member";
var targetMemberType = targetMember.IsConstructorParameter() ? "constructor parameter" : "member";

throw new MappingConfigurationException(string.Format(
CultureInfo.InvariantCulture,
Expand Down
17 changes: 12 additions & 5 deletions AgileMapper/DataSources/DataSourceSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,21 @@ private Expression BuildValueExpression()

for (var i = _dataSources.Count - 1; i >= 0;)
{
var isFirstDataSource = value == default(Expression);
var dataSource = _dataSources[i--];

value = dataSource.AddPreConditionIfNecessary(value == default(Expression)
? dataSource.Value
: Expression.Condition(
var dataSourceValue = dataSource.IsConditional
? Expression.Condition(
dataSource.Condition,
dataSource.Value.GetConversionTo(value.Type),
value));
isFirstDataSource
? dataSource.Value
: dataSource.Value.GetConversionTo(value.Type),
isFirstDataSource
? dataSource.Value.Type.ToDefaultExpression()
: value)
: dataSource.Value;

value = dataSource.AddPreConditionIfNecessary(dataSourceValue);
}

return value;
Expand Down
27 changes: 12 additions & 15 deletions AgileMapper/MappingDataExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
namespace AgileObjects.AgileMapper
{
#if NET35
using Microsoft.Scripting.Ast;
#else
using System.Linq.Expressions;
#endif
using System.Collections.Generic;
using DataSources;
using Extensions.Internal;
using Members;
using ObjectPopulation;
using ObjectPopulation.ComplexTypes;

internal static class MappingDataExtensions
{
public static bool IsStandalone(this IObjectMappingData mappingData)
=> mappingData.IsRoot || mappingData.MappingTypes.RuntimeTypesNeeded;

public static bool IsTargetConstructable(this IObjectMappingData mappingData)
=> mappingData.GetTargetObjectCreation() != null;
=> GetTargetObjectCreationInfos(mappingData).Any();

public static IList<IBasicConstructionInfo> GetTargetObjectCreationInfos(this IObjectMappingData mappingData)
{
return mappingData
.MapperData
.MapperContext
.ConstructionFactory
.GetTargetObjectCreationInfos(mappingData);
}

public static bool IsConstructableFromToTargetDataSource(this IObjectMappingData mappingData)
=> mappingData.GetToTargetDataSourceOrNullForTargetType() != null;
Expand Down Expand Up @@ -48,15 +54,6 @@ public static IConfiguredDataSource GetToTargetDataSourceOrNullForTargetType(thi
return null;
}

public static Expression GetTargetObjectCreation(this IObjectMappingData mappingData)
{
return mappingData
.MapperData
.MapperContext
.ConstructionFactory
.GetNewObjectCreation(mappingData);
}

public static bool HasSameTypedConfiguredDataSource(this IObjectMappingData mappingData)
{
return
Expand Down
2 changes: 1 addition & 1 deletion AgileMapper/Members/Dictionaries/DictionaryTargetMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private bool CreateNonDictionaryChildMembers(Type sourceType)
// entry and we're mapping from a source of type object, we switch from
// mapping to flattened entries to mapping entire objects:
return HasObjectEntries &&
LeafMember.IsEnumerableElement() &&
this.IsEnumerableElement() &&
(MemberChain[Depth - 2] == _rootDictionaryMember.LeafMember) &&
(sourceType == typeof(object));
}
Expand Down
7 changes: 7 additions & 0 deletions AgileMapper/Members/MemberExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,16 @@ public static Expression GetQualifiedAccess(this IEnumerable<Member> memberChain
(accessSoFar, member) => member.GetAccess(accessSoFar));
}

[DebuggerStepThrough]
public static bool IsEnumerableElement(this QualifiedMember member) => member.LeafMember.IsEnumerableElement();

[DebuggerStepThrough]
public static bool IsEnumerableElement(this Member member) => member.MemberType == MemberType.EnumerableElement;

[DebuggerStepThrough]
public static bool IsConstructorParameter(this QualifiedMember member)
=> member.LeafMember.MemberType == MemberType.ConstructorParameter;

public static IList<string> ExtendWith(
this ICollection<string> parentJoinedNames,
string[] memberMatchingNames,
Expand Down
27 changes: 1 addition & 26 deletions AgileMapper/Members/MemberMapperDataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,31 +187,6 @@ public static void RegisterTargetMemberDataSourcesIfRequired(
mapperData.Parent.DataSourcesByTargetMember.Add(mapperData.TargetMember, dataSources);
}

public static bool TargetMemberIsUnmappable(this IMemberMapperData mapperData, out string reason)
{
if (!mapperData.RuleSet.Settings.AllowSetMethods &&
(mapperData.TargetMember.LeafMember.MemberType == MemberType.SetMethod))
{
reason = "Set methods are unsupported by rule set '" + mapperData.RuleSet.Name + "'";
return true;
}

if (mapperData.TargetMember.LeafMember.HasMatchingCtorParameter &&
((mapperData.Parent?.IsRoot != true) ||
!mapperData.RuleSet.Settings.RootHasPopulatedTarget))
{
reason = "Expected to be populated by constructor parameter";
return true;
}

return TargetMemberIsUnmappable(
mapperData,
mapperData.TargetMember,
md => md.MapperContext.UserConfigurations.QueryDataSourceFactories(md),
mapperData.MapperContext.UserConfigurations,
out reason);
}

public static bool TargetMemberIsUnmappable<TTMapperData>(
this TTMapperData mapperData,
QualifiedMember targetMember,
Expand Down Expand Up @@ -250,7 +225,7 @@ public static bool TargetMemberIsUnmappable<TTMapperData>(

[DebuggerStepThrough]
public static bool TargetMemberIsEnumerableElement(this IBasicMapperData mapperData)
=> mapperData.TargetMember.LeafMember.IsEnumerableElement();
=> mapperData.TargetMember.IsEnumerableElement();

[DebuggerStepThrough]
public static bool TargetMemberHasInitAccessibleValue(this IMemberMapperData mapperData)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace AgileObjects.AgileMapper.Members.Population
{
using System.Collections.Generic;
using Extensions.Internal;
#if NET35
using Microsoft.Scripting.Ast;
#else
using System.Linq.Expressions;
#endif
using Extensions.Internal;

internal abstract class MemberPopulationFactoryBase : IMemberPopulationFactory
{
Expand Down
55 changes: 47 additions & 8 deletions AgileMapper/Members/Population/MemberPopulatorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
namespace AgileObjects.AgileMapper.Members.Population
namespace AgileObjects.AgileMapper.Members.Population
{
using System;
using System.Collections.Generic;
#if NET35
using Microsoft.Scripting.Ast;
#else
using System.Linq.Expressions;
#endif
using Configuration;
using DataSources.Finders;
using Extensions;
using Extensions.Internal;
using Members;
using ObjectPopulation;
#if NET35
using Microsoft.Scripting.Ast;
#else
using System.Linq.Expressions;
#endif

internal class MemberPopulatorFactory
{
Expand Down Expand Up @@ -42,7 +42,7 @@ public IEnumerable<IMemberPopulator> Create(IObjectMappingData mappingData)
{
return memberPopulation;
}

return null;
})
.WhereNotNull();
Expand All @@ -52,7 +52,7 @@ private static IMemberPopulator Create(QualifiedMember targetMember, IObjectMapp
{
var childMapperData = new ChildMemberMapperData(targetMember, mappingData.MapperData);

if (childMapperData.TargetMemberIsUnmappable(out var reason))
if (TargetMemberIsUnmappable(childMapperData, mappingData, out var reason))
{
return MemberPopulator.Unmappable(childMapperData, reason);
}
Expand All @@ -76,6 +76,45 @@ private static IMemberPopulator Create(QualifiedMember targetMember, IObjectMapp
return MemberPopulator.WithRegistration(childMappingData, dataSources, populateCondition);
}

private static bool TargetMemberIsUnmappable(
IMemberMapperData mapperData,
IObjectMappingData mappingData,
out string reason)
{
if (!mapperData.RuleSet.Settings.AllowSetMethods &&
(mapperData.TargetMember.LeafMember.MemberType == MemberType.SetMethod))
{
reason = "Set methods are unsupported by rule set '" + mapperData.RuleSet.Name + "'";
return true;
}

if (TargetMemberWillBePopulatedByCtor(mapperData, mappingData))
{
reason = "Expected to be populated by constructor parameter";
return true;
}

return mapperData.TargetMemberIsUnmappable(
mapperData.TargetMember,
md => md.MapperContext.UserConfigurations.QueryDataSourceFactories(md),
mapperData.MapperContext.UserConfigurations,
out reason);
}

private static bool TargetMemberWillBePopulatedByCtor(IMemberMapperData mapperData, IObjectMappingData mappingData)
{
if (!mapperData.TargetMember.LeafMember.HasMatchingCtorParameter ||
(mapperData.RuleSet.Settings.RootHasPopulatedTarget && (mapperData.Parent?.IsRoot == true)))
{
return false;
}

var creationInfos = mappingData.GetTargetObjectCreationInfos();

return creationInfos.Any() &&
creationInfos.All(ci => ci.IsUnconditional && ci.HasCtorParameterFor(mapperData.TargetMember.LeafMember));
}

private static bool TargetMemberIsUnconditionallyIgnored(
IMemberMapperData mapperData,
out ConfiguredIgnoredMember configuredIgnore,
Expand Down
5 changes: 1 addition & 4 deletions AgileMapper/Members/QualifiedMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ private QualifiedMember(Member leafMember, MapperContext mapperContext)
_childMemberCache = mapperContext.Cache.CreateNew<Member, QualifiedMember>(default(HashCodeComparer<Member>));
}

RegistrationName = (LeafMember.MemberType != MemberType.ConstructorParameter)
? Name
: "ctor:" + Name;

RegistrationName = this.IsConstructorParameter() ? "ctor:" + Name : Name;
IsReadOnly = IsReadable && !leafMember.IsWriteable;
}

Expand Down
Loading