通过极简模拟框架让你了解ASP.NET Core MVC框架的设计与实现[上篇]

《200行代码,7个对象——让你了解ASP.NET Core框架的本质》让很多读者对ASP.NET Core管道有了真实的了解。在过去很长一段时间中,有很多人私信给我:能否按照相同的方式分析一下MVC框架的设计与实现原理,希望这篇文章能够满足你们的需求,源代码可以通过原文下载。

01

MVC与路由

整个MVC框架建立在路由中间件上。不论是面向Controller的Model-View-Controller编程模型,还是面向页面的Razor Pages编程模型,每个请求指向的都一个某个Action,所以MVC框架只需要将每个Action封装成一个路由终结点(RouteEndpoint),并通过自定义的EndpointDataSource注册到路由中间件上即可。

被封装的路由终结点它的请求处理器会帮助我们执行对应的Action,这是一个相对复杂的流程,所以我们创建了一个模拟框架。模拟框架采用真实MVC框架的设计和实现原理,但是会在各个环节进行最大限度地简化。我们希望读者朋友们通过这个模拟框架对MVC框架的设计与实现具有一个总体的认识。

02

Action元数据的解析

由于我们需要在应用启动的时候将所有Action提取出来并封装成路由终结点,所以我们需要一种“Action发现机制”得到定义在所有Controller类型的Action方法,以及所有Razor Page对应的Action方法,并将它们的元数据提取出来。两种编程模型的Action元数据都封装到一个ActionDescriptor对象中。

ActionDescriptor

模拟框架针对Action的描述体现在如下这个ActionDescriptor类型上,它的两个属性成员都与路由有关。我们知道面向Controller的MVC模型支持两种形式的路由,即“约定路由(Conventional Routing)”和“特性路由(Attribute Routing)”。对于前者,我们可以将路由规则定义在Action方法上标注的特性(比如HttpGetAttribute特性)上,后者则体现为针对路由的全局注册。

public abstract class ActionDescriptor
{public AttributeRouteInfo AttributeRouteInfo { get; set; }public IDictionary<string, string> RouteValues { get; set; }
}public class AttributeRouteInfo
{public int Order { get; set; }public string Template { get; set; }
}

我们将通过特性路由提供的原始信息封装成 一个AttributeRouteInfo对象,它的Template代表路由模板。对于一组给定的路由终结点来说,有可能存在多个终结点的路由模式都与某个请求匹配,所以代表路由终结点的RouteEndpoint类型定义了一个Order属性,该属性值越小,代表选择优先级越高。对于通过特性路由创建的RouteEndpoint对象来说,它的Order属性来源于对应AttributeRouteInfo对象的同名属性。

ActionDescriptor的RouteValues属性与“约定路由”有关。比如我们全局定义了一个模板为“{controller}/{action}/{id?}”的路由({controller}和{action}分别表示Controller和Action的名称),如果定义在某个Controller类型(比如FooController)的Action方法(比如Bar)上没有标注任何路由特性,它对应的路由终结点将采用这个约定路由来创建,具体的路由模板将使用真正的Controller和Action名称(“Foo/Bar/{id?}”)。ActionDescriptor的RouteValues属性表示某个Action为约定路由参数提供的参数值,这些值会用来替换约定路由模板中相应的路由参数来生成属于当前Action的路由模板。

我们的模拟框架只提供针对面向Controller的MVC编程模型的支持,针对该模型的Action描述通过如下这个ControllerActionDescriptor类型表示。ControllerActionDescriptor类型继承自抽象类ActionDescriptor,它的MethodInfo和ControllerType属性分别表示Action方法和所在的Controller类型。

public class ControllerActionDescriptor : ActionDescriptor
{public Type ControllerType { get; set; }public MethodInfo Method { get; set; }
}

IActionDescriptorProvider

当前应用范围内针对有效Action元数据的解析通过相应的IActionDescriptorProvider对象来完成。如下面的代码片段所示,IActionDescriptorProvider接口通过唯一的属性ActionDescriptors来提供用来描述所有有效Action的ActionDescriptor对象。

