问题
Using katana, why does the Startup class should not implement a respective interface, like for example:
interface IStartup
{
void Configuration(IAppBuilder app);
}
public class MyStartup : IStartup
{
public void Configuration(IAppBuilder app)
{
...
}
}
I think that it could be much more intuitive for the developers to understand what they should provide with to the WebApp.Start<T>
method as the T argument instead of guessing and looking for examples, it should be more explicit:
public void Start<T>() where T : IStartup
回答1:
The reason is "there's NO GOOD reason". Interfaces exist to communicate structure and purpose to an implementer (abstract classes do this as well, along with providing some minimal behavior). Without them, we're left with convention. In this case, by not constraining TStartup, OWIN is allowing you to use any nonsense Startup class and can only tell you at runtime if it will work. For example:
WebApp.Start<string>(BaseAddress);
This compiles fine but throws an EntryPointNotFoundException at runtime (No 'Configuration' method was found in class 'System.String).
Not to get all philosophical, but I see this as a general trend in computing today. REST, with it's no contracts, no guarantees, you figure it out paradigm is in; SOAP is out. In some ways this is a good thing, but I don't think this example is one of them.
来源:https://stackoverflow.com/questions/26368805/why-the-required-startup-class-doest-need-to-implement-an-appropriate-interface