Quantcast
Channel: Developer to developer
Viewing all articles
Browse latest Browse all 9076

How to write DRY code with filter facets

$
0
0

I'm working on a search solution using unified search that has a few category filters.

When making the filterfacets to display the number of results to be expected in each category, the code looks like

search = search.FilterFacet(Articles, content =>
    content.MatchTypeHierarchy(typeof(ArticlePage))
    | content.MatchTypeHierarchy(typeof(SpecialReport))
    | content.MatchTypeHierarchy(typeof(SomeOtherPublication)));

then, when actually filtering the results, the code looks like

if (filter == Articles)
{
    search = search.FilterHits(content =>
        content.MatchTypeHierarchy(typeof(ArticlePage))
        | content.MatchTypeHierarchy(typeof(SpecialReport))
        | content.MatchTypeHierarchy(typeof(SomeOtherPublication)));
}

So it seems like this should be the same thing, because it's the exact same code, but I'm having a bit of trouble separating it.

If I make it an extension method like this, for instance

public static EPiServer.Find.Api.Querying.Filter FilterArticles(this ISearchContent content)
{
    return content.MatchTypeHierarchy(typeof(ArticlePage))
        | content.MatchTypeHierarchy(typeof(SpecialReport))
        | content.MatchTypeHierarchy(typeof(SomeOtherPublication)));              
}

Then there's no repetition

search = search.FilterFacet(Articles, content =>
    content.FilterArticles());
if (filter == Articles)
{
    search = search.FilterHits(content =>
        content.FilterArticles());
}

But I get a runtime exception

variable 'content' of type 'EPiServer.Find.UnifiedSearch.ISearchContent' referenced from scope '', but it is not defined

I've tried various permutations on this. Anyone know the right way to do it?

By the way, I think the code is built this way instead of using the unified search features because the categories aren't well represented in the code. For instance, the "Article" category displayed to the user consists of multiple page types that don't share a common class in the code. At least not a common class that distinguishes them from other categories.


Viewing all articles
Browse latest Browse all 9076

Latest Images

Trending Articles



Latest Images