我設計一個流暢的API和使用情況處置對象是有點像這樣:從同一個對象
IUser user = work
.Timeout(TimeSpan.FromSeconds(5))
.WithRepository(c => c.Users)
.Do(r => r.LoadByUsername("matt"))
.Execute();
所以,讓我們說,work
是IUnitOfWork
型的,而是叫IActionFlow<IUserRepository>
WithRepository(c => c.Users)
返回一個接口中的方法這是IDisposable
。
當我打電話給Execute()
並得到最終結果時,我失去了對該IActionFlow<IUserRepository>
實例的引用,所以我無法處理它。
使實例在Execute()
方法中自行展開有什麼缺點?
喜歡的東西:
public TResult Execute()
{
// ...
Dispose();
return result;
}
的代碼看起來編譯得很好,但我在尋找怪異的行爲或錯誤可能上升,因爲這一點。是不是很差勁?
嗯,它只是意味着你返回一個處理對象,這看起來很奇怪:)。流暢接口有一些問題:https://ocramius.github.io/blog/fluent-interfaces-are-evil/有一個完整的討論在這裏使用流暢的接口來配置模式:https:// davefancher。com/2015/06/14/functional-c-fluent-interfaces-and-functional-method-chaining/ –