Skip to main content Link Menu Expand (external link) Document Search Copy Copied


To implement caching using Specification, you will need to enable caching on your specification when it is defined:

public class CustomerByNameWithStoresSpec : Specification<Customer>, ISingleResultSpecification
        public CustomerByNameWithStoresSpec(string name)
            Query.Where(x => x.Name == name)
                .Include(x => x.Stores)
                .EnableCache(nameof(CustomerByNameWithStoresSpec), name);

The .EnableCache method takes in two parameters: the name of the specification and the parameters of the specification. It does not include any parameters to control how the cache should behave (e.g. absolute expiration date, expiration tokens, …). However, one could create an extension method to the specification builder in order to add this information (example).

Implementing caching will also require infrastructure such as a CachedRepository, an example of which is given in the sample on GitHub. The EnableCache method is used to inform the cache implementation that caching should be used, and to configure the CacheKey based on the arguments supplied.