[Asp.net 5] Options-配置文件(2)
很久之前寫過一篇介紹Options的文章,2016年再打開發(fā)現(xiàn)很多變化。增加了新類,增加OptionMonitor相關(guān)的類。今天就對于這個現(xiàn)在所謂的新版本進行介紹。
老版本的傳送門([Asp.net 5] Options-配置文件之后昂的配置)。
首先上一個圖:
*綠線是繼承關(guān)系,藍線是關(guān)聯(lián)關(guān)系。
我們把上面切成2大部分。
Option部分
這部分分為倆部分,第一部分直接創(chuàng)建Options,該部分通過Options靜態(tài)類創(chuàng)建一個OptionsWrapper類,之后將(IOptions,OptionsWrapper)進行注入。這部分是DI的實體注入,很簡單,沒有什么可說的,此處應用非常常見的“工廠模式”。
第二部分是將(IOptions,OptionsManager)進行注入。我們OptionsManager會使用IEnumerable<IConfigureOptions<TOptions>>作為參數(shù),而內(nèi)部返回的是OptionsCache類型的對象,此處應用非常常見的“代理模式”
internal class OptionsCache<TOptions> where TOptions : class, new(){private readonly Func<TOptions> _createCache;private object _cacheLock = new object();private bool _cacheInitialized;private TOptions _options;private IEnumerable<IConfigureOptions<TOptions>> _setups;public OptionsCache(IEnumerable<IConfigureOptions<TOptions>> setups){_setups = setups;_createCache = CreateOptions;}private TOptions CreateOptions(){var result = new TOptions();if (_setups != null){foreach (var setup in _setups){setup.Configure(result);}}return result;}public virtual TOptions Value{get{return LazyInitializer.EnsureInitialized(ref _options,ref _cacheInitialized,ref _cacheLock,_createCache);}}}OptionsCache
此處附錄OptionsCache代碼,里面(IConfigureOptions,ConfigureOptions)已經(jīng)進行注入了。而ConfigureOptions代碼如下:
public class ConfigureOptions<TOptions> : IConfigureOptions<TOptions> where TOptions : class{public ConfigureOptions(Action<TOptions> action){if (action == null){throw new ArgumentNullException(nameof(action));}Action = action;}public Action<TOptions> Action { get; private set; }public virtual void Configure(TOptions options){if (options == null){throw new ArgumentNullException(nameof(options));}Action.Invoke(options);}}ConfigureOptions
而ConfigureOptions實際上只是對Action<TOptions>的封裝吧了(這里是不是可以理解為適配器)。
*為什么要傳遞Action<T>進行配置?我的理解是因為延時性。延時的概念就是,你做的修改不是立馬生效,以至于配置的時候,我們都不用考慮先后順序。
OptionsMonitor部分
OptionsMonitor是對Options的監(jiān)視器。我決定這部分好像一個調(diào)度者模式??。
的IOptionsChangeTokenSource
OptionsMonitor代碼如下:
public class OptionsMonitor<TOptions> : IOptionsMonitor<TOptions> where TOptions : class, new(){private OptionsCache<TOptions> _optionsCache;private readonly IEnumerable<IConfigureOptions<TOptions>> _setups;private readonly IEnumerable<IOptionsChangeTokenSource<TOptions>> _sources;public OptionsMonitor(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerable<IOptionsChangeTokenSource<TOptions>> sources){_sources = sources;_setups = setups;_optionsCache = new OptionsCache<TOptions>(setups);}public TOptions CurrentValue{get{return _optionsCache.Value;}}public IDisposable OnChange(Action<TOptions> listener){var disposable = new ChangeTrackerDisposable();foreach (var source in _sources){Action<object> callback = null;IDisposable previousSubscription = null;callback = (s) =>{// The order here is important. We need to take the token and then apply our changes BEFORE// registering. This prevents us from possible having two change updates to process concurrently.//// If the token changes after we take the token, then we'll process the update immediately upon// registering the callback.var token = source.GetChangeToken();// Recompute the options before calling the watchers_optionsCache = new OptionsCache<TOptions>(_setups);listener(_optionsCache.Value);// Remove the old callback after its been firedvar nextSubscription = token.RegisterChangeCallback(callback, s);disposable.Disposables.Add(nextSubscription);disposable.Disposables.Remove(previousSubscription);previousSubscription = nextSubscription;};previousSubscription = source.GetChangeToken().RegisterChangeCallback(callback, state: null);disposable.Disposables.Add(previousSubscription);}return disposable;}}OptionsMonitor
通過IOptionsChangeTokenSource的IChangeToken對象發(fā)出更改請求,之后Action<TOptions> listener進行數(shù)據(jù)更改。
Onchange方法,實現(xiàn)上就是每次調(diào)用都會創(chuàng)建一個新的IDisposable(ChangeTrackerDisposable),如此而已。
轉(zhuǎn)載于:https://www.cnblogs.com/watermoon2/p/5119085.html
總結(jié)
以上是生活随笔為你收集整理的[Asp.net 5] Options-配置文件(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “寒衾不可亲”下一句是什么
- 下一篇: html表单的创建和css的构成