Big editable collections

Big editable collections

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


Slayer posted on Monday, May 25, 2009

I have a question regarding big collections. In the past I have used one of 2 methods :

1.) I used the EditableRootListBase for a list of Editable Customer objects. This works fine if you have a limited amount of Customers. Editable Customer objects get quite big so having 10000 of them in a list would not be efficient. I like this method because you get a list first approach and when you modify a object, the original list gets updated.

2.) Whenever the collection gets 2 big, I do what Rocky does in his sample app - I build a compact read-only collection of CustomerInfo objects. Because this list is read-only you need to close it after you select the customer for editing.

My Question :

This project that i am currently working on uses products instead of customers and there are at least 20000 records. The master file changes a lot, meaning the user would modify +- 10 records in one sitting. Method 1 is perfect but would consume tons of memory(need to keep 20000 business objects in memory) and method 2 is tedious because you would need to populate the read-only collection for each edit.

Is there a mid-way with CSLA, If not does anyone have design tips on the matter?

Thanks

RockfordLhotka replied on Monday, May 25, 2009

In your #2, when you say "close it after you select the customer", you mean discard the read-only list?

You don't necessarily need to do that. You can keep the read-only list in memory on the client - perhaps in a static cache field. Basically in a global cache.

If you that, you can have CustomerEdit tell the read-only CustomerList that a customer has changed, and CustomerList can update the changed CustomerInfo. The only trick to this is that a ReadOnlyBase object won't normally raise PropertyChanged events, so if you are maintaining a bound form against this read-only list then you'll need to add an INotifyPropertyChanged implementation to your CustomerInfo class.

But basically do something like this:

public class CustomerList : ReadOnlyListBase
{
  private static CustomerList _cache;
  public void ClearCache()
  {
    _cache = null;
  }

  public static CustomerList GetList()
  {
    if (_cache == null)
      _cache = DataPortal.Fetch<CustomerList>();
    return _cache;
  }

  internal static void CustomerSaved(CustomerEdit cust)
  {
    if (_cache != null)
    {
      var info = this.SingleOrDefault(c => c.Id == cust.Id);
      if (info != null)
        info.CustomerSaved(cust);
    }
  }
}

public class CustomerInfo : ReadOnlyBase
{
  internal void CustomerSaved(CustomerEdit cust)
  {
    // refresh property values from cust
    // and maybe add OnPropertyChanged() to
    // refresh bindings in the UI
  }
}

public class CustomerEdit : BusinessBase
{
  public override CustomerEdit Save()
  {
    var result = base.Save();
    CustomerList.CustomerSaved(result);
    return result;
  }
}

 

Slayer replied on Tuesday, May 26, 2009

Thank you for the prompt response Rocky...

Yes, you understood my question perfectly. I will certainly try this approach.

I will also add some code(to the CustomerSaved() method) to add a new CustomerInfo object to the read-only collection in case a new customer gets added.

Copyright (c) Marimer LLC