• .netCore 源码看Build 模式


    .netCore 源码:

    https://github.com/dotnet/aspnetcore

    这是扩展,现在扩展的很多代码,移到其他的Resposities 中了 

    https://github.com/dotnet/extensions

    Build 模式,比较简单,就是有一个Builder .她会有很多零件,还有一个Build 方法,可以把这些零件生成一个 实现 IProvider 的对象。简单示例如下(摘自Eleven 老师)

       public class BuilderVolkswagen : AbstractBuilder
        {
            private Engine _Engine = null;
            private Wheels _Wheels = null;
            private Light _Light = null;
    
            public override void Engine()
            {
                this._Engine = new Engine()
                {
                    Name = "_Engine"
                };
    
                Console.WriteLine("{0} build Engine", this.GetType().Name);
            }
    
            public override void Wheels()
            {
                this._Wheels = new Wheels()
                {
                    Name = "_Wheels"
                };
                Console.WriteLine("{0} build Wheels", this.GetType().Name);
            }
    
            public override void Light()
            {
                this._Light = new Light()
                {
                    Name = "_Light"
                };
                Console.WriteLine("{0} build Light", this.GetType().Name);
            }
    
            public override Car Build()
            {
                Console.WriteLine("组装 {0} {1} {2}", this._Engine, this._Light, this._Wheels);
                Console.WriteLine("{0} build CC", this.GetType().Name);
    
                return new Car(this._Engine, this._Light, this._Wheels)
                {
                    Name = "CC"
                };
            }
        }


    public abstract class AbstractBuilder
    {

    
    

    public abstract void Engine();

    
    

    public abstract void Wheels();

    
    

    public abstract void Light();

    
    

    public abstract Car Build();
    }

    AbstractBuilder builder = new BuilderVolkswagen();
                        builder.Build();

    .NetCore 的也相似:

     public class Program
        {
            public static void Main(string[] args)
            {
                CreateWebHostBuilder(args).Build().Run();
            }
    
            public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>();
        }

    这个模式的特点在于,可以在Builder 内部,增加零件,而上层不用改动。在.Netcore上面代码中,把某些零件的创建暴露给上层。在配置中,有同样的用法:

     //
            // 摘要:
            //     /// Initializes a new instance of the Microsoft.AspNetCore.Hosting.WebHostBuilder
            //     class. ///
            public WebHostBuilder()
            {
                _hostingEnvironment = new HostingEnvironment();
                _configureServicesDelegates = new List<Action<WebHostBuilderContext, IServiceCollection>>();
                _configureAppConfigurationBuilderDelegates = new List<Action<WebHostBuilderContext, IConfigurationBuilder>>();
                _config = new ConfigurationBuilder().AddEnvironmentVariables("ASPNETCORE_").Build();
                if (string.IsNullOrEmpty(GetSetting(WebHostDefaults.EnvironmentKey)))
                {
                    UseSetting(WebHostDefaults.EnvironmentKey, Environment.GetEnvironmentVariable("Hosting:Environment") ?? Environment.GetEnvironmentVariable("ASPNET_ENV"));
                }
                if (string.IsNullOrEmpty(GetSetting(WebHostDefaults.ServerUrlsKey)))
                {
                    UseSetting(WebHostDefaults.ServerUrlsKey, Environment.GetEnvironmentVariable("ASPNETCORE_SERVER.URLS"));
                }
                _context = new WebHostBuilderContext
                {
                    Configuration = _config
                };
            }
    气功波(18037675651)
  • 相关阅读:
    A/B test模块使用以及配置
    Linux下的tar压缩解压缩命令详解
    php mysql转义特殊字符的函数(转)
    cas server + cas client 单点登录 原理介绍
    php二维码的生产以及下载
    shiro 获取已经登录的用户
    Sql 根据多条件查询重复的数据 MySQL删除重复数据
    js base64 转PDF并下载,js baser64转图片并下载
    当同时安装Python2和Python3后,如何兼容并切换使用详解(比如pip使用)
    我的博客初始“话”
  • 原文地址:https://www.cnblogs.com/qgbo/p/12208591.html
Copyright © 2020-2023  润新知