.net core 中的经典设计模式的应用
.net core 中的經(jīng)典設(shè)計(jì)模式的應(yīng)用
Intro
前段時(shí)間我們介紹了23種設(shè)計(jì)模式,今天來(lái)分享一下 .net core 源碼中我覺(jué)得比較典型的設(shè)計(jì)模式的應(yīng)用
實(shí)例
責(zé)任鏈模式
asp.net core 中間件的設(shè)計(jì)就是責(zé)任鏈模式的應(yīng)用和變形,
每個(gè)中間件根據(jù)需要處理請(qǐng)求,并且可以根據(jù)請(qǐng)求信息自己決定是否傳遞給下一個(gè)中間件,我也受此啟發(fā),封裝了一個(gè) PipelineBuilder 可以輕松構(gòu)建中間件模式代碼,可以參考這篇文章 https://www.cnblogs.com/weihanli/p/12700006.html
中間件示例:
app.UseStaticFiles();app.UseResponseCaching(); app.UseResponseCompression();app.UseRouting();app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());app.UseAuthentication(); app.UseAuthorization();app.UseEndpoints(endpoints => {endpoints.MapControllers();endpoints.MapControllerRoute(name: "areaRoute", "{area:exists}/{controller=Home}/{action=Index}");endpoints.MapDefaultControllerRoute(); });PipelineBuilder 實(shí)際示例:
var requestContext = new RequestContext() {RequesterName = "Kangkang",Hour = 12, };var builder = PipelineBuilder.Create<RequestContext>(context =>{Console.WriteLine($"{context.RequesterName} {context.Hour}h apply failed");}).Use((context, next) =>{if (context.Hour <= 2){Console.WriteLine("pass 1");}else{next();}}).Use((context, next) =>{if (context.Hour <= 4){Console.WriteLine("pass 2");}else{next();}}).Use((context, next) =>{if (context.Hour <= 6){Console.WriteLine("pass 3");}else{next();}}); var requestPipeline = builder.Build(); foreach (var i in Enumerable.Range(1, 8)) {Console.WriteLine();Console.WriteLine($"--------- h:{i} apply Pipeline------------------");requestContext.Hour = i;requestPipeline.Invoke(requestContext);Console.WriteLine("----------------------------");Console.WriteLine(); }建造者模式
asp.net core 中的各種 Builder, HostBuilder/ConfigurationBuilder 等,這些 Builder 大多既是 Builder 又是 Director,Builder 本身知道如何構(gòu)建最終的 Product(Host/Configuration)
var host = new HostBuilder().ConfigureAppConfiguration(builder =>{// 注冊(cè)配置builder.AddInMemoryCollection(new Dictionary<string, string>(){{"UserName", "Alice"}}).AddJsonFile("appsettings.json");}).ConfigureServices((context, services) =>{// 注冊(cè)自定義服務(wù)services.AddSingleton<IIdGenerator, GuidIdGenerator>();services.AddTransient<IService, Service>();if (context.Configuration.GetAppSetting<bool>("XxxEnabled")){services.AddSingleton<IUserIdProvider, EnvironmentUserIdProvider>();}}).Build();工廠模式
依賴注入框架中有著大量的工廠模式的代碼,注冊(cè)服務(wù)的時(shí)候我們可以通過(guò)一個(gè)工廠方法委托來(lái)獲取服務(wù)實(shí)例,
依賴注入的本質(zhì)就是將對(duì)象的創(chuàng)建交給 IOC 容器來(lái)處理,所以其實(shí) IOC 容器本質(zhì)就是一個(gè)工廠,從 IOC 中獲取服務(wù)實(shí)例的過(guò)程就是工廠創(chuàng)建對(duì)象的過(guò)程,只是會(huì)根據(jù)服務(wù)的生命周期來(lái)決定是創(chuàng)建新對(duì)象還是返回已有對(duì)象。
services.AddSingleton(sp => new Svc2(sp.GetRequiredService<ISvc1>(), "xx"));單例模式
在 dotnet 中有一個(gè) TimeQueue 的類型,純正的餓漢模式的單例模式代碼
class TimerQueue {#region singleton pattern implementation// The one-and-only TimerQueue for the AppDomain.static TimerQueue s_queue = new TimerQueue();public static TimerQueue Instance{get { return s_queue; }}private TimerQueue(){// empty private constructor to ensure we remain a singleton.}#endregion// ... }https://referencesource.microsoft.com/#mscorlib/system/threading/timer.cs,49
在 dotnet 源碼中還有一些懶漢式的單例模式
使用 Interlocked 原子操作
internal class SimpleEventTypes<T>: TraceLoggingEventTypes {private static SimpleEventTypes<T> instance;internal readonly TraceLoggingTypeInfo<T> typeInfo;private SimpleEventTypes(TraceLoggingTypeInfo<T> typeInfo): base(typeInfo.Name,typeInfo.Tags,new TraceLoggingTypeInfo[] { typeInfo }){this.typeInfo = typeInfo;}public static SimpleEventTypes<T> Instance{get { return instance ?? InitInstance(); }}private static SimpleEventTypes<T> InitInstance(){var newInstance = new SimpleEventTypes<T>(TraceLoggingTypeInfo<T>.Instance);Interlocked.CompareExchange(ref instance, newInstance, null);return instance;} }另外一個(gè)示例,需要注意,下面這種方式不能嚴(yán)格的保證只會(huì)產(chǎn)生一個(gè)實(shí)例,在并發(fā)較高的情況下可能不是同一個(gè)實(shí)例,這也可以算是工廠模式的一個(gè)示例
static internal class ConfigurationManagerHelperFactory {private const string ConfigurationManagerHelperTypeString = "System.Configuration.Internal.ConfigurationManagerHelper, " + AssemblyRef.System;static private volatile IConfigurationManagerHelper s_instance;static internal IConfigurationManagerHelper Instance {get {if (s_instance == null) {s_instance = CreateConfigurationManagerHelper();}return s_instance;}}[ReflectionPermission(SecurityAction.Assert, Flags = ReflectionPermissionFlag.MemberAccess)][SuppressMessage("Microsoft.Security", "CA2106:SecureAsserts", Justification = "Hard-coded to create an instance of a specific type.")]private static IConfigurationManagerHelper CreateConfigurationManagerHelper() {return TypeUtil.CreateInstance<IConfigurationManagerHelper>(ConfigurationManagerHelperTypeString);} }原型模式
dotnet 中有兩個(gè)數(shù)據(jù)結(jié)構(gòu) Stack/Queue 這兩個(gè)數(shù)據(jù)都實(shí)現(xiàn)了 ICloneable 接口,內(nèi)部實(shí)現(xiàn)了深復(fù)制 來(lái)看 Stack 的 Clone 方法實(shí)現(xiàn):
public virtual Object Clone() {Contract.Ensures(Contract.Result<Object>() != null);Stack s = new Stack(_size);s._size = _size;Array.Copy(_array, 0, s._array, 0, _size);s._version = _version;return s; }詳細(xì)可以參考:https://referencesource.microsoft.com/#mscorlib/system/collections/stack.cs,6acda10c5f8b128e
享元模式
string intern(字符串池),以及 Array.Empty<int>()/Array.Empty<string>() 等
策略模式
asp.net core 中的認(rèn)證和授權(quán),我覺(jué)得就是策略模式的應(yīng)用,在使用 [Authorize] 的時(shí)候會(huì)使用默認(rèn)的 policy,也可以指定要使用的策略 [Authorize("Policy1")] 這樣就會(huì)使用另外一種策略 Policy1,policy 還是比較簡(jiǎn)單的
policy 是用來(lái)根據(jù)用戶的認(rèn)證信息來(lái)控制授權(quán)訪問(wèn)的,而認(rèn)證則是根據(jù)當(dāng)前上下文(請(qǐng)求上下文、線程上下文、環(huán)境上下文等)的信息進(jìn)行認(rèn)證從而獲取用戶信息的過(guò)程
而不同的認(rèn)證模式(Cookie/JWT/自定義Token等)其實(shí)是不同的處理方法,也就是策略模式中不同的算法實(shí)現(xiàn),指定哪種認(rèn)證模式,就是使用哪種算法實(shí)現(xiàn)來(lái)獲取用戶信息
觀察者模式
常使用事件(event)進(jìn)行解耦,外部代碼通過(guò)訂閱事件來(lái)解耦,實(shí)現(xiàn)對(duì)內(nèi)部狀態(tài)的觀察
在 Process 類中有很多事件,可以用來(lái)捕獲另一個(gè)進(jìn)程中的輸出,錯(cuò)誤等
public event DataReceivedEventHandler OutputDataReceived;public event DataReceivedEventHandler ErrorDataReceived;通常這兩個(gè)事件我們就可以獲取到另外一個(gè)進(jìn)程中的輸出信息,除此之外還有很多的類在使用事件,相信你也用過(guò)很多
組合模式
WPF、WinForm 中都有控件的概念,這些控件的設(shè)計(jì)屬于是組合模式的應(yīng)用,所有的控件都會(huì)繼承于某一個(gè)共同的基類, 使得單個(gè)對(duì)象和組合對(duì)象都可以看作是他們共同的基類對(duì)象
迭代器模式
c# 中定義了迭代器模式,原始定義:
// 聚集抽象 public interface IEnumerable {/// <summary>Returns an enumerator that iterates through a collection.</summary>/// <returns>An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.</returns>IEnumerator GetEnumerator(); }// 迭代器抽象 public interface IEnumerator {/// <summary>Advances the enumerator to the next element of the collection.</summary>/// <returns>/// <see langword="true" /> if the enumerator was successfully advanced to the next element; <see langword="false" /> if the enumerator has passed the end of the collection.</returns>/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.</exception>bool MoveNext();/// <summary>Gets the element in the collection at the current position of the enumerator.</summary>/// <returns>The element in the collection at the current position of the enumerator.</returns>object Current { get; }/// <summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.</exception>void Reset(); }Array 和 List 各自實(shí)現(xiàn)了自己的迭代器,感興趣可以去看下源碼
More
.net core 中的設(shè)計(jì)模式應(yīng)用還有很多,不僅上面提到的這幾個(gè)模式,也不僅僅是我所提到的這幾個(gè)地方
上面有一些示例是直接用的 dotnet framework 中的源碼,因?yàn)橛泻芏啻a都是類似的,用的 https://referencesource.microsoft.com 的源碼
以上均是個(gè)人理解,如果有錯(cuò)誤還望指出,十分感謝,歡迎補(bǔ)充更多設(shè)計(jì)模式應(yīng)用的源碼實(shí)例
Reference
https://github.com/dotnet/aspnetcore
https://github.com/dotnet/extensions
https://github.com/dotnet/corefx
https://github.com/dotnet/aspnetcore
https://github.com/dotnet/runtime
總結(jié)
以上是生活随笔為你收集整理的.net core 中的经典设计模式的应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何在生产环境下用好EFCore
- 下一篇: .NET Core + Kubernet