![](/img/trans.png)
[英]Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side
[英]Where evaluate invariants after and before call a routine?
在契約式設計中,class 不變量必須在兩種情況下得到滿足:在創建 object 之后和調用例程之后。 是否有任何示例或條件,我也必須在調用例程之前進行評估?
在功能調用之前,可以違反 class 不變量。 條件可能不同,我只介紹最明顯的條件:
別名。 object 引用了涉及 class 不變量的其他一些 object 並且其他 ZA8CFDE6331BD59EB26666F8911ZC4 被第三方修改:
class SWITCH -- Creation procedure is ommitted for brevity. feature toggle1, toggle2: TOGGLE -- Mutually exclusive toggles. ... invariant toggle1.is_on = not toggle2.is_on end
現在下面的代碼違反了 class SWITCH
的不變量:
switch.toggle1.turn_on -- Make `switch.toggle1.is_on = True` switch.toggle2.turn_on -- Make `switch.toggle2.is_on = True` switch.operate -- Class invariant is violated before this call
外部 state。 object 與在 class 不變量中引用的外部數據耦合,並且可能會意外更改:
class TABLE_CELL feature item: DATA do Result:= cache -- Attempt to use cached value. if not attached Result then -- Load data from the database (slow). Result:= database.load_item (...) cache:= Result end end feature {NONE} -- Storage cache: detachable DATA invariant consistent_cache: -- Cache contains up-to-date value. attached cache as value implies value ~ database.load_item (...) end
現在,如果在應用程序外部修改數據庫,緩存可能會變得不一致,並且在以下功能調用之前觸發 class 不變違規:
data:= table_cell.item -- Class invariant is violated before this call.
打回來。 object 可以傳遞給無效 state 中的另一個:
class HANDLER feature process (s: STRUCTURE) do... -- Some code that sets `is_valid` to False. s.iterate_over_elements (Current) end process_element (e: ELEMENT) do... end is_valid: BOOLEAN do... end invariant is_valid end
由 class STRUCTURE
的功能iterate_over_elements
執行的對HADNLER
的回調會導致不變量違規,因為handler
狀況不佳:
handler.process_element (...) -- Class invariant is violated before the call.
可以說所有情況都是由於軟件錯誤和缺陷造成的,但這正是 class 不變量的目的,以捕捉包括在功能調用之前發生違規的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.