问题描述
流利接口是一个相当热门的话题这些天。 C#3.0有一些不错的功能(尤其是扩展方法),帮助您使它们。
"Fluent interfaces" is a fairly hot topic these days. C# 3.0 has some nice features (particularly extension methods) that help you make them.
仅供参考,能说一口流利的API意味着每个方法调用返回一些有用的东西,你经常呼吁的方法相同的对象,所以你可以保持链的事情。 Martin Fowler的一个Java的例子。这个概念怪人是这样的:
FYI, a fluent API means that each method call returns something useful, often the same object you called the method on, so you can keep chaining things. Martin Fowler discusses it with a Java example here. The concept kooks something like this:
var myListOfPeople = new List<Person>();
var person = new Person();
person.SetFirstName("Douglas").SetLastName("Adams").SetAge(42).AddToList(myListOfPeople);
我已经看到在C#中的一些非常有用的流利接口(一个例子是用于验证的参数中发现的一口流利的方法在。这让我始料未及,这是能够给予高度可读的语法表达参数的验证规则,而且,如果没有异常,它能够避免初始化任何对象!所以对于正常情况下,有非常小的开销。这一次珍闻教我的庞大量在很短的时间,我想找到更多类似的东西)。
I have seen some incredibly useful fluent interfaces in C# (one example is the fluent approach for validating parameters found in an earlier StackOverflow question I had asked. It blew me away. It was able to give highly readable syntax for expressing parameter validation rules, and also, if there were no exceptions, it was able to avoid instantiating any objects! So for the "normal case", there was very little overhead. This one tidbit taught me a huge amount in a short time. I want to find more things like that).
所以,我想通过看和讨论一些很好的例子,以了解更多信息。因此,什么是你做还是在C#中看到了一些优秀的流畅接口,是什么让他们如此重要?
So, I'd like to learn more by looking at and discussing some excellent examples. So, what are some excellent fluent interfaces you've made or seen in C#, and what made them so valuable?
感谢。
推荐答案
荣誉的方法参数验证,你给了我为我们的API流利了新的思路。我反正恨我们的先决条件检查...
Kudos for the method parameter validation, you've given me a new idea for our fluent APIs. I've hated our precondition checks anyways...
我已经建立了一个可扩展系统的开发,在那里你可以流利地介绍了相关命令可用的新的产品,用户界面元素和更多。这个运行在StructureMap和FluentNHibernate,这是很好的API太顶部
I've built a extensibility system for a new product in development, where you can fluently describe the commands available, the user interface elements and more. This runs on top of StructureMap and FluentNHibernate, which are nice APIs too.
MenuBarController mb;
// ...
mb.Add(Resources.FileMenu, x =>
{
x.Executes(CommandNames.File);
x.Menu
.AddButton(Resources.FileNewCommandImage, Resources.FileNew, Resources.FileNewTip, y => y.Executes(CommandNames.FileNew))
.AddButton(null, Resources.FileOpen, Resources.FileOpenTip, y =>
{
y.Executes(CommandNames.FileOpen);
y.Menu
.AddButton(Resources.FileOpenFileCommandImage, Resources.OpenFromFile, Resources.OpenFromFileTop, z => z.Executes(CommandNames.FileOpenFile))
.AddButton(Resources.FileOpenRecordCommandImage, Resources.OpenRecord, Resources.OpenRecordTip, z => z.Executes(CommandNames.FileOpenRecord));
})
.AddSeperator()
.AddButton(null, Resources.FileClose, Resources.FileCloseTip, y => y.Executes(CommandNames.FileClose))
.AddSeperator();
// ...
});
和您可以配置提供这样的所有命令:
And you can configure all commands available like this:
Command(CommandNames.File)
.Is<DummyCommand>()
.AlwaysEnabled();
Command(CommandNames.FileNew)
.Bind(Shortcut.CtrlN)
.Is<FileNewCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
Command(CommandNames.FileSave)
.Bind(Shortcut.CtrlS)
.Enable(WorkspaceStatusProviderNames.DocumentOpen)
.Is<FileSaveCommand>();
Command(CommandNames.FileSaveAs)
.Bind(Shortcut.CtrlShiftS)
.Enable(WorkspaceStatusProviderNames.DocumentOpen)
.Is<FileSaveAsCommand>();
Command(CommandNames.FileOpen)
.Is<FileOpenCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
Command(CommandNames.FileOpenFile)
.Bind(Shortcut.CtrlO)
.Is<FileOpenFileCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
Command(CommandNames.FileOpenRecord)
.Bind(Shortcut.CtrlShiftO)
.Is<FileOpenRecordCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
我们的视图配置自己的控件使用由工作区给他们一个服务命令标准的编辑菜单,他们只是告诉它观察他们:
Our view configure their controls for the standard edit menu commands using a service given to them by the workspace, where they just tell it to observe them:
Workspace
.Observe(control1)
.Observe(control2)
如果用户按Tab键的控件,工作区将自动获得相应的适配器用于控制和提供了撤销/重做和剪贴板操作。
If the user tabs to the controls, the workspace automatically gets an appropriate adapter for the control and provides undo/redo and clipboard operations.
它帮助我们大大减少了设置代码,使其更具有可读性。
It has helped us reduce the setup code dramatically and make it even more readable.
我忘了告诉我们使用在我们的WinForms MVP的模型演示验证意见库:的。真的很容易,真的可测试的,真好看!
I forgot to tell about a library we're using in our WinForms MVP model presenters to validate the views: FluentValidation. Really easy, really testable, really nice!
这篇关于你有没有提出或C#的非常有价值的所见所闻流利的接口?什么是如此之大他们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!