当前位置: 首页 > news >正文

深圳网站建设代理商湖北网站定制开发价格表

深圳网站建设代理商,湖北网站定制开发价格表,岳池做网站电话,职业技术培训.net core 中的经典设计模式的应用Intro前段时间我们介绍了23种设计模式#xff0c;今天来分享一下 .net core 源码中我觉得比较典型的设计模式的应用实例责任链模式asp.net core 中间件的设计就是责任链模式的应用和变形#xff0c;每个中间件根据需要处理请求#xff0c;并… .net core 中的经典设计模式的应用Intro前段时间我们介绍了23种设计模式今天来分享一下 .net core 源码中我觉得比较典型的设计模式的应用实例责任链模式asp.net core 中间件的设计就是责任链模式的应用和变形每个中间件根据需要处理请求并且可以根据请求信息自己决定是否传递给下一个中间件我也受此启发封装了一个 PipelineBuilder 可以轻松构建中间件模式代码可以参考这篇文章 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}/{controllerHome}/{actionIndex});endpoints.MapDefaultControllerRoute(); }); PipelineBuilder 实际示例var requestContext new RequestContext() {RequesterName Kangkang,Hour 12, };var builder PipelineBuilder.CreateRequestContext(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 又是 DirectorBuilder 本身知道如何构建最终的 Product(Host/Configuration)var host new HostBuilder().ConfigureAppConfiguration(builder {// 注册配置builder.AddInMemoryCollection(new Dictionarystring, string(){{UserName, Alice}}).AddJsonFile(appsettings.json);}).ConfigureServices((context, services) {// 注册自定义服务services.AddSingletonIIdGenerator, GuidIdGenerator();services.AddTransientIService, Service();if (context.Configuration.GetAppSettingbool(XxxEnabled)){services.AddSingletonIUserIdProvider, EnvironmentUserIdProvider();}}).Build(); 工厂模式依赖注入框架中有着大量的工厂模式的代码注册服务的时候我们可以通过一个工厂方法委托来获取服务实例依赖注入的本质就是将对象的创建交给 IOC 容器来处理所以其实 IOC 容器本质就是一个工厂从 IOC 中获取服务实例的过程就是工厂创建对象的过程只是会根据服务的生命周期来决定是创建新对象还是返回已有对象。services.AddSingleton(sp new Svc2(sp.GetRequiredServiceISvc1(), xx)); 单例模式在 dotnet 中有一个 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 SimpleEventTypesT: TraceLoggingEventTypes {private static SimpleEventTypesT instance;internal readonly TraceLoggingTypeInfoT typeInfo;private SimpleEventTypes(TraceLoggingTypeInfoT typeInfo): base(typeInfo.Name,typeInfo.Tags,new TraceLoggingTypeInfo[] { typeInfo }){this.typeInfo typeInfo;}public static SimpleEventTypesT Instance{get { return instance ?? InitInstance(); }}private static SimpleEventTypesT InitInstance(){var newInstance new SimpleEventTypesT(TraceLoggingTypeInfoT.Instance);Interlocked.CompareExchange(ref instance, newInstance, null);return instance;} } 另外一个示例,需要注意下面这种方式不能严格的保证只会产生一个实例在并发较高的情况下可能不是同一个实例这也可以算是工厂模式的一个示例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.CreateInstanceIConfigurationManagerHelper(ConfigurationManagerHelperTypeString);} } 原型模式dotnet 中有两个数据结构 Stack/Queue 这两个数据都实现了 ICloneable 接口内部实现了深复制 来看 Stack 的 Clone 方法实现public virtual Object Clone() {Contract.Ensures(Contract.ResultObject() ! null);Stack s new Stack(_size);s._size _size;Array.Copy(_array, 0, s._array, 0, _size);s._version _version;return s; } 详细可以参考https://referencesource.microsoft.com/#mscorlib/system/collections/stack.cs,6acda10c5f8b128e享元模式string intern(字符串池)以及 Array.Emptyint()/Array.Emptystring() 等策略模式asp.net core 中的认证和授权我觉得就是策略模式的应用在使用 [Authorize] 的时候会使用默认的 policy也可以指定要使用的策略 [Authorize(Policy1)] 这样就会使用另外一种策略 Policy1policy 还是比较简单的policy 是用来根据用户的认证信息来控制授权访问的而认证则是根据当前上下文请求上下文、线程上下文、环境上下文等的信息进行认证从而获取用户信息的过程而不同的认证模式Cookie/JWT/自定义Token等其实是不同的处理方法也就是策略模式中不同的算法实现指定哪种认证模式就是使用哪种算法实现来获取用户信息观察者模式常使用事件(event)进行解耦外部代码通过订阅事件来解耦实现对内部状态的观察在 Process 类中有很多事件可以用来捕获另一个进程中的输出错误等public event DataReceivedEventHandler OutputDataReceived;public event DataReceivedEventHandler ErrorDataReceived; 通常这两个事件我们就可以获取到另外一个进程中的输出信息除此之外还有很多的类在使用事件相信你也用过很多组合模式WPF、WinForm 中都有控件的概念这些控件的设计属于是组合模式的应用所有的控件都会继承于某一个共同的基类, 使得单个对象和组合对象都可以看作是他们共同的基类对象迭代器模式c# 中定义了迭代器模式原始定义// 聚集抽象 public interface IEnumerable {/// summaryReturns an enumerator that iterates through a collection./summary/// returnsAn see crefT:System.Collections.IEnumerator / object that can be used to iterate through the collection./returnsIEnumerator GetEnumerator(); }// 迭代器抽象 public interface IEnumerator {/// summaryAdvances the enumerator to the next element of the collection./summary/// returns/// see langwordtrue / if the enumerator was successfully advanced to the next element; see langwordfalse / if the enumerator has passed the end of the collection./returns/// exception crefT:System.InvalidOperationExceptionThe collection was modified after the enumerator was created./exceptionbool MoveNext();/// summaryGets the element in the collection at the current position of the enumerator./summary/// returnsThe element in the collection at the current position of the enumerator./returnsobject Current { get; }/// summarySets the enumerator to its initial position, which is before the first element in the collection./summary/// exception crefT:System.InvalidOperationExceptionThe collection was modified after the enumerator was created./exceptionvoid Reset(); } Array 和 List 各自实现了自己的迭代器感兴趣可以去看下源码More.net core 中的设计模式应用还有很多不仅上面提到的这几个模式也不仅仅是我所提到的这几个地方上面有一些示例是直接用的 dotnet framework 中的源码因为有很多代码都是类似的用的 https://referencesource.microsoft.com 的源码以上均是个人理解如果有错误还望指出十分感谢欢迎补充更多设计模式应用的源码实例Referencehttps://github.com/dotnet/aspnetcorehttps://github.com/dotnet/extensionshttps://github.com/dotnet/corefxhttps://github.com/dotnet/aspnetcorehttps://github.com/dotnet/runtime
http://www.yutouwan.com/news/18965/

