传递动态参数打破流畅的接口 [英] Passing dynamic arguments breaks fluent interfaces
问题描述
我有一组所谓的流畅接口,所以我可以使用这样的语法:
I have a set of so called fluent interfaces, so I can use syntax like this:
a.With(abc ).Then(that);
a.With("abc").Do("this").Then("that);
每个方法都返回一个对象转换到一个相应的接口在设计时我可以使用Intellisense轻松地在API方法之间导航,如果我把其中一个参数转换为动态的:
Each method returns an object cast to a corresponding interface. At design time I can use Intellisense to easily navigate between API methods. However I can no longer do it if I cast one of the arguments to a dynamic:
a.With((dynamic)abc)Do(this)。然后(that);
a.With((dynamic)"abc").Do("this").Then("that);
不仅我在设计时失去了Intellisense,此转型会影响运行时执行:所有后续调用使用动态类型的返回对象可能打破执行逻辑。
Not only I lose Intellisense at design time, this cast affects runtime execution: all subsequent calls after With return objects of a dynamic type potentially breaking the execution logic.
我不明白为什么一个动态参数影响只使用静态类型的契约。如果方法With设计为返回一个ISomeInterface的实例,并且实现返回SomeClass(实现ISomeInterface),为什么在不相关的地方使用的动态对象会感染所有后续的调用链?有什么方法可以防止吗?
What I fail to understand is why should a dynamic argument affect contracts that only use static types. If a method With is designed to return an instance of ISomeInterface and the implementation returns SomeClass (that implements ISomeInterface), why should a dynamic object used in unrelated place infect all subsequent call chain? Is there any way to prevent it?
推荐答案
我相信这篇文章Erik Lippert回答一切:
I believe this post by Erik Lippert answer it all:
http://blogs.msdn.com/b/ericlippert/archive/2012/10/22/a-method-group-of-one.aspx
这篇关于传递动态参数打破流畅的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!