Radical Documentation
View on GitHub
Primary version
Primary version
  • 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
    • 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
    • Radical 2.0.0 to Radical 2.1.0
Powered by GitBook
On this page
  1. Presentation

AbstractViewModel

PreviousHomeNextConventions

Last updated 3 years ago

When dealing with MVVM and ViewModel(s) there are a lot of things that a base class, such as the AbstractViewModel, can do for us in order to reduce the friction of the daily work.

The AbstractViewModel can (it is not required, even if is highly suggested) be used as a base class for all the application ViewModel(s), defining a view model is as easy as:

class MainViewModel : AbstractViewModel
{

}

Nothing special, a simple and trivial class that inherits from the base AbstractViewModel type.

For the Radical toolkit a ViewModel is not required to be an AbstractViewModel, but if you do not to use the AbstractViewModel class as a base class for all the ViewModels you end up with 2 options:

  • implement on your view model the IViewModel interface;

  • or replace the AttachViewToViewModel that is responsible to reverse link the View to the ViewModel;

As soon as we do that we gain some benefits:

Property change notification:

the obvious benefit is that we immediately get INotifyPropertyChanged support:

private String _text;

public String Text
{
    get { return _text; }
    set 
    {
        _text = value;
        this.OnPropertyChanged( () => this.Text );
    }
}

But given that writing properties in such a verbose way is a waste of time we can leverage the power of the Property System.

Radical Property System:

The above property can be written in the following manner without altering the behavior:

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

But the property system is not limited to changes notification, we can for example do the following:

class MainViewModel : AbstractViewModel
{
    public MainViewModel()
    {
        this.GetPropertyMetadata( () => this.Text )
            .AddCascadeChangeNotifications( () => this.Sample );
    }

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

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

we have defined 2 properties and we are chaining the properties change notification in order to notify a change to the Sample property each time the Text property changes.

convention