Data Access has been discontinued. Please refer to this page for more information.

Preparing CarsViewModel

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

In this step you will start implementing the main view model CarsViewModel by defining the class and adding the required fields and properties.

Preparing CarsViewModel

  1. In the ViewModels folder create a new class named CarsViewModel.
  2. Inherit ViewModelBase with IMainView as the type argument.

    public class CarsViewModel : ViewModelBase<IMainView>
    {
    }
    
    Public Class CarsViewModel
        Inherits ViewModelBase(Of IMainView)
    End Class
    
  3. Define the messages which will be used through out the view model

    private const string STATUS_MESSAGE = "{0} cars retrieved from the database.";
    private const string CONFIRM_DELETE_MESSAGE = "You are about to delete {0} {1}. Are you sure?";
    private const string CONFIRM_DELETE_TITLE = "Confirm delete.";
    private const string DELETE_ERROR_RELATED_ENTRY_MESSAGE = "Could not delete this car entry because other entries related to it exist in the database. Exception message:\r\n";
    private const string DELETE_ERROR_MESSAGE = "Internal error has occured. Exception message:\r\n";
    private const string DELETE_ERROR_TITLE = "Deletion error!";
    
    Private Const STATUS_MESSAGE As String = "{0} cars retrieved from the database."
    Private Const CONFIRM_DELETE_MESSAGE As String = "You are about to delete {0} {1}. Are you sure?"
    Private Const CONFIRM_DELETE_TITLE As String = "Confirm delete."
    Private Const DELETE_ERROR_RELATED_ENTRY_MESSAGE As String = "Could not delete this car entry because other " +
        "entries related to it exist in the database. Exception message:" + vbCrLf
    Private Const DELETE_ERROR_MESSAGE As String = "Internal error has occured. Exception message:" + vbCrLf
    Private Const DELETE_ERROR_TITLE As String = "Deletion error!"
    
  4. Define the fields which will be used in the view model. context is the OpenAccessContext object through which cars will be retrieved and deleted. carMakerFilter is the string specifying the car maker by which the retrieved results will be filtered. isFilteringActive indicates whether or not a filter is currently applied to the displayed results. status contains a status message which is to be displayed on the view. carsToDisplay is a collection of the retrieved cars which should be displayed on the view. selectedCar is the car currently selected from the view. command is an object containing a command which can be bound to controls on the view.

    private readonly SofiaCarRentalContext context;
    private string carMakerFilter;
    private bool isFilteringActive;
    private string status;
    private List<Car> carsToDisplay; 
    private Car selectedCar;
    private RelayCommand command;
    
    Private ReadOnly _context As SofiaCarRentalContext
    Private _carMakerFilter As String
    Private _isFilteringActive As Boolean
    Private _status As String
    Private _carsToDisplay As List(Of Car)
    Private _selectedCar As Car
    Private _command As RelayCommand
    
  5. Create properties for carMakerFilter, carsToDisplay, selectedCar and status. These properties will be used to for binding and follow the same pattern - they raise the PropertyChanged event whenever they are set.

    public string CarMakerFilter
    {
        get
        {
            return this.carMakerFilter;
        }
        set
        {
            this.carMakerFilter = value.Trim();
            this.RaisePropertyChanged("CarMakerFilter");
        }
    }
    public string Status
    {
        get
        {
            return this.status;
        }
        private set
        {
            this.status = value.Trim();
            this.RaisePropertyChanged("Status");
        }
    }
    public List<Car> CarsToDisplay
    {
        get
        {
            return this.carsToDisplay;
        }
        private set
        {
            this.carsToDisplay = value;
            this.RaisePropertyChanged("CarsToDisplay");
        }
    }
    public Car SelectedCar
    {
        get
        {
            return this.selectedCar;
        }
        set
        {
            this.selectedCar = value;
            this.RaisePropertyChanged("SelectedCar");
        }
    }
    
    Public Property CarMakerFilter As String
        Get
            Return Me._carMakerFilter
        End Get
        Set(value As String)
            Me._carMakerFilter = value.Trim()
            Me.RaisePropertyChanged("CarMakerFilter")
        End Set
    End Property
    Public Property Status As String
        Get
            Return Me._status
        End Get
        Private Set(value As String)
            Me._status = value.Trim()
            Me.RaisePropertyChanged("Status")
        End Set
    End Property
    Public Property CarsToDisplay As List(Of Car)
        Get
            Return Me._carsToDisplay
        End Get
        Private Set(value As List(Of Car))
            Me._carsToDisplay = value
            Me.RaisePropertyChanged("CarsToDisplay")
        End Set
    End Property
    Public Property SelectedCar As Car
        Get
            Return Me._selectedCar
        End Get
        Set(value As Car)
            Me._selectedCar = value
            Me.RaisePropertyChanged("SelectedCar")
        End Set
    End Property
    
  6. Define a constructor initializing the class fields. The base constructor is also called with a new instance of the view associated with the model.

    public CarsViewModel()
        :base(new MainWindow())
    {
        this.context= new SofiaCarRentalContext();
        this.CarMakerFilter = string.Empty;
        this.isFilteringActive = false;
        this.Status = string.Empty;
    }
    
    Public Sub New()
        MyBase.New(New MainWindow())
        Me._context = New SofiaCarRentalContext()
        Me._carMakerFilter = String.Empty
        Me._isFilteringActive = False
        Me._status = String.Empty
    End Sub
    

Next step: Retrieving Cars from the Database and Disposing the Context