3

Working with EPiServer Find and trying to build a generic facet funcionality for it to simplify managing which facet should be enabled. I would like to construct two generic methods, one for adding active filters to perform the hits search and one to perform the available facet filters remaining.

The first method will perform the following (specific code for brand filter):

var brandFilter = client.BuildFilter<FashionProduct>();
foreach (var facet in SelectedGroup.Facets.Where(x => x.Selected))
{
    brandFilter =  brandFilter.Or(x => x.Brand.Match(facet.Key));
}
query = query.Filter(brandFilter);

I would like to be able to call it in a generic way so I could base the available facets on some simple list of strings or objects. Like this:

query = AddFilterFacet<FashionProduct>(query, "Brand", SelectedGroup.Facets)

So the method would take the type of object to filter on, the query to append the filters on, the name of the property to filter on and the list of values to add.

The second method is similar but relates more to perform the following:

facetQuery = facetQuery.TermsFacetFor(x => x.Brand)
...
var brandFacets = facetResult.TermsFacetFor(x => x.Brand).Terms;

Is it possible to build this kind of functionality? The biggest questionmark I have is how to translate the "Brand" input string to be the Brand Property in x => x.Brand

private void AddFilterFacet<T>(IClient client, ref ITypeSearch<T> query, string propertyName, List<FacetOption> facets)
{
    var filter = client.BuildFilter<T>();
    foreach (var facet in facets)
    {
        filter = filter.Or(x => x.????.Match(facet.Key));
    }
    query = query.Filter(filter);
}

The .Or method takes a

System.Linq.Expressions.Expression<Func<T, Find.Api.Querying.Filter>>

so perhaps something can be used to make a proper generic call to it

Hyzac
  • 475
  • 3
  • 16

1 Answers1

0

It's definitely possible to create generic lambda expressions, it's just not easy and requires a lot of reflection code.

I haven't done it in a while, but maybe if you look at the code i created for something similar a while ago (Generic lambda expressions) it'll help. I'm sure someone with fresher experience will help you out here soon enough.

Decimal precision attribute <-- take a look a this answer witch has code to genereate modelBuilder.Entity<CLASS>().Property(OBJECT=> OBJECT.PROPERTY).HasPrecision(12, 10) automatically from an attribute in a class

Community
  • 1
  • 1
KinSlayerUY
  • 1,903
  • 17
  • 22