相关文章:

  • 贵阳网站方舟网络最好wordpress 占用内存高
  • 钛钢饰品移动网站建设如何修改代码wordpress
  • 网站优化怎样的系统开发的一般过程
  • 微知微网站建设实训平台总部基地网站建设公司
  • 色彩搭配 网站百度商桥绑定网站
  • wordpress搭建企业网站wordpress 无法登陆后台
  • 苏州手机网站建设wordpress的用户名
  • 淄博市建设业协会网站郑州正规的网站制作
  • wordpress站外搜索为什么网站要备案
  • php网站开发技术优点企业站seo价格
  • iis7.0网站错误代码解决视频图站主题 wordpress
  • 深圳网站建设服务哪个便宜点珠海网站制作推广
  • 深圳极速网站建设电话国内免费推广产品的网站
  • 凡科建站网站怎么保存发给别人数字城市建设网站
  • 智慧团建网站登录入口手机版wordpress 游客
  • 沈阳做网站的设计公司哪家好wordpress字体更换
  • 地方网站做相亲赢利点在哪网站建设dede模板免费
  • 机器人软件开发和网站开发需要一个网站
  • 安徽池州网站制作佛山做网站的
  • 新网站怎么做友情链接网站开发后所有权
  • 怎样创造一个网站建设银行有招投标网站吗
  • 网站内容建设的原则wordpress后台500出错
  • 企业网站建设的困难和问题太原搜索引擎优化招聘信息
  • 做短视频网站收益大学网络推广培训
  • 找做网站的公司网站模板上传
  • 做个小网站 虚拟空间 买服务器莘县聊城做网站
  • 泉州(晋江)网站建设网络小程序开发公司
  • 网站规划与网页设计第四版电子书网站建设 图书
  • 清远市住房和城乡建设局网站网站建设公司 壹宇网络
  • 做海淘网站赚钱吗上哪儿找做网站的客户