较长或较短的语法哪个更流畅?
我正在尝试创建我的第一个流畅的界面,我只是想知道其他人认为哪个更流畅以及他们更喜欢使用哪个?
Check.Field().Named("est").WithValueOf("sdsd").IsNotNull()
Check.Field("est").WithValueOf("sdsd").IsNotNull()
Check.Field("est").WithValue("sdsd").IsNotNull()
干杯 安东尼
I am trying to create my first fluent interface and I was just wondering what other poeple thought was more fluent and which one they would prefer to use?
Check.Field().Named("est").WithValueOf("sdsd").IsNotNull()
Check.Field("est").WithValueOf("sdsd").IsNotNull()
Check.Field("est").WithValue("sdsd").IsNotNull()
Cheers
Anthony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最后一张肯定是:
Last one definitely:
我同意:
尽可能短,同时仍然有意义。
避免像
.as 这样的干扰词。 。的。 。和。 .in.
除非它们添加了上下文含义。 我见过执行此操作的流畅界面,除了更多的输入和应用程序执行时要跳过的更多环之外,它没有添加任何有用的东西。I concur:
As short as possible, while still making sense.
Avoid noise words like
.as. .of. .and. .in.
unless they add contextual meaning. I've seen fluent interfaces that do this, and it adds nothing useful except more typing and more hoops for the application to jump through when it executes.另一种选择可能是:
使用谓词调用 LINQ 的 FirstOrDefault 之类的东西,而不是过滤然后调用 FirstOrDefault。
Another option might be:
Something like LINQ's FirstOrDefault called with a predicate instead of filtering and then calling FirstOrDefault.
什么是
检查
? 我怀疑根本不需要。 我理解您想要做什么,但请记住您尝试构建的表达式仍然可能以 if 语句结束。 考虑到这一点,请考虑“if check X”如何读。 不太好,恕我直言。 此外,“Field”函数必须做什么才能解析该名称? 您是否在其他地方可以以更好的方式呈现(也许是某些基本类型的扩展方法?)What is
Check
? I suspect it's not needed at all. I understand what you're trying to do, but remember that the expression you're trying to build will still likely end up in an if statement. With that in mind, think about how "if check X" reads. Not too well, imho. Additionally, what does the 'Field' function have to do to resolve that name? Do you have that somewhere else that you could present in a nicer way (perhaps an extension method on some base type?)或者,要模仿某些集合,请使用 Item property:
有些人可能还会说你应该只使用 .IsNull() 并否定整个表达式,就像这样,但这是一个“六对半打”的事情:
Or, to mimic some of the collections, use of Item property:
Some might also say you should just use .IsNull() and negate the whole expression, like this, but that's a "six -vs- half a dozen" thing: