什么是DSL的中/流利的接口点 [英] What's the point of DSLs / fluent interfaces

查看:134
本文介绍了什么是DSL的中/流利的接口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近看一个关于直播如何建立一个流畅的DSL ,然后我不得不承认,我不不懂为什么人们会用这种方式(至少给出的例子)的原因。



网上直播呈现的图像缩放类,允许你指定一个输入图像,调整其大小和(使用C#)将其保存到使用以下语法一个输出文件:

 仪分级机=新仪(); 
sizer.FromImage(inputImage的)
.ToLocation(outputImage)
.ReduceByPercent(50)
.OutputImageFormat(ImageFormat.Jpeg)
.Save();



我不明白这是怎么多了一个传统的方法,需要一些更好的参数:

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



从一个易用性来看,这似乎是一个更容易使用,因为它清楚地告诉你什么该方法期望输入。与此相反,流畅的界面,没有什么可以阻止你忽略/忘记一个参数/方法调用,例如:

 分级机.ToLocation(outputImage).Save(); 



所以,在我的问题:



1? - 有一些方法来提高流畅的界面的可用性(也就是告诉用户什么他应该做的)



< STRONG> 2 - 这是一口流利的接口方式只是在C#中不存在的命名方法参数的替代品?将命名参数使流畅的界面已经过时,例如类似的东西客观-C提供:

  sizer.Resize(来自:输入到:输出,resizeBy:0.5,..) 

3 - 精通接口过度使用,只是因为他们是目前流行?



4 - 抑或是只是被选为网络直播了一个坏榜样?在这种情况下,告诉我这种方法的优点是,在是否有意义使用它



BTW:我知道jQuery的,看看如何轻松它使事情,所以我不是在寻找有关意见或其他现有的例子。



我更找一些(一般)的意见,以帮助我了解(例如)何时实现流畅的界面(而不是传统的类库),并注意什么实现一个时。


解决方案

2 - 这是一口流利的接口方法
只是针对非
存在于
C#命名方法参数的替代品?将命名参数使流畅
接口已经过时,例如什么
相似的目标-C提供:




恩yes和no。在流畅的界面为您提供了灵活性较大的数量。这不能与名为PARAMS可以实现的东西是:



  sizer.FromImage(我)
.ReduceByPercent(X)
.Pixalize()
.ReduceByPercent(X)
.OutputImageFormat(ImageFormat.Jpeg)
.ToLocation(O)
.Save();



FromImage,ToLocation和OutputImageFormat流体界面,闻了一下给我。相反,我会做沿着这些路线,我认为这是更清晰的东西。

 新仪(bob.jpeg) 
.ReduceByPercent(X)
.Pixalize()
.ReduceByPercent(X)
.Save(file.jpeg,ImageFormat.Jpeg);



流利的接口有同样的问题很多编程技术都有,他们可能被滥用,过度使用或使用不足。我认为,当这种技术被有效地使用它可以创建一个更丰富,更简洁的编程模型。即使StringBuilder的支持它。

  VAR SB =新的StringBuilder(); 
sb.AppendLine(你好)
.AppendLine(世界);


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).

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();

So on to my questions:

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 - 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 - Are fluent interfaces over-used simply because they are currently popular?

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.

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 - 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();

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 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天全站免登陆