码迷,mamicode.com
首页 > Web开发 > 详细

.netCore 源码看Build 模式

时间:2020-01-18 13:09:58      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:ble   configure   摘要   ali   action   cti   col   ram   static   

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
            };
        }

.netCore 源码看Build 模式

标签:ble   configure   摘要   ali   action   cti   col   ram   static   

原文地址:https://www.cnblogs.com/qgbo/p/12208591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!