DSL/流畅接口的意义何在 [英] What's the point of DSLs / fluent interfaces

查看:25
本文介绍了DSL/流畅接口的意义何在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近观看了关于如何创建流畅的 DSL 的网络广播,我有承认,我不明白人们为什么会使用这种方法(至少对于给定的例子).

I was recently watching a webcast about how to create a fluent DSL and I have to admit, I don't understand the reasons why one would use such an approach (at least for the given example).

网络广播提供了一个图像大小调整类,它允许您使用以下语法(使用 C#)指定输入图像、调整其大小并将其保存到输出文件:

The webcast presented an image resizing class, that allows you to specify an input-image, resize it and save it to an output-file using the following syntax (using C#):

Sizer sizer = new Sizer();
sizer.FromImage(inputImage)
     .ToLocation(outputImage)
     .ReduceByPercent(50)
     .OutputImageFormat(ImageFormat.Jpeg)
     .Save();

我不明白这比采用一些参数的传统"方法有什么好处:

I don't understand how this is better than a "conventional" method that takes some parameters:

sizer.ResizeImage(inputImage, outputImage, 0.5, ImageFormat.Jpeg);

从可用性的角度来看,这似乎更容易使用,因为它清楚地告诉您该方法期望什么作为输入.相比之下,使用流畅的界面,没有什么能阻止您省略/忘记参数/方法调用,例如:

From a usability point of view, this seems a lot easier to use, since it clearly tells you what the method expects as input. In contrast, with the fluent interface, nothing stops you from omitting/forgetting a parameter/method-call, for example:

sizer.ToLocation(outputImage).Save();

接下来是我的问题:

1 - 有没有办法提高流畅界面的可用性(即告诉用户他应该做什么)?

1 - Is there some way to improve the usability of a fluent interface (i.e. tell the user what he is expected to do)?

2 - 这种流畅的接口方法是否只是替代 C# 中不存在的命名方法参数?命名参数是否会使流畅的接口过时,例如类似的 Objective-C 提供的东西:

2 - Is this fluent interface approach just a replacement for the non existing named method parameters in C#? Would named parameters make fluent interfaces obsolete, e.g. something similar objective-C offers:

sizer.Resize(from:input, to:output, resizeBy:0.5, ..)

3 - 流畅的界面是否仅仅因为它们目前很流行而被过度使用?

3 - Are fluent interfaces over-used simply because they are currently popular?

4 - 或者这只是为网络广播选择的一个不好的例子?在这种情况下,请告诉我这种方法的优点是什么,在哪里使用它有意义.

4 - Or was it just a bad example that was chosen for the webcast? In that case, tell me what the advantages of such an approach are, where does it make sense to use it.

顺便说一句:我知道 jquery,并且看到它使事情变得多么容易,所以我不是在寻找关于它或其他现有示例的评论.

BTW: I know about jquery, and see how easy it makes things, so I'm not looking for comments about that or other existing examples.

我更多的是在寻找一些(一般性)评论来帮助我理解(例如)何时实现流畅的接口(而不是经典的类库),以及在实现时要注意什么.

I'm more looking for some (general) comments to help me understand (for example) when to implement a fluent interface (instead of a classical class-library), and what to watch out for when implementing one.

推荐答案

2 - 这是流畅的界面方法吗只是替代非中现有的命名方法参数C#?命名参数会变得流畅吗接口已过时,例如某物类似的 Objective-C 优惠:

2 - Is this fluent interface approach just a replacement for the non existing named method parameters in C#? Would named parameters make fluent interfaces obsolete, e.g. something similar objective-C offers:

好吧,是和不是.流畅的界面为您提供了更大的灵活性.使用命名参数无法实现的事情是:

Well yes and no. The fluent interface gives you a larger amount of flexibility. Something that could not be achieved with named params is:

sizer.FromImage(i)
 .ReduceByPercent(x)
 .Pixalize()
 .ReduceByPercent(x)
 .OutputImageFormat(ImageFormat.Jpeg)
 .ToLocation(o)
 .Save();

流体界面中的 FromImage、ToLocation 和 OutputImageFormat,对我来说有点味道.相反,我会按照这些思路做一些事情,我认为这要清楚得多.

The FromImage, ToLocation and OutputImageFormat in the fluid interface, smell a bit to me. Instead I would have done something along these lines, which I think is much clearer.

 new Sizer("bob.jpeg") 
 .ReduceByPercent(x)
 .Pixalize()
 .ReduceByPercent(x)
 .Save("file.jpeg",ImageFormat.Jpeg);

Fluent 接口与许多编程技术存在相同的问题,它们可能被误用、过度使用或未充分利用.我认为当这种技术被有效地使用时,它可以创建一个更丰富、更简洁的编程模型.甚至 StringBuilder 也支持它.

Fluent interfaces have the same problems many programming techniques have, they can be misused, overused or underused. I think that when this technique is used effectively it can create a richer and more concise programming model. Even StringBuilder supports it.

var sb = new StringBuilder(); 
sb.AppendLine("Hello")
 .AppendLine("World"); 

这篇关于DSL/流畅接口的意义何在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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