设计Manager类以进行值设置和验证反馈

时间:2012-10-25 04:06:33

标签: c# events n-tier-architecture

我想编写一个DesignManager类来监督和管理Item的值。

项目具有许多属性,并且相互依赖并为彼此放置某些值规则。这些值在UI级别输入到Item变量中,DesignManager检测到更改,对UI进行验证/计算和报告(通过事件)。

我现在的问题是围绕属性设置器模式。我想到了两种方法来完成关系,每种方式都有自己的优点/缺点,希望得到一个可以使用的建议:

// Syntax is cleaner
// DesignManager does not know about the change Item notifies DesignManager
// Nested collections can notify DesignManager, but there is problem of identifying the sender
DesignManager.Item.Property = value;

// Syntax is not clean
// Hard to support value setting of nested Items or collections within Items
// DesignManager immediately gets informed of the change via the calling UI logic.
DesignManager.SetItemProperty(value);

我不知道哪个更喜欢,因为我看不到与每个相关的所有警告。目前,我最大的问题是Item中的嵌套集合。

如果有人有这方面的经验,希望可以提供建议。谢谢。

1 个答案:

答案 0 :(得分:0)

这是来自c#数据库linq映射的片段:

    public int id
    {
        get
        {
            return this._id;
        }
        set
        {
            if ((this._id != value))
            {
                this.OnidChanging(value);
                this.SendPropertyChanging();
                this._id = value;
                this.SendPropertyChanged("id");
                this.OnidChanged();
            }
        }
    }

Microsoft采用第三种方式并使用属性,这些属性在set函数fire事件中。 在变更之前发送变换事件,因此可以用于否决变更(不是从MS实现)。 Microsoft不会为类的每个属性进行分离,只有一个事件包含属性的名称。 在您的经理中,您可以订阅事件并根据需要验证/计算属性。