在開發模塊化應用程序時,顯然我們需要使用Fail-fast系統嗎?在開發模塊化應用程序時使用失效快速方法
當創建模塊時,如果出現錯誤條件模塊無法處理,它應該報告錯誤(如拋出異常..),而不用擔心誰會處理它。看起來這可以在開發模塊時用作指導。這有什麼問題嗎?
編輯:例
在的Module.dll
public class SomeClass:ISomeInterface
{
public void CreateFile(string filename)
{
//The module have no idea who calls this. But there is something wrong
//somewhere so throw an exception early. The module writer has no control over
//how the exception is handled. So if this exception is not handled by the
//Client Application the application can potentially crash.Do he need to worry
//about that?
if(filename == null)
{
throw new ArgumentNullException("Filename is null");
}
//I think the following is bad. This code is making sure that a module
//exception wont crash the application.Is it good?
//if(filename ==null)
//{
//Logger.log("filename is null");
//return;
//}
}
}
+1,有趣。我需要知道是否有任何方法可以殺死故障子系統?對於我們的應用程序,我們根據類型爲XML文件提供不同的GUI設計器。這些GUI設計器被創建爲模塊(子系統)。如果因爲任何原因設計人員無法工作,我希望特定的設計人員不要使用應用程序,讓系統的其餘部分按預期工作。但是這裏的問題是子系統如果在無法處理的錯誤的情況下死亡? – Jimmy 2012-04-25 15:52:14
@Jimmy:一種方法是擁有一個主'IsAlive'屬性(可能由'_IsAlive'變量支持),並且擁有將使用對象測試該變量的方法。另一種方法(也可以與'IsAlive'一起使用)是使子系統方法將要使用的字段無效,可能使它們無效,或者可能用對虛擬對象的引用來替代它們,當試圖是嘗試時會引發更多信息異常使用它們。對於由鎖保護的數據結構,將鎖包裹在包含「危險」標誌的警衛中可能是有用的... – supercat 2012-04-25 16:58:14
@Jimmy:...有代碼將(有希望暫時)違反不變應該在鎖定釋放時設置危險標誌,並在不變量恢復時將其清除。在設置危險標誌時調用鎖定保護裝置上的Dispose將使基礎數據結構無效,然後清除鎖定;因此,想要使用數據結構的其他代碼不會永遠等待一個永遠不會被釋放的鎖,而是也不會訪問損壞的數據。 – supercat 2012-04-25 17:00:05