嵌套的工作单元和制作不需要的对象

时间:2015-06-28 06:51:55

标签: c# .net devexpress xpo

我的应用程序中有3个表单:frmTrucks,frmEditTruck和frmEditContent。 frmTrucks在网格中显示我的卡车。 我添加了一辆卡车,或从网格中选择一辆卡车在frmEditTruck中进行编辑

    public void Edit()
    {
        using (NestedUnitOfWork nuow = session.BeginNestedUnitOfWork())
        {
            Truck currentTruck = nuow.GetNestedObject(
        xpcTruck[gvTruck.GetDataSourceRowIndex(gvTruck.FocusedRowHandle)])
              as Truck;
            using (frmEditTruck form = new frmEditTruck(currentTruck))
            {
                if (form.ShowDialog() == DialogResult.OK)
                    nuow.CommitChanges();
            }
        }
    }

在frmEditTruck中,卡车属性和两个按钮有一些文本框。 btnSave和btnAddContent。 btnSave保存更改(now.CommitChanges();)。 btnAddContent的点击代码是:

    Truck truck;
    Session session;

    public frmEditTruck(Truck truck)
    {
        InitializeComponent();
        this.truck = truck;
        this.session = truck.Session;
    }


    private void btnAddContent_Click(object sender, EventArgs e)
    {
        TContent content = new TContent(session);
        using (frmEditTContent form = new frmEditTContent(content, truck))
        {
            if (form.ShowDialog() == DialogResult.OK)
                truck.TContents.Add(content);
        }
    }

它显示了frmEditContent。我可以在卡车上添加内容。问题是当我按AddContent然后取消它。之后,当我按下frmEditTruck上的保存按钮时,它会在我的内容表中添加一个空行。我想解决这个问题。我该怎么解决?我不确定我的问题是否足够清楚。请让我知道

public class Truck : XPObject
{
    .
    .
    .

    [Association("Truck-TContents")]
    public XPCollection<TContent> TContents { get { return GetCollection<TContent>("TContents"); } }

}

public class TContent : XPObject
{
    .
    .
    .
    private Truck truck;
    [Association("Truck-TContents")]
    public Truck Truck
    {
        get
        {
            return truck;
        }
        set
        {
            SetPropertyValue("Truck", ref truck, value);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

private void btnAddContent_Click(object sender, EventArgs e)
{
    TContent content = new TContent(session);
    using (frmEditTContent form = new frmEditTContent(content, truck))
    {
        if (form.ShowDialog() == DialogResult.OK)
            truck.TContents.Add(content);
    }
}

我已将代码更改为:

    private void btnAddContent_Add(object sender, EventArgs e)
    {
        TContent content = new TContent(session);
        using (frmEditTContent form = new frmEditTContent(content, truck))
        {
            if (form.ShowDialog() == DialogResult.OK)
            {
                truck.TContents.Add(content);
            }
            else
            {
                if (session.TrackingChanges)
                    session.RollbackTransaction();
            }
        }
    }

它运作正常。