

Public Dictionary Subst = new Dictionary() Internal class SubstExpressionVisitor : ExpressionVisitor Var visitor = new SubstExpressionVisitor Įxpression body = Expression.OrElse(a.Body, visitor.Visit(b.Body) ? throw new InvalidOperationException()) Īs you can see there is another class called SubstExpressionVisitor which we used to create our Expression tree easier. Return Expression.Lambda>(a.Body, a.Parameters) AND – extension for Expression Tree public static Expression> And(this Expression> a, Expression> b) To chain these two we will need to create two extension methods for Expression tree. Then we create expression tree and chain each filter using AND or OR. The whole idea is to add a bunch of filters as array of strings. Public FilterBuilder AddOrFilter(Expression> obj) Public FilterBuilder AddFilter(Expression> obj) Var filters = JsonConverter.TryParse>(filterValues) įoreach (var filter in filters.Where(x => !string.IsNullOrEmpty(x.FilterValue)))ĪddFilter(filter.FilterKey, filter.FilterValue) Public FilterBuilder AddFilters(string filterValues) Public FilterBuilder CanViewAll(CUserPrincipal user)ĪddFilter("CreatedByUserId", user.UserId) Public FilterBuilder AddFilter(string filterBy, string filter)īindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) Įxpression> obj = x => p.GetValue(x, null) != null & Generic FilterBuilder class public class FilterBuilder where T : class we do the query using our unit of work and repository class.we created filter expression tree based on generic FilterBuilder class.we created result object based on Customers class.Result.model = entityModels // not relevant, so I simplified it here not to confuse youĪs you can see we used FilterBuilder class to create filter based on bunch of string filterValues. X => x.Include(y => y.LastModifiedByUser)) public async Task GetList(string filterValues, string sortBy, OrderDirection orderDirection, int pageNumber, int pageSize, CUserPrincipal user) This way you will get better picture of usage. I ditched some things inside this method just to provide you the clean example. Let’s create imaginary Customer Controller with a method which returns filtered list of customers. Which means you can write something like this: SomeClass _someClass = new SomeClass() įor more info about implicit and explicit, please refer to: Basically you can convert object form one type to the other. It’s a conversion operator which always succeeds and never throws an exception. } Come again!? What’s implicit operator anyways? Return new Fitler(fb) // again, this is pretty simplified example! Public static implicit operator Filter(FilterBuilder fb) Simplified example 2: public class FilterBuilder Using it, we are ditching Build() method in favor of implicit conversion of an object. Or the other option is to use static implicit operator. Var filter = new FilterBuilder()./*some more method chaining*/.Build() Option 2 I skipped here some more methods in favor of simplifying things

Simplified example: public class FilterBuilder The final result of any builder pattern is to call BUILD() method which in return gives object of a class we are building. If you don’t, stay tuned for my next article! Where I’ll be writing about it and hopefully make it clear to you why and when should you use it!?īut firstly, let’s make use of builder pattern! Option 1 In order to create this kind of functionality you will need to have a good grasp about expression trees as well. Well, you do want to have the possibility to send from frontend as much filter values as you wish! It doesn’t make sense to write additional logic regarding each filter for itself, right!? To be more precise, we’ll build generic filter.

We will build a filter functionality using builder pattern. As there are already great Builder pattern articles (just google it by yourself), I’ll try to give you some more real life example. Using the Builder interface we control a steps or order in which we build the final object. Well it’s the best practice used to build a complex object in a easy, step by step approach. Builder pattern with expression trees in a real world, example, is the main topic of today! So what is builder pattern?
