-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
FeatureReference.cs
56 lines (51 loc) · 1.91 KB
/
FeatureReference.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.AspNetCore.Http.Features
{
/// <summary>
/// A cached reference to a feature.
/// </summary>
/// <typeparam name="T">The feature type.</typeparam>
public struct FeatureReference<T>
{
private T? _feature;
private int _revision;
private FeatureReference(T? feature, int revision)
{
_feature = feature;
_revision = revision;
}
/// <summary>
/// Gets the default <see cref="FeatureReference{T}"/>.
/// </summary>
public static readonly FeatureReference<T> Default = new FeatureReference<T>(default(T), -1);
/// <summary>
/// Gets the feature of type <typeparamref name="T"/> from <paramref name="features"/>.
/// </summary>
/// <param name="features">The <see cref="IFeatureCollection"/>.</param>
/// <returns>The feature.</returns>
public T? Fetch(IFeatureCollection features)
{
if (_revision == features.Revision)
{
return _feature;
}
_feature = (T?)features[typeof(T)];
_revision = features.Revision;
return _feature;
}
/// <summary>
/// Updates the reference to the feature.
/// </summary>
/// <param name="features">The <see cref="IFeatureCollection"/> to update.</param>
/// <param name="feature">The instance of the feature.</param>
/// <returns>A reference to <paramref name="feature"/> after the operation has completed.</returns>
public T Update(IFeatureCollection features, T feature)
{
features[typeof(T)] = feature;
_feature = feature;
_revision = features.Revision;
return feature;
}
}
}