locked
Tricky: In IDataService.SaveChanges method, data changed at server side may not post back to client side. RRS feed

  • Question

  • This is really tricky, I'm wondering if there is a better solution.

    This is how it happens:

    Having two entities, Order and OrderEntry, in screen OrderDetail, add one OrderEntry, click Save command, in server side OrderEntry_Inserting, update the OrderEntry.Order.Amount to OrderEntry.Order.OrderEntries.Sum(s=>s.Amount).

    In screen, the SalesOrder.Amount is not updated correctly, because the SalesOrder changed at server side is not post-back to client side.

    My resolution is to refresh the screen after saving.

    But I wonder if there is a better solution?


    Sunday, July 22, 2012 3:43 PM

All replies

  • Hi

    Use the below code within Screen_Saved method to refresh the screen after save.

    this.YourCollection.Refresh();

    VB

    Me.YourCollection.Refresh()

    Regards...


    Rashmi Ranjan Panigrahi
    www.lightswitchspecial.com

    If you found this post helpful, please “Vote as Helpful”. If it answered your question, please “Mark as Answer”.
    This will help other users to find their answer quickly.

    Tuesday, July 24, 2012 4:00 AM
  • Hi Baboo,

    Thanks for your advice, call Refresh in IVisualCollection is not my case since the visual collection data in client is up to date. In my case, it's SalesOrder not up to date.

    Tuesday, July 24, 2012 8:34 AM
  • Why not make the Amount property on the Order entity be a computed property?

    partial void Amount_Compute(ref decimal result)
    {
        result = this.OrderEntries.Sum(entry => entry.Amount);
    }

    Alternatively, when the Amount property on a OrderEntry is changed, update the Amount property on the corresponding Order entity.

    partial void Amount_Changed()
    {
        if (null != this.Order)
        {
            this.Order.Amount = this.Order.OrderEntries.Sum(entry => entry.Amount);
        }
    }


    Justin Anderson, LightSwitch Development Team

    Thursday, July 26, 2012 8:44 PM
    Moderator
  • Hi Justin,

    Computed property is not my case since computed property doesn't have a underlying data store, which won't be able to do sorting, grouping aggregation etc.

    Friday, July 27, 2012 5:26 AM
  • Then you should be able to use the alternative solution that I mentioned. Basically, calculate the order total everytime and order entry's total changes.

    Justin Anderson, LightSwitch Development Team

    Friday, July 27, 2012 10:12 AM
    Moderator
  • That's not what I want to do too, I used to do things like this but found a "bug"(maybe).

    Here is how it happens:

    In Amount_Changed method of SalesOrderEntry, update the SalesOrder.Amount property correctly.

    But it won't work if, In server side I changed SalesOrderEntry.Amount property by code, the Amount_Changed is not called.

    I suppose the Property_Changed method is only called at client side. I may have the property changed at server side.

    Friday, July 27, 2012 11:08 AM
  • But it won't work if, In server side I changed SalesOrderEntry.Amount property by code, the Amount_Changed is not called.

    I suppose the Property_Changed method is only called at client side. I may have the property changed at server side.

    No, it's not only called client-side. I just tried this myself and works exactly as I expect it. Changes on the client or server immediately update the parent order's total. You should recheck your logic to make sure that nothing is causing it to not run e.g. some kind of condition is not met when it's on the server.

    Here's my code that worked, just as an example:

    public partial class OrderItem
    {
        partial void Amount_Changed()
        {
            if (null != this.Order)
            {
                this.Order.Amount = this.Order.OrderItems.Sum(item => item.Amount);
            }
        }
    }

    public partial class ApplicationDataService
    {
        partial void Orders_Inserting(Order entity)
        {
            for (int i = 1; i < 6; i++)
            {
                OrderItem item = entity.OrderItems.AddNew();
                item.Amount = i * 100;
            }
        }
    }

    I only created an Order instance and immediately saved it. Five OrderItems were added and the Amount property of the Order instance was 1500, the sum of all five OrderItem Amounts.

    Justin Anderson, LightSwitch Development Team

    Monday, July 30, 2012 6:53 AM
    Moderator
  • HiJustin,

    Thank your for the reply, it works, but I just remember in some circumstance it doesn't work though I don't remember it now,

    I will try figuring it out the circumstance it doesn't work.

    Best regards.

    Monday, July 30, 2012 7:48 AM
  • Hi Justin, 

    I just recall how it happens.

    I'm having a ria service called ApprovalFlowService, which as a method called

    ApprovalResult Approve(string entityType,string entityId);

    In this method, I will get the entity instance, and modify some properties on it. The strange thing is the Property_Changed method is not fired in such scenario.

    Can you pls confirm this is by design? or do you need mo dedicated explination?

    Monday, July 30, 2012 2:30 PM