Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegisterCustom should come before ComplexMapCondition #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
55 changes: 26 additions & 29 deletions ExpressMapper NET40/MappingServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,40 +121,34 @@ public Expression GetMemberMappingExpression(Expression left, Expression right,
var destType = destNullableType ?? left.Type;
var sourceType = sourceNullableType ?? right.Type;

if (ComplexMapCondition(sourceType, destType))
var customMapExpression = GetCustomMapExpression(right.Type, left.Type);
if (customMapExpression != null)
{
var customMapExpression = GetCustomMapExpression(right.Type, left.Type);
if (customMapExpression != null)
{
var srcExp = Expression.Variable(right.Type,
string.Format("{0}Src", Guid.NewGuid().ToString("N")));
var assignSrcExp = Expression.Assign(srcExp, right);
var srcExp = Expression.Variable(right.Type,
string.Format("{0}Src", Guid.NewGuid().ToString("N")));
var assignSrcExp = Expression.Assign(srcExp, right);

var destExp = Expression.Variable(left.Type,
string.Format("{0}Dst", Guid.NewGuid().ToString("N")));
var assignDestExp = Expression.Assign(destExp, left);
var destExp = Expression.Variable(left.Type,
string.Format("{0}Dst", Guid.NewGuid().ToString("N")));
var assignDestExp = Expression.Assign(destExp, left);

// try precise substitute visitor
var substituteParameterVisitor =
new PreciseSubstituteParameterVisitor(
new KeyValuePair<ParameterExpression, ParameterExpression>(
Expression.Variable(right.Type, "srcTyped"), srcExp),
new KeyValuePair<ParameterExpression, ParameterExpression>(
Expression.Variable(left.Type, "dstTyped"), destExp));
// try precise substitute visitor
var substituteParameterVisitor =
new PreciseSubstituteParameterVisitor(
new KeyValuePair<ParameterExpression, ParameterExpression>(
Expression.Variable(right.Type, "srcTyped"), srcExp),
new KeyValuePair<ParameterExpression, ParameterExpression>(
Expression.Variable(left.Type, "dstTyped"), destExp));

var blockExpression = substituteParameterVisitor.Visit(customMapExpression) as BlockExpression;
var blockExpression = substituteParameterVisitor.Visit(customMapExpression) as BlockExpression;

var assignResultExp = Expression.Assign(left, destExp);
var resultBlockExp = Expression.Block(new[] { srcExp, destExp }, assignSrcExp, assignDestExp, blockExpression, assignResultExp);
var assignResultExp = Expression.Assign(left, destExp);

var checkNullExp =
Expression.IfThenElse(Expression.Equal(right, Expression.Default(right.Type)),
Expression.Assign(left, Expression.Default(left.Type)), resultBlockExp);

var releaseExp = Expression.Block(new ParameterExpression[] { }, (right.Type.GetInfo().IsPrimitive || right.Type.GetInfo().IsValueType ? resultBlockExp : (Expression)checkNullExp));
return Expression.Block(new[] { srcExp, destExp }, assignSrcExp, assignDestExp, blockExpression, assignResultExp);
}

return releaseExp;
}
if (ComplexMapCondition(sourceType, destType))
{

var returnTypeDifferenceVisitor = new ReturnTypeDifferenceVisitor(right);
returnTypeDifferenceVisitor.Visit(right);
Expand Down Expand Up @@ -196,7 +190,9 @@ public Expression GetMemberMappingExpression(Expression left, Expression right,
destNullableType,
sourceNullableType);

var conditionalExpression = nullCheckNestedMemberVisitor.CheckNullExpression != null ? Expression.Condition(nullCheckNestedMemberVisitor.CheckNullExpression, Expression.Assign(left, Expression.Default(left.Type)), binaryExpression) : (Expression)binaryExpression;
var conditionalExpression = nullCheckNestedMemberVisitor.CheckNullExpression != null ?
Expression.Condition(nullCheckNestedMemberVisitor.CheckNullExpression,
Expression.Assign(left, Expression.Default(left.Type)), binaryExpression) : binaryExpression;

return conditionalExpression;
}
Expand Down Expand Up @@ -446,7 +442,8 @@ internal static Expression ConvertCollection(Type destPropType,
else
{
ctor = destPropType.GetInfo().GetConstructors(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(
ci => {
ci =>
{
var param = ci.GetParameters();
return param.Length == 1 && param[0].ParameterType.GetInfo().IsAssignableFrom(destList);
});
Expand Down
22 changes: 16 additions & 6 deletions ExpressMapper.Tests NET40/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public void ParallelPrecompileCollectionTest()
Parallel.Invoke(actions.ToArray());
}

[Test]
public void RegisterCustomStringToStringTest()
{
Mapper.Register<TestDefaultDecimalToStringViewModel, TestDefaultDecimalToStringViewModel>();
Mapper.RegisterCustom<string, string>(src => src ?? "");
var tst = new TestDefaultDecimalToStringViewModel();
var result = Mapper.Map<TestDefaultDecimalToStringViewModel, TestDefaultDecimalToStringViewModel>(tst);
Assert.AreEqual(result.TestString, "");
}

[Test]
public void DefaultPrimitiveTypePropertyToStringTest()
{
Expand All @@ -74,9 +84,9 @@ public void MemberMappingsHasHigherPriorityThanCaseSensetiveTest()
.Member(t => t.Enabled, s => s.enabled == "Y");
Mapper.Compile();

var source = new Source { enabled = "N" };
var result = Mapper.Map<Source, TargetViewModel>(source);
Assert.AreEqual(result.Enabled, false);
var source = new Source { enabled = "N" };
var result = Mapper.Map<Source, TargetViewModel>(source);
Assert.AreEqual(result.Enabled, false);
}

[Test]
Expand Down Expand Up @@ -293,7 +303,7 @@ public void HiddenInheritedMemberMap()
Assert.AreEqual(result, srcDst.Value);
}

private void MapBaseMember<T, TN>(IMemberConfiguration<T,TN> mapConfig)
private void MapBaseMember<T, TN>(IMemberConfiguration<T, TN> mapConfig)
where T : Gift
where TN : GiftViewModel
{
Expand Down Expand Up @@ -948,7 +958,7 @@ public void ExistingSrcCollGreater()
Assert.AreEqual(result.Collection.ElementAt(i), testResult.Item3.Collection.ElementAt(i));
}
}

[Test]
public void ExistingDestDestCollGreater()
{
Expand Down Expand Up @@ -1201,7 +1211,7 @@ public void MemberCaseSensitivityGlobalMapTest()
};

var typoCaseViewModel = Mapper.Map<TypoCase, TypoCaseViewModel>(typoCase);

Assert.AreEqual(typoCaseViewModel.Id, Guid.Empty);
Assert.AreEqual(typoCaseViewModel.Name, null);
Assert.AreEqual(typoCase.TestId, typoCaseViewModel.TestId);
Expand Down