CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • Synchronizing ReadOnlyList with EditableBusinessObject

    rated by 0 users
    Answered (Verified) This post has 1 verified answer | 3 Replies | 2 Followers

    Top 500 Contributor
    23 Posts
    Alexander Becker Confused [*-)] posted on Mon, Mar 12 2012 6:20 AM

    Hello,

    given the following business and UI layer:
    PersonInfo : ReadOnlyBase<PersonInfo>
    PersonInfoList : ReadOnlyListBase<PersonInfoList, PersonInfo>
    PersonEdit : BusinessBase<PersonEdit>

    PeopleListViewModel : ViewModel<PersonInfoList>
    PersonEditViewModel : ViewModel<PersonEdit>

    PersonInfo is a small object only containing some header fields.
    PersonEdit is the full featured object containing all properties. 

    and the following use case within a MVVM WPF application:

    • a query based PersonInfoList is retrieved and displayed within a list
    • selecting a PersonInfo from the list triggers the creation of a PersonEditView
    • Saving a modified PersonEdit from this view should update the belonging PersonInfo from the list

    I do not want the complete list to be reloaded from the database, (or should I?).
    Could this be done by instancing a new PersonInfo from the updated PersonEdit object,
    and replacing the old PersonInfo from the PersonInfoList with the new PersonInfo?
    Should I define a method void Refresh(PersonInfo newPersonInfo) on the PeopleListViewModel to do such an approach?

    thanks in advance,
    Alexander. 

    Answered (Verified) Verified Answer

    Top 10 Contributor
    9,475 Posts

    The challenge here is to implement a solution that doesn't cause a memory leak.

    In concept, you can have some global reference to the PersonInfoList, and in the Saved event handler of the PersonEdit object you can just update the PersonInfo object with the new data. Or variations on that theme.

    The trick is that it is really easy to end up with circular or static references that create a memory leak.

    So the best solution is to use a UI framework that has a weak reference based event notification system (preferably publish/subscribe) so your Saved event handler on the PersonEdit object can publish a notification that a person has changed, and the PersonInfoList object can subscribe to that type of notification so it knows to update itself when the change has occurred.

    Does any of that make sense.

    In short, all you need to do is figure out a safe way to notify the PersonInfoList when a PersonEdit object has been saved. Preferably you'd pass a reference to the PersonEdit object as part of the notification.

    The PersonInfoList object can then find the PersonInfo matching the PersonEdit object, and can tell that PersonInfo object to update itself based on the PersonEdit object.

    The only additional thing to keep in mind is that you'll need to manually call OnPropertyChanged for each updated property in the PersonInfo object, because read-only objects don't automatically raise PropertyChanged events.

    Rocky

    All Replies

    Top 10 Contributor
    9,475 Posts

    The challenge here is to implement a solution that doesn't cause a memory leak.

    In concept, you can have some global reference to the PersonInfoList, and in the Saved event handler of the PersonEdit object you can just update the PersonInfo object with the new data. Or variations on that theme.

    The trick is that it is really easy to end up with circular or static references that create a memory leak.

    So the best solution is to use a UI framework that has a weak reference based event notification system (preferably publish/subscribe) so your Saved event handler on the PersonEdit object can publish a notification that a person has changed, and the PersonInfoList object can subscribe to that type of notification so it knows to update itself when the change has occurred.

    Does any of that make sense.

    In short, all you need to do is figure out a safe way to notify the PersonInfoList when a PersonEdit object has been saved. Preferably you'd pass a reference to the PersonEdit object as part of the notification.

    The PersonInfoList object can then find the PersonInfo matching the PersonEdit object, and can tell that PersonInfo object to update itself based on the PersonEdit object.

    The only additional thing to keep in mind is that you'll need to manually call OnPropertyChanged for each updated property in the PersonInfo object, because read-only objects don't automatically raise PropertyChanged events.

    Rocky

    Top 500 Contributor
    23 Posts

    Hello Rocky,

    just started work (6am) and found your fresh and detailed answer, thank you! Big Smile

    I plan to use the Prism framework for the software.
    What I've read about this so far, it provides a solution for notifications like you suggested.

    One thing confuses me a bit.
    You write to notify the PersonInfoList (model) about the saved PersonEdit object.
    To do that, the business layer has to subscribe to the notification system, but am I wrong this belongs to the UI layer only?
    Or do you mean the PeopleListViewModel gets informed by the notification and passes the update request to the PersonInfoList (model)

    I like the idea to update the PersonInfo instead of replacing it with a new instance.

    have a good night (just googled your timezone...),
    Alex. 

    Top 10 Contributor
    9,475 Posts

    You can manage this entirely at the viewmodel level, and that's probably best, as it does keep any dependency on the event aggregator out of the business layer.

    Rocky

    Page 1 of 1 (4 items) | RSS

    Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
    Email admin@lhotka.net for support.
    Powered by Community Server (Non-Commercial Edition), by Telligent Systems