Repository navigation
Expand file tree
/
Copy pathQuick.Core.Extensions.OptionsMonitor.pas
More file actions
123 lines (103 loc) · 4.51 KB
/
Copy pathQuick.Core.Extensions.OptionsMonitor.pas
File metadata and controls
123 lines (103 loc) · 4.51 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
{ ***************************************************************************
Copyright (c) 2016-2026 Kike Perez
Unit : Quick.Core.Extensions.OptionsMonitor
Description : DI extensions — AddOptionsMonitor / AddOptionsSnapshot
Author : Kike Perez
Version : 1.0
Created : 30/04/2026
Modified : 08/05/2026
This file is part of QuickCore: https://github.com/exilon/QuickCore
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Core.Extensions.OptionsMonitor;
{$i QuickCore.inc}
interface
uses
System.SysUtils,
Quick.Options,
Quick.Core.DependencyInjection,
Quick.Core.Options.Monitor;
type
/// <summary>
/// DI extensions for reactive options monitoring.
///
/// PREREQUISITE: Call services.Configure<T> before calling these methods
/// so that the options section is already registered in the container.
///
/// ─── AddOptionsMonitor<T> ────────────────────────────────────────────
/// Registers IOptionsMonitor<T> as a singleton. The monitor hooks into the
/// options container's OnConfigReloaded event (fired by the file watcher when
/// appsettings.json changes) and fans out to all registered callbacks.
///
/// services.Configure<TDatabaseOptions>('Database');
/// services.Extension<TOptionsMonitorServiceExtension>
/// .AddOptionsMonitor<TDatabaseOptions>;
///
/// // Later, in a service constructor:
/// constructor TMyService.Create(aMonitor: IOptionsMonitor<TDatabaseOptions>);
/// begin
/// fToken := aMonitor.OnChange(procedure(o: TDatabaseOptions)
/// begin
/// ReconnectWithNewSettings(o.ConnectionString);
/// end);
/// end;
///
/// ─── AddOptionsSnapshot<T> ──────────────────────────────────────────────
/// Registers IOptionsSnapshot<T> as a singleton whose Value property always
/// returns the current options value. Suitable for per-request consumers
/// (e.g. MVC controllers) that need a fresh read without subscribing to
/// change events.
///
/// services.Extension<TOptionsMonitorServiceExtension>
/// .AddOptionsSnapshot<TFeatureOptions>;
///
/// .NET equivalent:
/// services.AddOptions<T>()
/// .BindConfiguration("Section")
/// .AddOptionsMonitor / AddOptionsSnapshot
/// </summary>
TOptionsMonitorServiceExtension = class(TServiceCollectionExtension)
public
/// <summary>
/// Registers IOptionsMonitor<T> as a singleton. Requires T to be already
/// configured via services.Configure<T>.
/// </summary>
class function AddOptionsMonitor<T: TOptions>: TServiceCollection;
/// <summary>
/// Registers IOptionsSnapshot<T> as a singleton. Value always reflects the
/// latest loaded configuration. Requires T to be already configured.
/// </summary>
class function AddOptionsSnapshot<T: TOptions>: TServiceCollection;
end;
implementation
{ TOptionsMonitorServiceExtension }
class function TOptionsMonitorServiceExtension.AddOptionsMonitor<T>: TServiceCollection;
var
container : TOptionsContainer;
monitor : IOptionsMonitor<T>;
begin
container := ServiceCollection.AppServices.Options;
monitor := TOptionsMonitor<T>.Create(container);
ServiceCollection.AddSingleton<IOptionsMonitor<T>>(monitor);
Result := ServiceCollection;
end;
class function TOptionsMonitorServiceExtension.AddOptionsSnapshot<T>: TServiceCollection;
var
container : TOptionsContainer;
begin
container := ServiceCollection.AppServices.Options;
ServiceCollection.AddSingleton<IOptionsSnapshot<T>>(
TOptionsSnapshot<T>.Create(container) as IOptionsSnapshot<T>
);
Result := ServiceCollection;
end;
end.