成长共勉 2020-01-18
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
};
} 第一次打包,还成功了,可能也是运气问题吧,因为自己在百度的时候发现很多人都打包有问题,现在我就讲述下我的打包配置。Opening index.html over file:// won‘t work.