Skip to content

Commit 1681812

Browse files
authored
Docs: Simplify example on specification builder extensiblity (#249)
* Docs: Simply example on specification builder extensiblity * Docs: Apply code suggestions from @fiseni (#247) Co-authored-by: Michaël Vittorelli <[email protected]>
1 parent b1998da commit 1681812

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

docs/extensions/create-specification-builder.md renamed to docs/extensions/extend-specification-builder.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ nav_order: 2
66
---
77

88

9-
# How to create your own specification builder
9+
# How to add extensions to the specification builder
1010

1111
The specification builder from `Ardalis.Specification` is extensible by design. In fact, the methods you can use out of the box are implemented as extension methods themselves (check out the [source code](https://github.com/ardalis/Specification/blob/main/Specification/src/Ardalis.Specification/Builder/SpecificationBuilderExtensions.cs)). Your project might have requirements that cannot be satisfied by the existing toolset of course, or you might want to simplify repetitive code in several specification constructors. Whatever your case, enhancing the default builder is easy by creating your own extension methods.
1212

@@ -87,28 +87,19 @@ public class Repository<T>
8787
}
8888
```
8989

90-
Finally, we need to take care of some plumbing to implement the `.GetCacheTTL` and `.SetCacheTTL` methods that we've used in the example repository and builder extension. The class below uses `ConditionalWeakTable` to do the trick. Another solution is to create a base class that inherits from `Specification<T>`.
90+
Finally, we need to take care of some plumbing to implement the `.GetCacheTTL` and `.SetCacheTTL` methods that we've used in the example repository and builder extension.
9191

9292
````csharp
9393
public static class SpecificationExtensions
9494
{
95-
private static readonly ConditionalWeakTable<object, CacheOptions> SpecificationCacheOptions = new();
96-
97-
public static void SetCacheTTL<T>(this ISpecification<T> spec, TimeSpan ttl)
95+
public static void SetCacheTTL<T>(this ISpecification<T> spec, TimeSpan timeToLive)
9896
{
99-
SpecificationCacheOptions.AddOrUpdate(spec, new CacheOptions() { TTL = ttl });
97+
spec.Items["CacheTTL"] = timeToLive;
10098
}
101-
10299
public static TimeSpan GetCacheTTL<T>(this ISpecification<T> spec)
103100
{
104-
var opts = SpecificationCacheOptions.GetOrCreateValue(spec);
105-
return opts?.TTL ?? TimeSpan.MaxValue;
106-
}
107-
108-
// ConditionalWeakTable need reference types; TimeSpan is a struct
109-
private class CacheOptions
110-
{
111-
public TimeSpan TTL { get; set; }
101+
spec.Items.TryGetValue("CacheTTL", out var ttl);
102+
return (ttl as TimeSpan?) ?? TimeSpan.MaxValue;
112103
}
113104
}
114105
````

docs/features/caching.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public class CustomerByNameWithStoresSpec : Specification<Customer>, ISingleResu
2323
}
2424
```
2525

26-
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](../extensions/create-specification-builder.md)).
26+
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](../extensions/extend-specification-builder.md)).
2727

2828
Implementing caching will also require infrastructure such as a CachedRepository, an example of which is given in [the sample](https://github.com/ardalis/Specification/blob/2605202df4d8e40fe388732db6d8f7a3754fcc2b/sample/Ardalis.SampleApp.Infrastructure/Data/CachedCustomerRepository.cs#L13) 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.

0 commit comments

Comments
 (0)