public interface IActionDescriptorProvider
{IEnumerable<ActionDescriptor> ActionDescriptors { get; }
}

如下这个ControllerActionDescriptorProvider类型是IActionDescriptorProvider接口针对面向Controller的MVC编程模型的实现。简单起见,我们在这里作了这么一个假设:所有的Controller类型都定义在当前ASP.NET Core应用所在的项目(程序集)中。基于这个假设,我们在构造函数中注入了代表当前承载环境的IHostEnvironment对象,并利用它得到当前的应用名称。由于应用名称同时也是程序集名称,所以我们得以获取应用所在的程序集,并从中解析出有效的Controller类型。

public class ControllerActionDescriptorProvider : IActionDescriptorProvider
{private readonly Lazy<IEnumerable<ActionDescriptor>> _accessor;public IEnumerable<ActionDescriptor> ActionDescriptors => _accessor.Value;public ControllerActionDescriptorProvider(IHostEnvironment environment){_accessor = new Lazy<IEnumerable<ActionDescriptor>>(() => GetActionDescriptors(environment.ApplicationName));}private IEnumerable<ActionDescriptor> GetActionDescriptors(string applicationName){var assemblyName = new AssemblyName(applicationName);var assembly = Assembly.Load(assemblyName);foreach (var type in assembly.GetExportedTypes()){if (type.Name.EndsWith("Controller")){var controllerName = type.Name.Substring(0,type.Name.Length - "Controller".Length);foreach (var method in type.GetMethods()){yield return CreateActionDescriptor(method, type, controllerName);}}}}private ControllerActionDescriptor CreateActionDescriptor(MethodInfo method,Type controllerType, string controllerName){var actionName = method.Name;if (actionName.EndsWith("Async")){actionName = actionName.Substring(0, actionName.Length - "Async".Length);}var templateProvider = method.GetCustomAttributes().OfType<IRouteTemplateProvider>().FirstOrDefault();if (templateProvider != null){var routeInfo = new AttributeRouteInfo{Order = templateProvider.Order ?? 0,Template = templateProvider.Template};return new ControllerActionDescriptor{AttributeRouteInfo = routeInfo,ControllerType = controllerType,Method = method};}return new ControllerActionDescriptor{ControllerType = controllerType,Method = method,RouteValues = new Dictionary<string, string>{["controller"] = controllerName,["action"] = actionName}};}
}

简单起见,我们只是将定义在当前应用所在程序集中采用“Controller”后缀命名的类型解析出来,并将定义在它们之中的公共方法作为Action方法(针对Controller和Action方法应该做更为严谨的有效性验证,为了使模拟框架显得更简单一点,我们刻意将这些验证简化了)。我们根据类型和方法解析出Controller名称(类型名称去除“Controller”后缀)和Action名称(方法名去除“Async”后缀),并进一步为每个Action方法创建出对应的ControllerActionDescriptor对象。

如果Action方法上标注了如下这个IRouteTemplateProvider接口类型的特性(比如HttpGetAttribute类型最终实现了该接口),意味着当前Action方法采用“特性路由”,那么最终创建的ControllerActionDescriptor对象的AttributeRouteInfo属性将通过这个特性构建出来。如果没有标注这样的特性,意味着可能会采用约定路由,所以我们需要将当前Controller和Action名称填充到RouteValues属性表示的”必需路由参数值字典”中。

public interface IRouteTemplateProvider
{string Name { get; }string Template { get; }int? Order { get; }
}

IActionDescriptorCollectionProvider

ControllerActionDescriptorProvider类型仅仅是IActionDescriptorProvider接口针对面向Controller的MVC编程模型的实现,Razor Pages编程模型中对应的实现类型为PageActionDescriptorProvider。由于同一个应用是可以同时支持这两种编程模型的,所以这两个实现类型可能会同时注册到应用的依赖注入框架中。MVC框架需要获取两种编程模型的Action,这一个功能体现在如下这个IActionDescriptorCollectionProvider接口上,描述所有类型Action的ActionDescriptor对象通过它的ActionDescriptors属性返回。

