Migrating Oracle Forms to WPF .NET using Model-View-ViewModel Pattern

The Model View ViewModel (MVVM) is an UI design pattern used in Windows Presentation Foundation (WPF) UI development platform.

Oracle Forms concepts such as forms, data blocks and triggers allow you to easily utilize MVVM during application migration to .NET platform.

Typical UI Programming

In a typical even-driven UI application, UI controls actually contain most data during the application execution. Application code gets data from UI controls, inserts/selects them into/from the database and most logic is performed in UI event-handlers referencing UI controls throughout the code.

Such approach does not allow to easily redesign or extend the user interface. Also it is significantly complicates automated testing since you have to emulate user actions such as entering text to controls, clicking buttons etc.

Model View ViewModel Concepts

MVVM is designed to separate UI and business logic, and to simplify automated testing.

  • Model

Model refers to the data access layer (database). For example, CustomerName column of Customer table represents a model.

   CREATE TABLE Customers
   (
      id INT,
      name VARCHAR(70)
      ...
    );

Besides table definitions and data, we can also consider ADO.NET data access as a part of Model since they provide rich and powerful functionality to easily handle data without additional wrappers.

For example, this C# code (for Oracle ADO.NET data provider) reads all rows returned by the query:

   using (OracleConnection con = new OracleConnection(connectionString))
   {
      OracleDataAdapter da = new OracleDataAdapter("SELECT * FROM customers", con);
 
      DataSet ds = new DataSet();
      da.Fill(ds);
      // ...
   }
  • View

View contains all UI elements such as buttons, text boxes, check boxes etc. For example, a TextBox to enter customer name in the order form.

In Windows Presentation Foundation, the UI layout is described in a XAML file:

    <Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Order Form" Height="107" Width="300">
    <StackPanel Orientation="Horizontal">
        <Label Width="120" Content="Customer Name:"/>
        <TextBox Width="100"/>
    </StackPanel>
    </Window>
  • ViewModel

ViewModel is the abstraction of View, and it links View and Model.

ViewModel does not need to know about View, ViewModel just exposes public properties (data contained in Model), and View binds its UI controls with these properties. ViewModel is set as the DataContext of View, so any changes are propagated between View and ViewModel automatically via data binding.

Only ViewModel, not View, performs all modifications to Model, and View does not know that Model exists. At the same time, Model does not know that View and ViewModel exist.

Benefits of Model View ViewModel UI Design

MVVM design pattern has some benefits over other UI implementations:

  • UI and Business Logic Separation

This is a very loosely coupled design of a database-driven UI application. It allows the development team focus on ViewModel classes, while the design team can make user-friendly Views.

  • Testing

Applications based on MVVM are easier to test as View and unit test are just two different types of ViewModel consumers, so it is possible to have a test suite for quick regression testing, which helps reduce the maintenance cost.

Previously, UI applications were too difficult to test automatically because you have to emulate user actions in the testing tools, or have a QA team validating not only updated functionality, but most importantly, looking for side effects in other application features (regression testing). MVVM allows you to significantly automate the application testing.

  • Productivity

Model-View-ViewModel implementation in WPF provides data binding, templates, styles and many other features that reduce coding and help you speed up software development.

When you use MVVM pattern with data binding to separate UI and business logic, you do not need to write code to exchange data between View and ViewModel. In case of MVC or MVP (Model-View-Presenter) patterns you typically need to implement this logic in event handles. In case of MVVM you can get rid of many event handlers and use data binding.

Migration Resources

Discussion

, October 27, 2011 12:29 pm

All of my questions stteeld-thanks!

You could leave a comment if you were logged in.