Class CacheableObjectResultExtensions
- Namespace
- Cuemon.Extensions.AspNetCore.Mvc
- Assembly
- Cuemon.Extensions.AspNetCore.Mvc.dll
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
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.
public static ICacheableObjectResult WithCacheableHeaders<T>(this T instance, Action<CacheableObjectResultOptions<T>> setup)
Parameters
instanceTThe instance to make cacheable.
setupAction<CacheableObjectResultOptions<T>>The CacheableObjectResultOptions<T> that needs to be configured.
Returns
- ICacheableObjectResult
An ICacheableObjectResult implementation.
Type Parameters
TThe type of the object to make cacheable.
- See Also
-
CacheableObjectResult<T>
WithEntityTagHeader<T>(T, Action<ContentBasedObjectResultOptions<T>>)
Encapsulates the specified instance within an integrity based object that is processed by an HTTP ETag filter implementation.
public static ICacheableObjectResult WithEntityTagHeader<T>(this T instance, Action<ContentBasedObjectResultOptions<T>> setup)
Parameters
instanceTThe instance to make cacheable.
setupAction<ContentBasedObjectResultOptions<T>>The ContentBasedObjectResultOptions<T> that needs to be configured.
Returns
- ICacheableObjectResult
An ICacheableObjectResult implementation.
Type Parameters
TThe type of the object to make cacheable.
- See Also
-
CacheableObjectResult<T>
WithLastModifiedHeader<T>(T, Action<TimeBasedObjectResultOptions<T>>)
Encapsulates the specified instance within a timestamp based object that is processed by a Last-Modified filter implementation.
public static ICacheableObjectResult WithLastModifiedHeader<T>(this T instance, Action<TimeBasedObjectResultOptions<T>> setup)
Parameters
instanceTThe instance to make cacheable.
setupAction<TimeBasedObjectResultOptions<T>>The TimeBasedObjectResultOptions<T> that needs to be configured.
Returns
- ICacheableObjectResult
An ICacheableObjectResult implementation.
Type Parameters
TThe type of the object to make cacheable.
- See Also
-
CacheableObjectResult<T>