John Kouraklis

MVVM in Delphi

Architecting and Building Model View ViewModel Applications

John Kouraklis

London, UK

Any source code or other supplementary materials referenced by the author in this text are available to readers at www.apress.com/9781484222133 . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ . Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter.

ISBN 978-1-4842-2213-3

e-ISBN 978-1-4842-2214-0

DOI 10.1007/978-1-4842-2214-0

Library of Congress Control Number: 2016956108

© John Kouraklis 2016

John Kouraklis

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

To my parents, who demonstrated breadth of mind and a sense of vision when they bought me my first computer in times when computers were in their infancy in my country and they were considered a “bad influence” on kids by many.

Introduction

The wide use of portable devices (tablets, smart phones, and smart watches) and the connectability between them gave rise to a whole new challenge to software development. Cross-platform programming, or the production of applications that target more than one platform, is getting momentum as the new wave of software development. Under these circumstances, professionals must preserve resources (time, skills, infrastructure, human capital, and knowledge) and maximize output while developing for multiple target platforms. Companies and professionals have responded to this development by offering new tools that facilitate the development of cross-platform applications.

Delphi FireMonkey framework is such a tool. It allows the development of cross-platform applications using Delphi; an incarnation of Object Pascal and one of the strongest and well developed programming languages available to developers. However, having a tool is not enough to produce efficient cross-platform applications. Although it makes programming easier and perhaps makes developing user interfaces on different devices trivial, it does not answer the question about how we can design our software in such way that cross-platform operations—such as moving across platforms, switching data providers, and injecting new platform-dependent implementations of algorithms—can be done with maximum flexibility and quality and minimum expense to programming effort and time.

These thoughts give strong support to programming patterns and, in particular, to design patterns, which is the overall concept of this book. Design patterns take us one step before designing the front-end GUI; they open the angle and perspective from which we see our applications and force us to think in a modular way.

The ModelView-View-Model (MVVM) design pattern is characterized by its flexibility and adaptability to different situations under diverse conditions. This book attempts to open the field of MVVM to Delphi developers. It saves deep theoretical discussions and conceptual analysis for other types of publications and focuses on the practicalities of implementing a Delphi framework according to the guidelines of MVVM. At the same time, the book develops a methodology about how to convert an application that doesn’t follow a design pattern to one that complies to the MVVM paradigm.

As always, in the field of software development, there are hardly universal solutions applicable to every situation; there are rather efficient solutions for specific problems. The code in this book and the suggestions offered serve as a foundation to help you make your own choices about the version and implementation of MVVM that suits your needs, your company’s requirements and policies, and the specifications of the applications you are developing. For sure, there are areas where you can apply more sophistication and abstraction to the design and others that simpler approaches exist.

Who This Book Is For

The book focuses on the busy Delphi developer with good knowledge of Object Pascal. Although the code has been developed using Delphi, it doesn’t rely on specific features that cannot be found in other versions of Object Pascal (including standard forms and visual elements). Therefore, the value that can be gained expands the Delphi domain and falls into the broader area of Object Pascal.

After reading this book you will be able to:

  • Identify the different aspects of the Model-ModelView-View (MVVM) design pattern.

  • Design applications based on the MVVM approach.

  • Implement the different elements of the framework in Delphi in ways that suit your application needs.

  • Undertake the task of converting existing applications to meet the MVVM design.

The Development Environment

The code in this book was developed using the following environment:

  • Embarcadero Delphi 10 Seattle Architect

  • Microsoft Windows 7 Professional

  • FireMonkey framework

I use the Architect edition of Delphi, but this is not a requirement. You can use whichever version of Delphi you have access to. I use FireMonkey but, again, everything we discuss in this book is applicable to VCL as well. If the VCL approach is different, I flag this in the code. As mentioned, the code in this book uses pure Object Pascal which, in turn, means you can use different versions of development environments like Lazarus and the like. Lastly, if linking to the graphical interface (forms and views) is not your concern or priority, you can develop a console application using the ViewModel and Model classes as they are presented in this book, without any modifications. That is the first hint to the simplicity and power of the MVVM pattern.

