Skip to content

Commit

Permalink
Failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogard committed Feb 26, 2014
1 parent c038178 commit 0d83f85
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/AutoMapper/QueryableExtensions.cs
Expand Up @@ -25,10 +25,7 @@ public static class Extensions
public static Expression<Func<TSource, TDestination>> CreateMapExpression<TSource, TDestination>(this IMappingEngine mappingEngine)
{
return (Expression<Func<TSource, TDestination>>)
_expressionCache.GetOrAdd(new TypePair(typeof(TSource), typeof(TDestination)), tp =>
{
return CreateMapExpression(mappingEngine, tp.SourceType, tp.DestinationType);
});
_expressionCache.GetOrAdd(new TypePair(typeof(TSource), typeof(TDestination)), tp => CreateMapExpression(mappingEngine, tp));
}


Expand Down Expand Up @@ -61,33 +58,33 @@ public static class Extensions
return new ProjectionExpression<TSource>(source, mappingEngine);
}

private static LambdaExpression CreateMapExpression(IMappingEngine mappingEngine, Type typeIn, Type typeOut)
private static LambdaExpression CreateMapExpression(IMappingEngine mappingEngine, TypePair typePair)
{
// this is the input parameter of this expression with name <variableName>
ParameterExpression instanceParameter = Expression.Parameter(typeIn, "dto");
ParameterExpression instanceParameter = Expression.Parameter(typePair.SourceType, "dto");

var total = CreateMapExpression(mappingEngine, typeIn, typeOut, instanceParameter);
var total = CreateMapExpression(mappingEngine, typePair, instanceParameter);

return Expression.Lambda(total, instanceParameter);
}

private static Expression CreateMapExpression(IMappingEngine mappingEngine, Type typeIn, Type typeOut, Expression instanceParameter)
private static Expression CreateMapExpression(IMappingEngine mappingEngine, TypePair typePair, Expression instanceParameter)
{
var typeMap = mappingEngine.ConfigurationProvider.FindTypeMapFor(typeIn, typeOut);
var typeMap = mappingEngine.ConfigurationProvider.FindTypeMapFor(typePair.SourceType, typePair.DestinationType);

if (typeMap == null)
{
const string MessageFormat = "Missing map from {0} to {1}. Create using Mapper.CreateMap<{0}, {1}>.";

var message = string.Format(MessageFormat, typeIn.Name, typeOut.Name);
var message = string.Format(MessageFormat, typePair.SourceType.Name, typePair.DestinationType.Name);

throw new InvalidOperationException(message);
}

var bindings = CreateMemberBindings(mappingEngine, typeIn, typeMap, instanceParameter);
var bindings = CreateMemberBindings(mappingEngine, typePair.SourceType, typeMap, instanceParameter);

Expression total = Expression.MemberInit(
Expression.New(typeOut),
Expression.New(typePair.DestinationType),
bindings.ToArray()
);

Expand Down Expand Up @@ -117,10 +114,11 @@ private static Expression CreateMapExpression(IMappingEngine mappingEngine, Type
Type sourceListType = null;
// is list

sourceListType = result.Type.GetGenericArguments().First();
sourceListType = result.Type.GetGenericArguments().First();
var listTypePair = new TypePair(sourceListType, destinationListType);

//var newVariableName = "t" + (i++);
var transformedExpression = CreateMapExpression(mappingEngine, sourceListType, destinationListType);
var transformedExpression = CreateMapExpression(mappingEngine, listTypePair);

MethodCallExpression selectExpression = Expression.Call(
typeof(Enumerable),
Expand Down Expand Up @@ -150,8 +148,7 @@ private static Expression CreateMapExpression(IMappingEngine mappingEngine, Type
propertyMap.DestinationPropertyType.BaseType != typeof(ValueType) &&
propertyMap.DestinationPropertyType.BaseType != typeof(Enum))
{
var transformedExpression = CreateMapExpression(mappingEngine, result.Type,
propertyMap.DestinationPropertyType,
var transformedExpression = CreateMapExpression(mappingEngine, new TypePair(result.Type, propertyMap.DestinationPropertyType),
result.ResolutionExpression);

bindExpression = Expression.Bind(destinationMember, transformedExpression);
Expand Down
67 changes: 67 additions & 0 deletions src/UnitTests/ExpressionBridge.cs
Expand Up @@ -232,6 +232,73 @@ public void List_of_abstract_should_be_mapped()
abstractProducts[0].Types[2].GetType().ShouldEqual(typeof (ProdTypeA));
}
#endif
}

#if !NETFX_CORE
namespace CircularReferences
{
public class A
{
public int AP1 { get; set; }
public string AP2 { get; set; }
public virtual B B { get; set; }
}

public class B
{
public B()
{
BP2 = new HashSet<A>();
}
public int BP1 { get; set; }
public virtual ICollection<A> BP2 { get; set; }
}

public class AEntity
{
public int AP1 { get; set; }
public string AP2 { get; set; }
public virtual BEntity B { get; set; }
}
public class BEntity
{
public BEntity()
{
BP2 = new HashSet<AEntity>();
}
public int BP1 { get; set; }
public virtual ICollection<AEntity> BP2 { get; set; }
}

public class When_mapping_circular_references : AutoMapperSpecBase
{
private IQueryable<BEntity> _bei;

protected override void Establish_context()
{
Mapper.CreateMap<BEntity, B>().MaxDepth(3);
Mapper.CreateMap<AEntity, A>().MaxDepth(3);
}

protected override void Because_of()
{
var be = new BEntity();
be.BP1 = 3;
be.BP2.Add(new AEntity() { AP1 = 1, AP2 = "hello", B = be });
be.BP2.Add(new AEntity() { AP1 = 2, AP2 = "two", B = be });

var belist = new List<BEntity>();
belist.Add(be);
_bei = belist.AsQueryable();
}

[Fact]
public void Should_not_throw_exception()
{
typeof(StackOverflowException).ShouldNotBeThrownBy(() => _bei.Project().To<B>());
}
}
}
#endif
}
}

0 comments on commit 0d83f85

Please sign in to comment.