public interface IActionDescriptorCollectionProvider
{IReadOnlyList<ActionDescriptor> ActionDescriptors { get; }
}

如下所示的DefaultActionDescriptorCollectionProvider是对IActionDescriptorCollectionProvider接口的默认实现,它直接利用在构造函数中注入的IActionDescriptorProvider对象列表来提供描述Action的ActionDescriptor对象。

public class DefaultActionDescriptorCollectionProvider : IActionDescriptorCollectionProvider
{private readonly Lazy<IReadOnlyList<ActionDescriptor>> _accessor;public IReadOnlyList<ActionDescriptor> ActionDescriptors => _accessor.Value;public DefaultActionDescriptorCollectionProvider(IEnumerable<IActionDescriptorProvider> providers)=> _accessor = new Lazy<IReadOnlyList<ActionDescriptor>>(() => providers.SelectMany(it => it.ActionDescriptors).ToList());    
}

03

路由

当描述Action的所有ActionDescriptor对象被解析出来之后,MVC框架需要将它们转换成表示路由终结点的RoutEndpoint对象。一个RoutEndpoint对象由代表路由模式的RoutePattern对象和代表请求处理器的RequestDelegate对象组成。RoutePattern对象可以直接通过ActionDescriptor对象提供的路由信息构建出来,所以最难解决的是如果创建出用来执行目标Action的RequestDelegate对象。MVC框架中针对Action的执行是通过一个IActionInvoker对象来完成的。

IActionInvoker

MVC框架需要解决的核心问题就是根据请求选择并执行目标Action,所以用来执行Action的IActionInvoker对象无疑是整个MVC框架最为核心的对象。虽然重要性不容置疑,但是IActionInvoker接口的定义却极其简单。如下面的代码片段所示,IActionInvoker接口只定义了一个唯一的InvokeAsync,这是一个返回类型为Task的无参数方法。

public interface IActionInvoker
{Task InvokeAsync();
}

用来执行Action的IActionInvoker对象是根据每个请求上下文动态创建的。具体来说,当路由解析成功并执行匹配终结点的请求处理器时,针对目标Action的上下文对象会被创建出来,一个IActionInvokerFactory对象会被用来创建执行目标Action的IActionInvoker对象。顾名思义,IActionInvokerFactory接口代表创建IActionInvoker对象的工厂,针对IActionInvoker对象的创建体现在如下这个CreateInvoker方法上。

public interface IActionInvokerFactory
{IActionInvoker CreateInvoker(ActionContext actionContext);
}

具体的IActionInvokerFactory对象应该创建怎样的IActionInvoker对象取决于提供的ActionContext上下文。如下面的代码片段所示,ActionContext对象是对当前HttpContext上下文的封装,它的ActionDescriptor属性返回的ActionDescriptor对象是对待执行Action的描述。

public class ActionContext
{public ActionDescriptor ActionDescriptor { get; set; }public HttpContext HttpContext { get; set; }
}

ActionEndpointDataSourceBase

终结点的路由模式可以通过描述Action的ActionDescriptor对象提供的路由信息来创建,它的处理器则可以利用IActionInvokerFactory工厂创建的IActionInvoker对象来完成针对请求的处理,所以我们接下来只需要提供一个自定义的EndpointDataSource类型按照这样的方式为每个Action创建对应的路由终结点就可以了。考虑到两种不同编程模型的差异,我们会定义不同的EndpointDataSource派生类,它们都继承自如下这个抽象的基类ActionEndpointDataSourceBase。

