Why the required Startup class does't need to implement an appropriate interface, like IStartup?

拥有回忆 提交于 2020-01-03 13:01:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!