未保存更改的模式

时间:2009-04-15 10:13:09

标签: winforms design-patterns

我正在开发一个包含许多不同表单和用户控件的winforms应用程序。是否有一个我可以实现的推荐模式,当表单/控件退出时以及应用程序关闭时,通知用户当前表单/控件上有未保存的更改?

2 个答案:

答案 0 :(得分:1)

  1. Memento 是一种封装可撤消更改的方法。

    然后,您可以记录未提交的memento实例。

    但这通常是复杂的方式。

  2. 通常最好。

    您的应用程序有两种“更改”状态:已保存所有更改,未保存的更改。

    每个州都有一个基于“更改”和“保存”方法的转换规则。

    • “保存”的“保存所有更改”实施不执行任何操作。

    • “保存”的未保存更改实施将状态设置为“已保存所有更改”。

    • Saved All Changes实施“更改”将状态设置为未保存的更改。

    • “更改”的未保存更改实施不执行任何操作。

答案 1 :(得分:1)

我正在使用LLBL Gen pro作为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);

在我们的场景中效果很好。

相关问题