Radical Documentation
View on GitHub
release-2
release-2
  • Home
  • Presentation
    • AbstractViewModel
    • Conventions
      • Bootstrap Conventions
      • Runtime Conventions
      • Conventions override
    • Commands and DelegateCommand
    • IViewResolver
      • Default view behaviors
      • view life cycle events
        • Callback expectations
        • notify messages
    • Message broker MVVM built-in messages
    • Application boot process
      • Application configuration
      • Application shutdown
      • Singleton applications
    • AbstractMementoViewModel
      • Simple ViewModel graphs
      • Collections and complex ViewModel graphs
    • Validation and Validation Services
    • Resources
      • Services as resources
      • ViewModels as resources
    • UI Composition
      • Region content lifecycle
      • TabControl region
      • Create a custom region
  • Concepts
    • Inversion of Control
      • Third party DI containers
    • Entities
      • Property System
    • Messaging and Message Broker
      • POCO messages
      • Standalone message handlers
    • Observers
      • PropertyObserver
      • MementoObserver
      • BrokerObserver
  • Memento
    • Change Tracking Service
      • MementoEntity and MementoEntityCollection
      • Handling change tracking:
        • Simple model
        • Collections
        • Complex objects graph
      • Atomic operations
      • Change Tracking Service API
      • Property Metadata for the ChangeTrackingService
      • Handling collection sync
      • Property State
  • Behaviors
    • DataGrid Behaviors
    • Password
    • Generic routed event handler to command behavior
    • Overlay adorner
      • Busy status manager
    • TextBox behaviors:
      • Command
      • Auto select
      • DisableUndoManager (.Net 3.5 only)
  • Markup Extensions
    • Editor binding
    • Auto Command binding
  • How to
    • Get the view of a given view model
    • Bi-directional communication between different windows/views
    • Handle the busy status during async/long running operations
    • Implement a customer improvement program
    • Manage focus
    • Create a splash screen
    • Access view model after view is closed
    • Intercept ViewModels before they are used
  • Upgrade guides
    • Radical Presentation 1.x to Radical 2.x for .NET Core
Powered by GitBook
On this page
  1. Memento
  2. Change Tracking Service
  3. Handling change tracking:

Complex objects graph

PreviousCollectionsNextAtomic operations

Last updated 4 years ago

We briefly introduced and we've seen how to and in .

The last type of graph we want to be able to track is a complex graph, where at least one of the properties of the root tracked object is a memento entity or a memento collection itself:

class Person : MementoEntity
{
    public Person()
    {
        this.Addresses = new MementoEntityCollection<Address>();
    }

    public String FirstName
    {
        get { return this.GetPropertyValue(() => this.FirstName); }
        set { this.SetPropertyValue(() => this.FirstName, value); }
    }

    public String LastName
    {
        get { return this.GetPropertyValue(() => this.LastName); }
        set { this.SetPropertyValue(() => this.LastName, value); }
    }

    public IList<Address> Addresses { get; private set; }
}

class Address : MementoEntity
{   
    public String Street
    {
        get { return this.GetPropertyValue(() => this.Street); }
        set { this.SetPropertyValue(() => this.Street, value); }
    }
}

In the above sample the Person class has a property, Addresses, whose type is itself a memento entity, a MementoEntityCollection<Address> in this specific case. Using the following snippet:

var memento = new ChangeTrackingService();

var person = new Person();
memento.Attach( person );

the Addresses collection is not automatically tracked, the memento does not know anything of the structure of the graph. We can update the Person class so to instruct the memento that also the Addresses collection needs to be tracked:

class Person : MementoEntity
{
    protected override void OnMementoChanged( IChangeTrackingService newMemento, IChangeTrackingService oldMemento )
    {
        base.OnMementoChanged(newMemento, oldMemento);
        if(oldMemento != null) 
        {
            oldMemento.Detach(this.Addresses);
        }

        if(newMemento != null) 
        {
            newMemento.Attach(this.Addresses);
        }

    //rest of the Person class code
}

We are intercepting the moment in which the Person instance is tracked by the memento service overriding the OnMementoChanged and we are manually propagating the memento to inner instances. It is important to detach the memento entity from the previous memento instance if any, a memento entity can be tracked by one memento only at a time.

NOTE: changes to a MementoEntityCollection<T> automatically propagates the memento service to list items if they are a memento entity.

MementoEntity and MementoEntityCollection
track changes in a simple graph
collections