Table of Contents

CacheableObjectResultExtensions Class

Definition

Namespace
Cuemon.Extensions.AspNetCore.Mvc
Assemblies
Cuemon.Extensions.AspNetCore.Mvc.dll
Source
src/Cuemon.Extensions.AspNetCore.Mvc/CacheableObjectResultExtensions.cs

Extension methods for the ICacheableObjectResult interface.

public static class CacheableObjectResultExtensions
Inheritance
CacheableObjectResultExtensions

Examples

The following example mirrors the cacheable object patterns covered by the unit tests: a payload can expose Last-Modified metadata, an ETag, or both at once. It calls WithLastModifiedHeader, WithEntityTagHeader, and WithCacheableHeaders in sequence on a ProductDto, each with timestamp and checksum provider callbacks. The resulting ICacheableObjectResult is read back to display the modified timestamp and entity-tag validation, showing how to attach HTTP caching headers to response payloads.

using System;
using System.Text;
using Cuemon.AspNetCore.Mvc;
using Cuemon.Data.Integrity;
using Cuemon.Extensions.AspNetCore.Mvc;

namespace Cuemon.Extensions.AspNetCore.Mvc.DocExamples;

public sealed class CacheableObjectResultExample
{
    public ICacheableObjectResult CreateLastModifiedResult()
    {
        var product = new ProductDto(42, "Coffee Beans");
        return product.WithLastModifiedHeader(options =>
        {
            options.TimestampProvider = _ => new DateTime(2024, 6, 1, 8, 0, 0, DateTimeKind.Utc);
            options.ChangedTimestampProvider = _ => new DateTime(2024, 6, 18, 8, 30, 0, DateTimeKind.Utc);
        });
    }

    public ICacheableObjectResult CreateEntityTagResult()
    {
        var product = new ProductDto(42, "Coffee Beans");
        return product.WithEntityTagHeader(options =>
        {
            options.ChecksumProvider = dto => Encoding.UTF8.GetBytes($"{dto.Id}:{dto.Name}");
            options.WeakChecksumProvider = _ => false;
        });
    }

    public ICacheableObjectResult CreateFullyCacheableResult()
    {
        var product = new ProductDto(42, "Coffee Beans");
        return product.WithCacheableHeaders(options =>
        {
            options.TimestampProvider = _ => new DateTime(2024, 6, 1, 8, 0, 0, DateTimeKind.Utc);
            options.ChangedTimestampProvider = _ => new DateTime(2024, 6, 18, 8, 30, 0, DateTimeKind.Utc);
            options.ChecksumProvider = dto => Encoding.UTF8.GetBytes($"{dto.Id}:{dto.Name}");
            options.WeakChecksumProvider = _ => false;
        });
    }

    public void Describe()
    {
        var result = CreateFullyCacheableResult();
        var timestamp = (IEntityDataTimestamp)result;
        var integrity = (IEntityDataIntegrity)result;
        var product = (ProductDto)result.Value;

        Console.WriteLine($"{product.Name}: {(timestamp.Modified ?? timestamp.Created):O} [{integrity.Validation}]");
    }
}

public sealed class ProductDto
{
    public ProductDto(int id, string name)
    {
        Id = id;
        Name = name;
    }

    public int Id { get; }

    public string Name { get; }
}

Methods

Name Description
WithCacheableHeaders<T>(T, Action<CacheableObjectResultOptions<T>>)

Encapsulates the specified instance within a timestamp and integrity based object that is processed by both HTTP Last-Modified and HTTP ETag filters implementation.

WithEntityTagHeader<T>(T, Action<ContentBasedObjectResultOptions<T>>)

Encapsulates the specified instance within an integrity based object that is processed by an HTTP ETag filter implementation.

WithLastModifiedHeader<T>(T, Action<TimeBasedObjectResultOptions<T>>)

Encapsulates the specified instance within a timestamp based object that is processed by a Last-Modified filter implementation.

See Also