public abstract class ActionEndpointDataSourceBase : EndpointDataSource
{private readonly Lazy<IReadOnlyList<Endpoint>> _endpointsAccessor;protected readonly List<Action<EndpointBuilder>> Conventions;public override IReadOnlyList<Endpoint> Endpoints => _endpointsAccessor.Value;protected ActionEndpointDataSourceBase(IActionDescriptorCollectionProvider provider){Conventions = new List<Action<EndpointBuilder>>();_endpointsAccessor = new Lazy<IReadOnlyList<Endpoint>>(() => CreateEndpoints(provider.ActionDescriptors, Conventions));}public override IChangeToken GetChangeToken() => NullChangeToken.Instance;protected abstract List<Endpoint> CreateEndpoints(IReadOnlyList<ActionDescriptor> actions,IReadOnlyList<Action<EndpointBuilder>> conventions);
}       

MVC框架支持采用全局注册方式的 “约定理由(Conventional Routing )” ,这里的约定路由规则通过Action<EndpointBuilder>对象的列表来体现,对应着ActionEndpointDataSourceBase类型的Conventions属性。ActionEndpointDataSourceBase类型的构造函数中注入了一个IActionDescriptorCollectionProvider对象,我们利用它来获取描述当前应用范围内所有Action的ActionDescriptor对象。Endpoints属性返回的路由终结点列表最终是通过抽象方法CreateEndpoints根据提供的ActionDescriptor对象列表和约定路由列表创建的。对于重写的GetChangeToken方法,我们直接返回如下这个不具有变化监测功能的NullChangeToken对象。

internal class NullChangeToken : IChangeToken
{public bool ActiveChangeCallbacks => false;public bool HasChanged => false;public IDisposable RegisterChangeCallback(Action<object> callback, object state) => new NullDisposable() ;public static readonly NullChangeToken Instance = new NullChangeToken();private class NullDisposable : IDisposable{public void Dispose() {}}
}         

ControllerActionEndpointDataSource

ControllerActionEndpointDataSource是ActionEndpointDataSourceBase的派生类型,它帮助我们完成基于Controller的MVC编程模式下的路由终结点的创建。不过在正式介绍这个类型之前,我们先来介绍两个与 “约定路由” 相关的类型。如下这个ConventionalRouteEntry结构表示单个约定路由的注册项,其中包括路由名称、路由模式、Data Token和排列位置。我们在上面说过,注册的约定路由规则最终体现为一个Action<EndpointBuilder>对象的列表,ConventionalRouteEntry的Conventions属性返回的就是这个列表。

internal struct ConventionalRouteEntry
{public string RouteName;public RoutePattern Pattern { get; }public RouteValueDictionary DataTokens { get; }public int Order { get; }public IReadOnlyList<Action<EndpointBuilder>> Conventions { get; }public ConventionalRouteEntry(string routeName, string pattern,RouteValueDictionary defaults, IDictionary<string, object> constraints,RouteValueDictionary dataTokens, int order,List<Action<EndpointBuilder>> conventions){RouteName = routeName;DataTokens = dataTokens;Order = order;Conventions = conventions;Pattern = RoutePatternFactory.Parse(pattern, defaults, constraints);}
}  

另一个与约定路由相关的是如下这个ControllerActionEndpointConventionBuilder类型,我们从其明明不难看出该类型用来帮助我们构建约定路由。ControllerActionEndpointConventionBuilder是对一个Action<EndpointBuilder>列表的封装,它定义的唯一的Add方法仅仅是向该列表中添加一个表示路由约定的Action<EndpointBuilder>对象罢了。

public class ControllerActionEndpointConventionBuilder : IEndpointConventionBuilder
{private readonly List<Action<EndpointBuilder>> _conventions;public ControllerActionEndpointConventionBuilder(List<Action<EndpointBuilder>> conventions){_conventions = conventions;}public void Add(Action<EndpointBuilder> convention) => _conventions.Add(convention);
}   
   

