I'm trying to figure out a way to structure my data so that it is model bindable. My Issue is that I have to create a query filter which can represent multiple expressions in data.
For example:
x => (x.someProperty == true && x.someOtherProperty == false) || x.UserId == 2
x => (x.someProperty && x.anotherProperty) || (x.userId == 3 && x.userIsActive)
I've created this structure which represents all of the expressions fine my Issue is how can I make this so it's property Model Bindable
public enum FilterCondition
{
Equals,
}
public enum ExpressionCombine
{
And = 0,
Or
}
public interface IFilterResolver<T>
{
Expression<Func<T, bool>> ResolveExpression();
}
public class QueryTreeNode<T> : IFilterResolver<T>
{
public string PropertyName { get; set; }
public FilterCondition FilterCondition { get; set; }
public string Value { get; set; }
public bool isNegated { get; set; }
public Expression<Func<T, bool>> ResolveExpression()
{
return this.BuildSimpleFilter();
}
}
//TODO: rename this class
public class QueryTreeBranch<T> : IFilterResolver<T>
{
public QueryTreeBranch(IFilterResolver<T> left, IFilterResolver<T> right, ExpressionCombine combinor)
{
this.Left = left;
this.Right = right;
this.Combinor = combinor;
}
public IFilterResolver<T> Left { get; set; }
public IFilterResolver<T> Right { get; set; }
public ExpressionCombine Combinor { get; set; }
public Expression<Func<T, bool>> ResolveExpression()
{
var leftExpression = Left.ResolveExpression();
var rightExpression = Right.ResolveExpression();
return leftExpression.Combine(rightExpression, Combinor);
}
}
My left an right members just need to be able to be resolved to an IResolvable, but the model binder only binds to concrete types. I know I can write a custom model binder but I'd prefer to just have a structure that works.
I know I can pass json as a solutions but as a requirement I can't
Is there a way I can refine this structure so that it can still represent all simple expression while being Model Bindable? or is there an easy way I can apply this structure so that it works with the model binder?
EDIT Just in case anyone is wondering, my expression builder has a whitelist of member expressions that it it filters on. The dynamic filtering work I just looking for a way to bind this structure naturally so that my Controller can take in a QueryTreeBranch or take in a structure which accurately represent the same data.
public class FilterController
{
[HttpGet]
[ReadRoute("")]
public Entity[] GetList(QueryTreeBranch<Entity> queryRoot)
{
//queryRoot no bind :/
}
}
Currently the IFilterResolver has 2 implementations which need to be chosen dynamically based on the data passed
I'm looking for a solution closest to out of the box WebApi / MVC framework. Preferable one that does NOT require me to adapt the input to another structure in order generate my expression
DbContext.SqlQuery
which would allow you to send(someProperty = true AND someOtherProperty = false) OR UserId = 2
– CaeomaEntity
object definition? – Bissonnette