本文介绍了使我的课堂“流利"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我发现,如果我像这样从每个方法返回类实例,就可以模拟一个流畅的接口.

I discovered yesterday that I can simulate a fluent interface if I return the class instance from each method like this...

public class IsThisFluent
{
    public IsThisFluent Stuff()
    {
        //...
        return this;
    }

    public IsThisFluent OtherStuff()
    {
        // ...
        return this;
    }
}

这就是全部吗?

我承认,我是一个头脑很小的熊,我想继续做下去,但是我认为最好与一个大人一起检查一下.

I admit, I'm a bear of very little brain and I want to carry on this this but I thought it might be best to check with a grown up.

我想念什么吗?

是否存在我没​​有用这种模式发现的陷阱"?

Is there a 'gotcha' that I haven't spotted with this pattern?

推荐答案

return this并非全部有效.链接方法是构建流畅的API的一种简单形式,但是流畅的API通常看起来像DSL(特定于域的语言),并且很多很难设计.

return this is not all there is to fluent interfaces.Chaining methods is a simplistic form of building a fluent API, but fluent APIs generally look like DSLs (domain specific languages) and are much, much harder to design.

以起订量为例:

new Mock<IInterface>()
    .Setup(x => x.Method())
    .CallBack<IInterface>(Console.WriteLine)
    .Returns(someValue);

  • 在类型Mock<T>上定义的Setup方法返回ISetup<T, TResult>的实例.

    • The Setup method, defined on the type Mock<T>, returns an instance of ISetup<T, TResult>.

      ICallback<TMock, TResult>定义的Callback方法返回IReturnsThrows<TMock,TResult>的实例.请注意,ISetup<T, TResult>扩展了IReturnsThrows<TMock,TResult>.

      The Callback method, defined for ICallback<TMock, TResult> returns an instance of IReturnsThrows<TMock,TResult>. Note that ISetup<T, TResult> extends IReturnsThrows<TMock,TResult>.

      最后,在IReturns<TMock,TResult>上定义了Returns并返回IReturnsResult<TMock>.另请注意,IReturnsThrows<TMock,TResult>扩展了IReturnsResult<TMock>.

      Finally, Returns is defined on IReturns<TMock,TResult> and returns IReturnsResult<TMock>. Also note that IReturnsThrows<TMock,TResult> extends IReturnsResult<TMock>.

      所有这些细微差别都会迫使您以特定的顺序调用这些方法,并禁止您连续两次调用Setup.或者在调用Setup之前先调用Returns.

      All these little nuances are there to force you to call these methods in a particular order, and to forbid you from calling Setup twice in a row, for example. Or from calling Returns before you call Setup.

      这些细节对于确保良好的用户体验非常重要.

      These details are very important to ensure a good user experience.

      要了解有关设计流畅接口的更多信息,请参阅Martin Fowler在 FluentInterface 上的文章. FluentAssertions 是设计可能变得多么复杂的又一个主要例子-而且结果的可读性更高

      To read more on designing fluent interfaces, take a look at Martin Fowler's article on FluentInterface. FluentAssertions is another prime example of how complex the design might get - but also of how much more readable the outcome is.

      这篇关于使我的课堂“流利"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:50