我们最后来看看ControllerActionEndpointDataSource类型的定义。对于ControllerActionEndpointDataSource对象构建的路由终结点来说,作为请求处理器的RequestDelegate委托对象指向的都是ProcessRequestAsync方法。我们先来看看ProcessRequestAsync方法是如何处理请求的:该方法首先从HttpContext上下文中获取当前终结点的Endpoint对象,并从其元数据列表中得到预先放置的用来表示目标Action的ActionDescriptor对象。接下来,该方法根据HttpContext上下文和这个ActionDescriptor对象创建出ActionContext上下文。该方法最后从基于请求的依赖注入容器中提取出IActionInvokerFactory工厂,并利用它根据当前ActionContext上下文创建出对应的IActionInvoker对象。请求的处理最终通过执行该IActionInvoker得以完成。

public class ControllerActionEndpointDataSource : ActionEndpointDataSourceBase
{private readonly List<ConventionalRouteEntry> _conventionalRoutes;private int _order;private readonly RoutePatternTransformer _routePatternTransformer;private readonly RequestDelegate _requestDelegate;public ControllerActionEndpointConventionBuilder DefaultBuilder { get; }public ControllerActionEndpointDataSource(IActionDescriptorCollectionProvider provider,RoutePatternTransformer transformer) : base(provider){_conventionalRoutes = new List<ConventionalRouteEntry>();_order = 0;_routePatternTransformer = transformer;_requestDelegate = ProcessRequestAsync;DefaultBuilder = new ControllerActionEndpointConventionBuilder(base.Conventions);}public ControllerActionEndpointConventionBuilder AddRoute(string routeName,string pattern, RouteValueDictionary defaults,IDictionary<string, object> constraints, RouteValueDictionary dataTokens){List<Action<EndpointBuilder>> conventions = new List<Action<EndpointBuilder>>();order++;conventionalRoutes.Add(new ConventionalRouteEntry(routeName, pattern, defaults,constraints, dataTokens, _order, conventions));return new ControllerActionEndpointConventionBuilder(conventions);}protected override List<Endpoint> CreateEndpoints(IReadOnlyList<ActionDescriptor> actions,IReadOnlyList<Action<EndpointBuilder>> conventions){var endpoints = new List<Endpoint>();foreach (var action in actions){var attributeInfo = action.AttributeRouteInfo;if (attributeInfo == null) //约定路由{foreach (var route in _conventionalRoutes){var pattern = _routePatternTransformer.SubstituteRequiredValues(route.Pattern, action.RouteValues);if (pattern != null){var builder = new RouteEndpointBuilder(_requestDelegate, pattern, route.Order);builder.Metadata.Add(action);endpoints.Add(builder.Build());}}}else //特性路由{var original = RoutePatternFactory.Parse(attributeInfo.Template);var pattern = _routePatternTransformer.SubstituteRequiredValues(original, action.RouteValues);if (pattern != null){var builder = new RouteEndpointBuilder(_requestDelegate, pattern, attributeInfo.Order);builder.Metadata.Add(action);endpoints.Add(builder.Build());}}}return endpoints;}private Task ProcessRequestAsync(HttpContext httContext){var endpoint = httContext.GetEndpoint();var actionDescriptor = endpoint.Metadata.GetMetadata<ActionDescriptor>();var actionContext = new ActionContext{ActionDescriptor = actionDescriptor,HttpContext = httContext};var invokerFactory = httContext.RequestServices.GetRequiredService<IActionInvokerFactory>();var invoker = invokerFactory.CreateInvoker(actionContext);return invoker.InvokeAsync();}
}

ControllerActionEndpointDataSource定义了一个List<ConventionalRouteEntry类型的字段_conventionalRoutes用来表示存储添加的约定路由注册项。的构造函数中除了注入了用于提供Action描述的IActionDescriptorCollectionProvider对象之外,还注入了用于路由模式转换的RoutePatternTransformer对象。它的_order字段表示为注册的约定路由指定的位置编号,最终会赋值到表示路由终结点的RouteEndpoint对象的Order属性。

在实现的CreateEndpoints方法中,ControllerActionEndpointDataSource会便利提供的每个ActionDescriptor对象,如果该对象的AttributeRouteInfo属性为空,意味着应该采用约定路由,该方法会为每个表示约定路由注册项的ConventionalRouteEntry对象创建一个路由终结点。具体来说,ControllerActionEndpointDataSource会将当前ActionDescriptor对象RouteValues属性携带的路由参数值(包含Controller和Action名称等必要信息),并将其作为参数调用RoutePatternTransformer对象的SubstituteRequiredValues方法将全局注册的原始路由模式(比如“{controller}/{action}/{id?}”)中相应的路由参数替换掉(最终可能变成“Foo/Bar/{id?}”)。SubstituteRequiredValues返回RoutePattern对象将作为最终路由终结点的路由模式。

如果ActionDescriptor对象的AttributeRouteInfo属性返回一个具体的AttributeRouteInfo对象,意味着应该采用特性路由,支持它会利用这个AttributeRouteInfo对象创建一个新的RoutePattern对象将作为最终路由终结点的路由模式。不论是采用何种路由方式,用来描述当前Action的ActionDescriptor对象都会以元数据的形式添加到路由终结点的元数据集合中(对应于Endpoint类型的Metadata属性),ProcessRequestAsync方法中从当前终结点提取的ActionDescriptor对象就来源于此。

ControllerActionEndpointDataSource还提供了一个DefaultBuilder属性,它会返回一个默认的ControllerActionEndpointConventionBuilder对象用来进一步注册约定路由。约定路由可以直接通过调用AddRoute方法进行注册,由于该方法使用自增的_order字段作为注册路由的Order属性,所以先注册的路由具有更高的选择优先级。AddRoute方法同样返回一个ControllerActionEndpointConventionBuilder对象。

如下定义的针对IEndpointRouteBuilder接口的MapMvcControllers扩展方法帮助我们方便地注册ControllerActionEndpointDataSource对象。另一个MapMvcControllerRoute扩展方法则在此基础上提供了约定路由的注册。这两个扩展分别模拟的是MapControllers和MapControllerRoute扩展方法的实现,为了避免命名冲突,我们不得不起一个不同的方法名。

public static class EndpointRouteBuilderExtensions
{public static ControllerActionEndpointConventionBuilder MapMvcControllers(this IEndpointRouteBuilder endpointBuilder){var endpointDatasource = endpointBuilder.ServiceProvider.GetRequiredService<ControllerActionEndpointDataSource>();endpointBuilder.DataSources.Add(endpointDatasource);return endpointDatasource.DefaultBuilder;}public static ControllerActionEndpointConventionBuilder MapMvcControllerRoute(this IEndpointRouteBuilder endpointBuilder, string name, string pattern,RouteValueDictionary defaults = null, RouteValueDictionary constraints = null,RouteValueDictionary dataTokens = null){var endpointDatasource = endpointBuilder.ServiceProvider.GetRequiredService<ControllerActionEndpointDataSource>();endpointBuilder.DataSources.Add(endpointDatasource);return endpointDatasource.AddRoute(name, pattern, defaults, constraints,dataTokens);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/310846.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux rsync删文件速度,Linux下使用rsync最快速删除大量文件的方法

要在Linux下删除海量文件的情况&#xff0c;需要删除三层哈希目录下的几十万个文件。这个时候&#xff0c;删除命令rm -rf * 就不好用了&#xff0c;因为要等待的时间太长。所以必须要采取一些非常手段。我们可以使用rsync来实现快速删除大量文件。安装步骤&#xff1a;1、先安…

[蓝桥杯][算法提高VIP]摆花-多重背包计数问题

题目描述 小明的花店新开张&#xff0c;为了吸引顾客&#xff0c;他想在花店的门口摆上一排花&#xff0c;共m盆。通过调查顾客的喜好&#xff0c;小明列出了顾客最喜欢的n种花&#xff0c;从1到n标号。为了在门口展出更多种花&#xff0c;规定第i种花不能超过ai盆&#xff0c;…

通过极简模拟框架让你了解ASP.NET Core MVC框架的设计与实现[中篇]:请求响应

《200行代码&#xff0c;7个对象——让你了解ASP.NET Core框架的本质》让很多读者对ASP.NET Core管道有了真实的了解。在过去很长一段时间中&#xff0c;有很多人私信给我&#xff1a;能否按照相同的方式分析一下MVC框架的设计与实现原理&#xff0c;希望这篇文章能够满足你们的…

linux strcpy函数实现,strcpy(char *dest , char *src)的漏洞

前言&#xff1a;研究了几天DOS下的溢出原理&#xff0c;最后明白了其实原理都很简单关键是要懂得为什么C语言的strcpy函数有漏洞&#xff0c;为什么对这个函数的不正常使用会造成溢出。一节&#xff1a;介绍strcpy函数能看到这篇文章的人可能都知道问题很多是出在它的身上吧呵…

洛谷 P2853 [USACO06DEC]Cow Picnic S-dfs

输入: 2 4 4 2 3 1 2 1 4 2 3 3 4输出: 2代码如下: #include <iostream> #include <vector> #include <cstring> using namespace std; const int N 1010; bool vis[N]; vector<int>v[N]; int r[N]; int mk[N];void dfs(int u) {mk[u];for (int i …

.NET Core开发实战(第34课:MediatR:轻松实现命令查询职责分离模式(CQRS))--学习笔记(上)...

34 | MediatR&#xff1a;轻松实现命令查询职责分离模式&#xff08;CQRS&#xff09;核心对象IMeditatorIRequese、IRequestIRequestHandler<in TRequest, TResponse>源码链接&#xff1a;https://github.com/witskeeper/geektime/tree/master/samples/MediatorDemo首先…

Linux和unix发行版本,UNIX发展历史和发行版本

[TOC]>[success] # UNIX发展历史和发行版本**UNIX**跟**Linux**之间的关系&#xff0c;用**父子**比喻更为恰当&#xff0c;因为学习了**Linux**以后更有助于以后学习**UNIX**&#xff0c;无论是**Linux**还是**UNIX**的**内核**都是用**C语言**写的&#xff0c;早起是用**汇…

十一届蓝桥杯国赛 玩具蛇-dfs

【问题描述】 小蓝有一条玩具蛇&#xff0c;一共有 16 节&#xff0c;上面标着数字 1 至 16。每一节都是一 个正方形的形状。相邻的两节可以成直线或者成 90 度角。 小蓝还有一个 4 4 的方格盒子&#xff0c;用于存放玩具蛇&#xff0c;盒子的方格上依次标着 字母 A 到 P 共 1…

远程终端管理和检测系统

TerminalMACS(Terminal Manager And Check System)远程终端管理和检测系统本文同步更新地址&#xff1a;https://dotnet9.com/11429.html一、本系统可监控多种终端资源&#xff1a;移动端AndroidiOSPC端WindowsLinuxMac二、整个系统分为三类进程&#xff1a;被控端(Client)被控…

《ASP.NET Core 3 框架揭秘(上下册)》送书结果公告

【免费送书】.Net5实操后的我一夜未眠&#xff0c;来个大胆预测&#xff01;的送书抽奖结果已经出来了&#xff1a;这位中奖的同学尽快填写收货地址&#xff0c;4/2 日还没有完成填写将作废&#xff0c;奖品可是热门的《ASP.NET Core 3 框架揭秘&#xff08;上下册&#xff09;…

十一届蓝桥杯国赛 扩散-多源bfs

【问题描述】 小蓝在一张无限大的特殊画布上作画。 这张画布可以看成一个方格图&#xff0c;每个格子可以用一个二维的整数坐标表示。 小蓝在画布上首先点了一下几个点&#xff1a;(0, 0), (2020, 11), (11, 14), (2000, 2000)。 只有这几个格子上有黑色&#xff0c;其它位置都…

linux下Qt编写串口调试助手,如何在linux下用QT写一个简单的串口调试助手

如何在linux下用QT写一个简单的串口调试助手QT5串口类在QT5以前&#xff0c;编写串口一般使用的是qextserialport类&#xff0c;但在QT5之后有了QT自带的串口类SerialPort(串口基础类)和SerialPortInfo(串口信息类)使用方法pro中添加QT serialport工程中包含相应的头文件#incl…

当代年轻人到底怎么跨越阶层?

0最近大道理讲的有点多&#xff0c;鸡汤灌多了容易腻味&#xff0c;还容易上火。别说你们烦我&#xff0c;我自己讲的也烦&#xff0c;感觉像一个叨逼叨的老头&#xff0c;天天灌一些被90后、00后唾弃的东西。毕竟天天熬鸡汤不仅累还容易熏晕自己。很多东西吧&#xff0c;的确是…

十一届蓝桥杯国赛 美丽的2-枚举

【问题描述】 小蓝特别喜欢 2&#xff0c;今年是公元 2020 年&#xff0c;他特别高兴。 他很好奇&#xff0c;在公元 1 年到公元 2020 年&#xff08;包含&#xff09;中&#xff0c;有多少个年份的数位中 包含数字 2&#xff1f; 代码如下: #include <iostream> using…

分析linux相关日志文件,Linux日志系统与分析.pdf

Linux日志系统与分析1目录 Linux日志系统简介 Linux日志分析 案例介绍&#xff1a; Linux日志入侵发现Linux日志系统简介 日志的主要用途是系统审计、监测追踪和分析统计。 为了保证 Linux 正常运行、准确定位系统问题&#xff0c;认真检查日志文件是管理员的一项非常…

MySQL对JSON类型UTF-8编码导致中文乱码探讨

继上文发表之后&#xff0c;结合评论意见并亲自验证最终发现是编码的问题&#xff0c;但是对于字符编码还是有点不解&#xff0c;于是乎&#xff0c;有了本文&#xff0c;我们来学习字符编码&#xff0c;在学习的过程中&#xff0c;我发现对于MySQL中JSON类型的编码导致数据中文…

Linux 文本格式显示折线图,linux 折线图

可以这样回答&#xff1a;emmm 怎么说呢。数据库就是用来存储数据的仓库&#xff0c;就像我们生活中存放物品的容器一样&#xff0c;但是容器也有通用和专用之分&#xff0c;比如塑料袋就是通用容器&#xff0c;因为塑料袋可以装各种东西&#xff1a;即可以装糖果&#xff0c;也…

十一届蓝桥杯国赛 本质上升序列-dp

【问题描述】 小蓝特别喜欢单调递增的事物。 在一个字符串中&#xff0c;如果取出若干个字符&#xff0c;将这些字符按照在字符串中的顺 序排列后是单调递增的&#xff0c;则成为这个字符串中的一个单调递增子序列。 例如&#xff0c;在字符串 lanqiao 中&#xff0c;如果取出字…

互联网公司的大龄社畜

作者&#xff1a;邹溪源&#xff0c;长沙资深互联网从业者&#xff0c;架构师社区特邀嘉宾&#xff01;一某年&#xff0c;我有幸加入了一家互联网公司B公司。互联网公司无处不充满了奋斗的精神。以此为背景。二有一天&#xff0c;有同事跟我说&#xff1a;你发现没&#xff0c…

LeetCode 1122 数组的相对排序-简单-unordered_map容器的应用

给你两个数组&#xff0c;arr1 和 arr2&#xff0c; arr2 中的元素各不相同 arr2 中的每个元素都出现在 arr1 中对 arr1 中的元素进行排序&#xff0c;使 arr1 中项的相对顺序和 arr2 中的相对顺序相同。未在 arr2 中出现过的元素需要按照升序放在 arr1 的末尾。 示例&#x…