Synchonizing List Changes

Last post 12-17-2004 8:40 PM by exptrade2000. 3 replies.

Sort Posts:

  • Synchonizing List Changes

    12-16-2004, 3:50 PM
    • Contributor
      3,931 point Contributor
    • rsmoke21
    • Member since 10-14-2003, 9:12 AM
    • Charlotte, NC
    • Posts 792
    OK, so I have a business object, say Order (which was already created and saved), which has a collection of Products. I bind the Products collection to a list box on an .aspx page. On the page users can edit (add and remove Products) the list (Order). Once they are done making changes they press the "Save" button and I (will) update the changes to the database.

    My question is this: when they press "Save" I load the current Order from the database so I have a copy of what the Order IS and then the page has the data that the Order SHOULD BE. So, I copy the data (irrelevent for this question), then I need to synch the (Product) collections. In this example the data in each Product is irrelevent. I simply want to get the collection synched up. How are people doing this? I see 3 options:

    1. Clear out the Order's Product collection and recreate each item that should be in the collection. Since there are not a lot of items in the collection this is feasible. However, this would generate extra DB operations and is kind of ugly, IMO.

    2. Brute force check whether each Product in the Page's list is in the Order's list. If not add the Product. Then, check whether each Product in the Order's list is in the Page, if not, then remove it. This works, but seems pretty inefficient...

    I'm leaning towards #2 and will probably implement it, but I just wondered how anyone else handled this situation. It would be easy with WinForms since the objects would all still be in memory and removing them from the Page's (Form) would also remove from the Collection (and vice-versa). With ASP.NET we have to do some trickery...

    Smoke
  • Re: Synchonizing List Changes

    12-16-2004, 10:54 PM
    • Participant
      1,760 point Participant
    • ssmoot
    • Member since 10-27-2003, 2:46 PM
    • Posts 352
    #2: "...This works, but seems pretty inefficient... "

    Inefficient is relative. I'd suggest using a high performance timer (I found a nice one on CodeProject), and measuring the process you're actually describing. I know that for me, such little tests put the meaning of the terms inefficient or slow into perspective. I'm guessing the process you're talking about doesn't take more than a few tens of milliseconds at most.
  • Re: Synchonizing List Changes

    12-17-2004, 8:25 AM
    • Contributor
      3,931 point Contributor
    • rsmoke21
    • Member since 10-14-2003, 9:12 AM
    • Charlotte, NC
    • Posts 792
    Ah yes, I agree that inefficient is a relative term. The theorist in me says there SHOULD be a better way than brute force, but you're right that doesn't mean there is, or that I should search for it.... I just have to think this is an issue that a lot of people deal with and I wonder if there's any other (more radical/alternative) solutions. For now, though, I will brute force it!
  • Re: Synchonizing List Changes

    12-17-2004, 8:11 PM
    • Contributor
      3,309 point Contributor
    • exptrade2000
    • Member since 03-03-2004, 5:01 AM
    • Allen, TX
    • Posts 710
    I allways first delete and then add. Something like this.
    Get the "database list".
    Iterate through "database list" and check if "page list" contains item in "database list"; if not delete it.
    Now, iterate through "page list" and check if "database list" contains item in "page list"; if not add it.
    I think I've run into problem first adding and then deleting so i allways first delete and then add.
    You died at the very end of your life
Page 1 of 1 (4 items)