I can successfully replace simple parameter types in a lambda expression thanks to some answers on a previous question but I cannot figure out how to replace parameters from an incoming lambda to a nested parameter.
Consider the following objects:
public class DtoColour {
public DtoColour(string name)
{
Name = name;
}
public string Name { get; set; }
public ICollection<DtoFavouriteColour> FavouriteColours { get; set; }
}
public class DtoPerson
{
public DtoPerson(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
FavouriteColours = new Collection<DtoFavouriteColour>();
}
public string FirstName { get; private set; }
public string LastName { get; private set; }
public ICollection<DtoFavouriteColour> FavouriteColours { get; set; }
}
public class DtoFavouriteColour
{
public DtoColour Colour { get; set; }
public DtoPerson Person { get; set; }
}
public class DomainColour {
public DomainColour(string name)
{
Name = name;
}
public string Name { get; set; }
public ICollection<DomainPerson> People { get; set; }
}
public class DomainPerson {
public DomainPerson(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
Colours = new Collection<DomainColour>();
}
public string FirstName { get; private set; }
public string LastName { get; private set; }
public ICollection<DomainColour> Colours { get; set; }
}
and a Repository:
public class ColourRepository {
private IList<DtoColour> Colours { get; set; }
public ColourRepository()
{
var favColours = new Collection<DtoFavouriteColour>
{
new DtoFavouriteColour() { Person = new DtoPerson("Peter", "Parker") },
new DtoFavouriteColour() { Person = new DtoPerson("John", "Smith") },
new DtoFavouriteColour() { Person = new DtoPerson("Joe", "Blogs") }
};
Colours = new List<DtoColour>
{
new DtoColour("Red") { FavouriteColours = favColours },
new DtoColour("Blue"),
new DtoColour("Yellow")
};
}
public IEnumerable<DomainColour> GetWhere(Expression<Func<DomainColour, bool>> predicate)
{
var coonvertedPred = MyExpressionVisitor.Convert(predicate);
return Colours.Where(coonvertedPred).Select(c => new DomainColour(c.Name)).ToList();
}
}
and finally an expression visitor which should convert the predicate into the correct one for the Dto Models
public class MyExpressionVisitor : ExpressionVisitor
{
private ReadOnlyCollection<ParameterExpression> _parameters;
public static Func<DtoColour, bool> Convert<T>(Expression<T> root)
{
var visitor = new MyExpressionVisitor();
var expression = (Expression<Func<DtoColour, bool>>)visitor.Visit(root);
return expression.Compile();
}
protected override Expression VisitParameter(ParameterExpression node)
{
var param = _parameters?.FirstOrDefault(p => p.Name == node.Name);
if (param != null)
{
return param;
}
if(node.Type == typeof(DomainColour))
{
return Expression.Parameter(typeof(DtoColour), node.Name);
}
if (node.Type == typeof(DomainPerson))
{
return Expression.Parameter(typeof(DtoFavouriteColour), node.Name);
}
return node;
}
protected override Expression VisitLambda<T>(Expression<T> node)
{
_parameters = VisitAndConvert<ParameterExpression>(node.Parameters, "VisitLambda");
return Expression.Lambda(Visit(node.Body), _parameters);
}
protected override Expression VisitMember(MemberExpression node)
{
var exp = Visit(node.Expression);
if (node.Member.DeclaringType == typeof(DomainColour))
{
if (node.Type == typeof(ICollection<DomainPerson>))
{
return Expression.MakeMemberAccess(exp, typeof(DtoColour).GetProperty("FavouriteColours"));
}
return Expression.MakeMemberAccess(exp, typeof(DtoColour).GetProperty(node.Member.Name));
}
if (node.Member.DeclaringType == typeof(DomainPerson))
{
var nested = Expression.MakeMemberAccess(exp, typeof(DtoFavouriteColour).GetProperty("Person"));
return Expression.MakeMemberAccess(nested, typeof(DtoPerson).GetProperty(node.Member.Name));
}
return base.VisitMember(node);
}
}
Currently I get the following Exception
[System.ArgumentException: Expression of type 'System.Collections.Generic.ICollection
1[ExpressionVisitorTests.DtoFavouriteColour]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable
1[ExpressionVisitorTests.DomainPerson]' of method 'Boolean Any[DomainPerson](System.Collections.Generic.IEnumerable1[ExpressionVisitorTests.DomainPerson], System.Func
2[ExpressionVisitorTests.DomainPerson,System.Boolean])']
Here is a dotnetfiddle of it not working.
Thank in advance for any help.
DtoColour
andDtoPerson
have a property namedFavoriteColours
. Should those collections be in sync? Anyway makes no sense for an object to have a property that is not actually a property of an object. Collection of people who like a particular color is not a property of that color. Collection of colors that a person likes might be a property of a person, but I would remove that too, and just have one common collection of all "person likes color" relationships. – Ethnocentrism