2009-04-15 45 views
2

我正在開發一個帶有許多不同窗體和用戶控件的winforms應用程序。是否有我可以實現的推薦模式,通知用戶當表單/控件退出時以及應用程序關閉時,當前表單/控件上還有未保存的更改?未保存更改的模式

回答

1

我使用LLBL親代的ORM,使具有內置對象一些好的實體跟蹤。 我有種自己推出,似乎工作得很好。 我創建了一個新的界面,我的基本用戶控件和鹼的形式實現:

public interface IClosingNotification 
{ 
    /// <summary> 
    /// True if there is a dirty entity (or a dirty entity in the collection) present 
    /// </summary> 
    bool DirtyEntityPresent { get; } 
    /// <summary> 
    /// Register an entity to be watched for changes 
    /// </summary> 
    /// <param name="entity"></param> 
    void RegisterForClosingNotification(IEntity entity); 
    /// <summary> 
    /// Register a collection to be watched for changes 
    /// </summary> 
    /// <param name="collection"></param> 
    void RegisterForClosingNotification(IEntityCollection collection); 
    /// <summary> 
    /// Returns true if the form should close without any notification 
    /// </summary> 
    /// <returns></returns> 
    bool ShouldClose(); 
} 

在我的基地控制/表單我有我看每個窗體上實體的集合,和我有一個CloseForm()方法在表單關閉時使用的這些類中。 在我的表格中,每當我創建一個對象時,我都可以使用以下命令註冊它: RegisterForClosingNotification(MyCustomer);

它適用於我們的情況。

1
  1. 備忘錄是封裝可撤消變化的方式。

    然後,您可以保留未提交的備忘錄實例的日誌。

    但這通常是複雜的方式。

  2. 狀態通常是最好的。

    您的應用程序有兩個「更改」狀態:保存所有更改,未保存的更改。

    每個州都有基於「更改」和「保存」方法的過渡規則。

    • 「保存」的「保存的所有更改」實施不執行任何操作。

    • 「保存」的未保存更改實施將狀態設置爲「保存所有更改」。

    • 保存的所有更改實施「更改」將狀態設置爲未保存的更改。

    • 未保存更改「更改」的執行沒有任何作用。