Use relation between CSLA Business Objects

Use relation between CSLA Business Objects

Old forum URL: forums.lhotka.net/forums/t/4859.aspx


juancho559 posted on Sunday, May 18, 2008

Hi,

I would like to know how is the best way to model an use relation between business objects in CSLA?

For example: I have a business object called Payment. This object has many properties like PaymentId, OrderId, BillingInfo, ShippingInfo.

BillingInfo and ShippingInfo are two businness objects storing all the information about the billing and shipping.

When I retrieve the information about billing and Shipping I store this information in the payment object, for example: 

Payment.BillingInfo = Billing.GetBillingById(idBilling);
Payment.ShippingInfo = Shipping.GetShippingById(IdShipping);

When I try to update the payment object, I receive this exception: "Edit level mismatch in AcceptChanges".

So, How is the best way to model this scenario or how can I solve this problem?

I appreciate your help!!

 

 

sergeyb replied on Sunday, May 18, 2008

This all looks OK.  This code should not cause edit level mismatch.  Are you calling BeginEdit immediately after loading the object before UI has a chance to interact with it?

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: juancho559 [mailto:cslanet@lhotka.net]
Sent: Sunday, May 18, 2008 12:12 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Use relationship in CSLA Business Objects

 

Hi,

I would like to know how is the best way to model an use relationship between business objects in CSLA?

For example: I have a business object called Payment. This object has many properties like PaymentId, OrderId, BillingInfo, ShippingInfo.

BillingInfo and ShippingInfo are two businness objects storing all the information about the billing and the shipping.

I just want to avoid the "Edit level mismatch in AcceptChanges" exception when I reference this objects. I get this exception when I do this and try to update the payment object:

Payment.BillingInfo = Billing.GetBillingById(idBilling);
Payment.ShippingInfo = Shipping.GetShippingById(IdShipping);

So, How is the best way to model this scenario?

I appreciate your help!!

 

 



juancho559 replied on Sunday, May 18, 2008

Yes, the code is equal to Project Tracker Sample.

When I load the form, I bind the UI and call the BeginEdit method.

AddWinPart(new ProjectEdit(Project.GetProject(projectId)));

    public ProjectEdit(Project project)
    {
      InitializeComponent();

      // store object reference
      _project = project;

      // set up binding
      this.roleListBindingSource.DataSource = RoleList.GetList();

      BindUI();

      // check authorization
      ApplyAuthorizationRules();
    }

regards...

 

sergeyb replied on Monday, May 19, 2008

Ahh, I see…

I think this is because you are loading child objects through public properties, dirtying the parent object.  Load them through private members instead:

 

Payment._billingInfo = Billing.GetBillingById(idBilling);
Payment._shippingInfo = Shipping.GetShippingById(IdShipping);

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: juancho559 [mailto:cslanet@lhotka.net]
Sent: Sunday, May 18, 2008 11:44 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: Use relationship in CSLA Business Objects

 

Yes, the code is equal to Project Tracker Sample.

When I load the form, I bind the UI and call the BeginEdit method.

AddWinPart(new ProjectEdit(Project.GetProject(projectId)));

    public ProjectEdit(Project project)
    {
      InitializeComponent();

      // store object reference
      _project = project;

      // set up binding
      this.roleListBindingSource.DataSource = RoleList.GetList();

      BindUI();

      // check authorization
      ApplyAuthorizationRules();
    }

regards...

 



juancho559 replied on Sunday, May 25, 2008

Hi, Thanks for your response.

I made the test and I still have the same error. I just change the access modifier to the variables that I want to get.

internal BillingInfo _billingInfo = null;
internal ShippingInfo _shippingInfo = null;

Then, I load the objects from other form.

Payment._billingInfo = Billing.GetBillingById(idBilling);
Payment._shippingInfo = Shipping.GetShippingById(IdShipping);

Regards...

stefan replied on Monday, May 26, 2008

Hi!

You say "Then, I load the objects from other form."
But the code you then quote does a standard call to the factory methods...

If you really get your data from another form, then make sure the data is properly unbound from any controls (most likely a bindingsource control) on that form, leaving  it on edit level 0.

Stefan

juancho559 replied on Monday, May 26, 2008

Hi Stefan,

I think that I already know what is the problem. I was retrieving the object to edit from the binding source, and I didn't call the EndEdit in the current object. I just change my code like this:

//Retrieve current object from the binding source
System.ComponentModel.IEditableObject current =
paymentBindingSource.Current
as System.ComponentModel.IEditableObject;

//Call EndEdit()
current.EndEdit();

_payment = (
Payment) current;

Thanks...

Copyright (c) Marimer LLC