Implementing conditional in a fluent interface

*爱你&永不变心* 提交于 2019-12-03 15:41:32
Ball

This is known as the finishing problem when method chaining Try this

TicketRules
.RequireValidation()
.When(quartType => quartType == QuartType.Before,
      rule => rule.TotalMilageIs(64))
.When(quartType => quartType == QuartType.After,
      rule => rule.TotalMilageIs(128));

It looks a little odd at first, but it wraps your conditionals into a different scope so you can conditionally execute them. Think about it like creating your own if block. By closing it, you know when you can "finish" a sub statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!