0

我有一个实体,例如客户继承自IEditableObject如下所述:

public class Customer : IEditableObject
{
    ...
    private Boolean backupAvailable = false;
    private ThisObject backupData;

    public void BeginEdit()
    {
        if (!backupAvailable)
        {
            this.backupData.Name = this.Name;
            backupAvailable = true;
        }
    }

    public void CancelEdit()
    {
        if (backupAvailable)
        {
            this.Name = this.backupData.Name;
            backupAvailable = false;
        }
    }

    public void EndEdit()
    {
        if (backupAvailable)
        {
            backupData = new ThisObject();
            backupAvailable = false;
        }
    }
}

在我的 UI 类中BindingSource,我有一个所有控件都绑定到的 ,以及 2 个按钮“更改”和“取消”:

BindingSource BSCustomer;

private void buttonChange_Click(object sender, EventArgs e)
{
    ...
    ((Customer)BSCustomer.Current).BeginEdit();
}


private void buttonCancel_Click(object sender, EventArgs e)
{
    ...
    ((Customer)BSCustomer.Current).CancelEdit();
}

这工作正常。

但是现在我发现,这BeginEdit()不仅是从我的显式调用中调用的,而且是从许多其他代码中调用的,例如:

BSCustomer.AllowNew = true;

或者

BSCustomer.AddNew();

或者

BSCustomer.IndexOf();

当我现在单击“更改”按钮时,backupAvailable已经设置了错误的值。当我单击“取消”时,会写回错误的值。

有没有可能阻止这种呼唤?或者我可以BeginEdit()在呼叫的来源方面有所不同吗?

4

2 回答 2

1

只是删除:IEditableObject是解决方案。没有它,BeginEdit()只会在手动调用时调用。

所有功劳归于伊万。

于 2017-08-22T11:54:18.490 回答
0

我也不得不处理这种情况。谢谢伊万,他们帮助解决了这个问题。结果,我用ICloneable做了这样的事情。

public class Customer : ICloneable
{
    struct ThisObject 
    {
        internal string guid;
        internal string name;
    }

    private Boolean backupAvailable = false;
    private ThisObject backupData;
    private ThisObject currentData;

    public void BeginEdit()
    {
        if (!backupAvailable)
        {
            var tempCustomer = (Customer)this.Clone();
            this.backupData = tempCustomer.currentData;
            backupAvailable = true;
        }
    }

    public void CancelEdit()
    {
        if (backupAvailable)
        {
            this.currentData = backupData;
            backupAvailable = false;
        }
    }

    public void EndEdit()
    {
        if (backupAvailable)
        {
            backupData = new ThisObject();
            backupAvailable = false;
        }
    }

    public Customer() : base()
    {
            this.currentData = new ThisObject();
            this.currentData.guid = Guid.NewGuid().ToString();
            this.currentData.name = string.Empty;
    }

    public string GUID
    {
        get { return this.currentData.guid; }
        set { this.currentData.guid = value; }
    }

    public string Name
    {
        get { return this.currentData.name; }
        set { this.currentData.name = value; }
    }

    public object Clone()
    {
        return (Customer)this.MemberwiseClone();
    }
}

...

private void buttonChange_Click(object sender, EventArgs e)
{
    ...
    ((Customer)BSCustomer.Current).BeginEdit();
}


private void buttonCancel_Click(object sender, EventArgs e)
{
    ...
    ((Customer)BSCustomer.Current).CancelEdit();
}
于 2021-05-20T01:23:09.467 回答