简体   繁体   English

为什么 ASP.NET Core 的 Startup 类不是接口或抽象类?

[英]Why is ASP.NET Core's Startup class not an interface or abstract class?

This is in regards to the design principals behind the Startup class explained here:这是关于此处解释的Startup类背后的设计原则:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-2.1 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-2.1

I understand that the class needs to include methods like ConfigureServices or Configure .我知道该类需要包含ConfigureServicesConfigure之类的方法。

Why CreateDefaultBuilder(args).UseStartup<Startup>() does not mandate any base class or interface for better readability?为什么CreateDefaultBuilder(args).UseStartup<Startup>()CreateDefaultBuilder(args).UseStartup<Startup>()要求任何基类或接口以提高可读性?

With this design approach, someone must read the documentation and know about the magic method names like ConfigureServices or Configure .使用这种设计方法,有人必须阅读文档并了解诸如ConfigureServicesConfigure类的神奇方法名称。

If this is part of a new class design mindset, then where can I read more about it?如果这是新类设计思维的一部分,那么我在哪里可以阅读更多相关信息?

There are several reasons why its done the way its done.其完成方式有多种原因。 One of the more obvious reasons is, because you can inject services into Configure method, such as一个比较明显的原因是,因为你可以将服务注入到Configure方法中,比如

public void Configure(IAppBuilder app, IMyService myService)
{
    myService.DoSomething();
}

Obviously, you can't do that with interfaces, abstract classes or inheritence.显然,你不能用接口、抽象类或继承来做到这一点。

The second reason why its done by convention method is, that there is not only Configure/ConfigureServices method, there is an infinite number of environment-dependent configure methods.使用约定方法完成的第二个原因是,不仅有Configure/ConfigureServices方法,还有无数种依赖于环境的配置方法。

public void Configure(IAppBuilder app) { }
public void ConfigureDevelopment(IAppBuilder app) { }
public void ConfigureProduction(IAppBuilder app) { }
public void ConfigureStaging(IAppBuilder app) { }
public void ConfigureSomethingElse(IAppBuilder app) { }

and depending on your environment variable for ASPNET_ENVIRONMENT a different method will be chosen and executed (or the default Configure/ConfigureServices if no matching environment specific method was found).并且根据您的ASPNET_ENVIRONMENT环境变量,将选择并执行不同的方法(如果找不到匹配的环境特定方法,则选择默认的Configure/ConfigureServices )。

None of this is possible with traditional OOP (inheritance/interfaces/abstract classes).使用传统的 OOP(继承/接口/抽象类),这一切都是不可能的。

The same applies to other parts of ASP.NET Core, like Middlewares and the Invoke Method.这同样适用于 ASP.NET Core 的其他部分,如中间件和Invoke方法。 The Invoke method can also have dependencies injected into it, but in order to call the next middleware you simply do Invoke方法也可以注入依赖项,但是为了调用下一个中间件,您只需执行

await next?.Invoke();

and do not have to worry which dependencies the next middleware requires or may take.并且不必担心下一个中间件需要或可能需要哪些依赖项。

And to be complete, one can also have multiple Startup classes with the default method names ( Configure / ConfigureServices ) named StartupDevelopment , StartupProduction , Startup (as fallback) and ASP.NET Core will pick up the correct one based on Environment variable set.为了完整StartupDevelopment ,还可以有多个具有默认方法名称( Configure / ConfigureServices )的Startup类,名为StartupDevelopmentStartupProductionStartup (作为回退),ASP.NET Core 将根据环境变量集选择正确的一个。

Startup class can be inherited from IStartup interface. Startup 类可以继承自 IStartup 接口。

// \packages\microsoft.aspnetcore.hosting.abstractions\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Hosting.Abstractions.dll
namespace Microsoft.AspNetCore.Hosting
{
 public interface IStartup
  {
   IServiceProvider ConfigureServices(IServiceCollection services);
   void Configure(IApplicationBuilder app);
  }
}

By default wizard doesn't create template file with implementation from IStartup.默认情况下,向导不会使用 IStartup 的实现创建模板文件。 Why not - probably mistake or influence of non-typed languages..为什么不 - 可能是非类型化语言的错误或影响..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM