在 C# 3 中编写流畅接口的技巧 [英] Tips for writing fluent interfaces in C# 3

查看:19
本文介绍了在 C# 3 中编写流畅接口的技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关 C# 中流畅界面的一些好技巧.我自己只是在学习它,但很想听听我正在阅读的文章之外的其他人的想法.特别是我追求:

I'm after some good tips for fluent interfaces in C#. I'm just learning about it myself but keen to hear what others think outside of the articles I am reading. In particular I'm after:

  1. 什么时候流利太多了?
  2. 有流畅的模式吗?
  3. C# 中什么使流畅的接口更加流畅(例如扩展方法)
  4. 复杂的流畅界面仍然是流畅的界面吗?
  5. 重构以获得流畅的界面或重构现有的流畅界面
  6. 有没有您曾经合作过或可以推荐的好例子?

如果您可以发布一个提示或想法,或者每个帖子中的任何内容.我也想看看他们是如何被投票的.

If you could post one tip or thought, or whatever per post. I want to see how they get voted on, too.

提前致谢.

推荐答案

关于你的第 4 点;

是的,我认为复杂的流畅界面仍然可以流畅.

Yes I think that a complex fluent interface can still be fluent.

我认为流畅的界面在某种程度上是一种妥协.(虽然不错!)已经有很多关于使用自然语言进行编程的研究,但通常自然语言不足以表达程序.

I think fluent interfaces are somewhat of a compromise. (although a good one!) There has been much research into using natural language for programming and generally natural language isn't precise enough to express programs.

Fluent 接口的构造使它们像编程语言一样编写,只允许使用自然语言表达的一小部分内容,但它们读起来像自然语言.

Fluent interfaces are constructed so that they write like a programming language, only a small subset of what you can express in a natural language is allowed, but they read like a natural language.

例如,如果您查看犀牛模拟,与普通库相比,编写部分已经很复杂.由于流畅的界面,我花了更长的时间来学习,但它使代码更易于阅读.因为程序通常只编写一次并且读取多次,所以这是一个很好的权衡.

If you look at rhino mocks for example the writing part has been complicated compared to a normal library. I took me longer to learn mostly due to the fluent interface but it makes code a lot easier to read. Because programs are usually written once and read a lot more than once this is a good tradeoff.

所以稍微限定一下我的观点.编写复杂但易于阅读的流畅界面仍然可以流畅.

So to qualify my point a bit. A fluent interface that's complex to write but easy to read can still be fluent.

这篇关于在 C# 3 中编写流畅接口的技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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