The Book’s Structure

This book has seven chapters. While you are going through the chapters, you will be exploring different aspects of MVVM and your knowledge will gradually build to cover common situations found in rich content applications.

Chapter 1 : MVVM as Design Pattern

This chapter builds on your understanding of the MVVM framework. It briefly visits different presentation patterns in a historical manner with the sole purpose of showing how the approach to presentation patterns shifted toward MVVM.

Chapter 2 : Setting Up the POSApp

In this chapter, we create POSApp . This is a simple application that resembles more sophisticated invoicing systems. We will build the application in such way that it doesn’t respect any design patterns. In the following chapters, we rely on POSApp to demonstrate the implementation of the MVVM pattern.

Chapter 3 : MVVM as Design Philosophy

In this chapter, we start exploring the MVVM pattern by first looking at a way to organize the different aspects of POSApp as they are understood by MVVM. This is the first time where we see the flexibility, clean separation of the different parts, and loose communication. We also see how communication is achieved between the different parts of the MVVM design.

Chapter 4 : Two-Way Communication

This chapter expands on the previous chapter and implements a way to accomplish bidirectional communication between the MVVM components without jeopardizing the principals of loose connections. At the end of this chapter, we formalize the methodology to convert non-MVVM application to follow the paradigm.

Chapter 5 : Converting the Invoice Form

Chapter 5 continues with the development of the most complex form of the POSApp . In this chapter, we apply the methodology we developed in Chapter 4 . This chapter offers a systematic way to approach legacy (or monolithic) applications when you want to convert them to modern patterns.

Chapter 6 : User Interaction

The content of this chapter deals with how we can manage user interactions (mouse clicks, popup menus, and so on) with respect to the MVVM and generate responsive actions.

Chapter 7 : Input Validation

The last chapter of the book deals with how to use the MVVM framework to check on inputs by the user, as in the case of an edit field. We will also complete the conversion of POSApp by developing some secondary actions such as a close and cancel button.

Code Files

This book comes with Delphi code files. You can download the code from the publisher’s web site at this address: http://www.apress.com/9781484222133 . For convenience, I include Table 1 , which lists the project names per chapter and a short description.

Table 1. Project Names Per Chapter as Found in the Book’s Code Files

Chapter

Project Name

Notes

2

POSApp

 

3

POSAppMVVM

The viewmodel and the model are created inside the main form

 

POSAppMVVMMainForm

The viewmodel and the model are created outside the main form

4

POSAppMVVMMainFormTest

Test application for the ProSu framework

 

POSAppMVVMMainFormInterfaces

POSApp uses interfaces as it appears in the chapter

 

POSAppMVVMMainFormFullInterfaces

POSApp is fully converted to use interfaces

5

View.InvoiceForm.fmx

View.InvoiceForm.pas

Empty invoice form

 

POSAppMVVMStart

Implements the invoice form with dummy variables

 

POSAppMVVMInvoiceForm

Implements all the changes in Chapter 5

6

POSAppMVVMUserInteraction

Implements user interaction

7

POSAppMVVMFinal

Final version of POSApp (converted to MVVM)

About the Author and About the Technical Reviewer

About the Author

John Kouraklis started exploring computers when he was 16. He started developing IN Delphi, as a hobby, initially, and then as a professional activity. He developed a wide range of applications, from financial software to reverse engineering tools and, more recently, discovered the fascinating world of cross-platform development.

About the Technical Reviewer

A371064_1_En_BookFrontmatter_Figb_HTML.jpg

Nick Hodges is a Senior Software Engineer at Gateway Ticketing Systems, a firm that provides ticketing and access control systems to the largest amusement parks, zoos, and museums around the world.

Nick is a software developer at heart. He’s been a Pascal/Delphi developer for over 20 years and still thinks that Delphi is the best development tool out there. He loves to read programming books, attend conferences, and watch cool videos about new programming techniques. He generally tries to be an industry thought leader.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset