Skip to content

query specifications

vijay@envivo edited this page May 26, 2026 · 15 revisions

Query Specifications

Query Specifications provide a filtered list of objects based – unsurprisingly – on a query, and are used in a number of places within a domain.

To create one, implement the IQuerySpecification<T> interface. This example uses an in-memory list, but a repository or data store could be used instead.

using Envivo.Fresnel.ModelTypes.Persistence;
using System.Collections.Generic;
using System.Linq;

public class ProductQuerySpecification : IQuerySpecification<Product> //👈
{ 
    private static readonly List<Product> _Products = BuildProductsForDemo();

    private static List<Product> BuildProductsForDemo()
    {
        var results = new List<Product> {
            new Product{
                Name = "Arturia Microfreak Synthesizer",
                Description = "Micro wavetable synth" },
            new Product{
                Name = "Korg Minilogue Synthesizer",
                Description = "Analog mono synth" },
            new Product{
                Name = "Korg R3 Synthesizer",
                Description = "Digital virtual analog synth" },
            new Product{
                Name = "Akai MPk-25 Midi Controller",
                Description = "25 key controller with CC and NRPN support" },
            new Product{
                Name = "Alesis Micron Synthesizer",
                Description = "Digital virtual analog synth" },
            new Product{
                Name = "Modal SKULPTsynth",
                Description = "Portable digital analog synth" },
            new Product{
                Name = "Zoom ARQ 48 Groove Box",
                Description = "Portable sequencer + synth" },
            };

        foreach (var product in results)
        {
            product.Id = Guid.NewGuid();
        }

        return results;
    }

    public Task<IEnumerable<Product>> GetResultsAsync(CancellationToken cancellationToken)
    {
        var matches = _Products.AsEnumerable();
        return Task.FromResult(matches);
    }
}

Context dependent Query Filters

In many cases, the results of the query are dependent on the object currently in context. Add an overloaded GetResultsAsync method with a parameter that matches the object, and you can filter the data dynamically:

using Envivo.Fresnel.ModelTypes.Persistence;
using System.Collections.Generic;
using System.Linq;

public class ProductQuerySpecification : IQuerySpecification<Product>
{
    private static readonly List<Product> _Products = BuildProductsForDemo();

    ...

    public Task<IEnumerable<Product>> GetResultsAsync(CancellationToken cancellationToken)
    {
        // In this example, we're using an in-memory list so async calls are not needed:
        var matches = _Products.AsEnumerable();

        return Task.FromResult(matches);
    }

    public Task<IEnumerable<Product>> GetResultsAsync(Account customerAccount, CancellationToken cancellationToken) //👈
    {
        // In this example, we're using an in-memory list so async calls are not needed:
        var matches =
            _Products
            .Where(p => <some predicate using customerAccount>) //👈
            .ToList();

        return Task.FromResult(matches);
    }
}

Clone this wiki locally