为什么所需的启动类不需要实现适当的接口,如IStartup? [英] Why the required Startup class does't need to implement an appropriate interface, like IStartup?

查看:304
本文介绍了为什么所需的启动类不需要实现适当的接口,如IStartup?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用武士刀,为什么在启动类不应该实现各自的接口,例如像:

 接口IStartup 
{
无效配置(IAppBuilder应用);
}

公共类MyStartup:IStartup
{
公共无效配置(IAppBuilder应用程序)
{
...
}
}

我觉得这可能是更直观的开发者了解什么

$ b:; T> 方法的T参数,而不是猜测和寻找例子,它应该是更加明确他们应该对 WebApp.Start<提供
$ b

 公共无效开始< T>()其中T:IStartup 


解决方案

原因是没有正当理由。接口存在的结构和用途进行通信,以实现者(抽象类也这么做,以提供一些最起码的行为一起)。没有他们,我们留下了一个约定。在这种情况下,通过不限制TSTARTUP,OWIN是允许你使用任何废话启动类,并只能告诉你在运行时是否会工作。例如:

  WebApp.Start<串GT;(BaseAddress); 

这编译罚款,但抛出在运行时EntryPointNotFoundException(没有配置方法在课堂上被发现系统.String)。



不要让所有的哲学,但我认为这是在当今的计算大势所趋。 REST,它是没有合同,没有保障,你看着办吧范式是; SOAP是的。在某些方面,这是一个很好的事情,但我不认为这例子是其中之一。


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

解决方案

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.

这篇关于为什么所需的启动类不需要实现适当的接口,如IStartup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