02
Sat, Nov
2 New Articles

How MVC Can Simplify and Modernize Your IBM i Applications

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Want to make your big ol’ RPG programs more modular and easier to maintain? Then give the MVC design pattern a try.

Editor’s note: This article is excerpted from chapter 24 of 21st Century RPG: /Free, ILE, and MVC.

Model-View-Controller (MVC) is a design pattern. Unlike many of the design patterns that have been defined, it is not uniquely oriented to object-oriented (OO) programming, but can easily be used with procedural languages as well.

Nor is it related to some sort of programming or communication issue. Instead, it is uniquely suited to the types of applications we run in the business world: applications where we access data, display it somewhere, make changes to it, take it back in, edit it in various ways, and write it back out to the spot it originally came from.

Now before I go any further, I want to make one thing clear. When I use the term “MVC” I am using it in a generic, not specific sense. That is, in a specific sense, MVC is a very specific design pattern that has very specific requirements and attributes.

It is actually part of a family that is loosely called “MVC.” This family consists of MVC plus MVP (Model-View-Presenter), MVA (Model-View-Adapter), MVVM (Model-View-ViewModel), HMVC (Hierarchical Model-View-Controller), and others. Each of these has very specific properties; the particular format I will be using is probably more MVP than MVC.

For more information about MVC specifics, I suggest Martin Fowler’s excellent article “GUI Architectures” on his website).

What Is MVC?

At a practical level, MVC is a pattern consisting of a number of separate modules of the following types:

  • The Model is somewhat misnamed in my opinion, but it’s too late to do anything about that now. Essentially the model is the business rules, the logic, that you want to apply in your application. It may or may not contain the access code to get to the data.
  • View is pretty self-explanatory; it is the code required to display the information that we have accessed or the user wants to enter.
  • Controller is the code that ties the other two together, acting as the traffic cop that tells the logic flow when to go to the appropriate module.

Generally, you will have multiple model and view modules. That is, it’s quite probable that you will have a number of screens in this function, and each one would be a separate view module. Similarly, you could easily have several models, each one oriented around retrieving or processing a specific type of data. Usually, however, there is only one controller module, although that is a guideline, not a rule. (Although now that I am editing this, I can’t imagine you needing two controller segments. Maybe it should be a rule.)

What is a rule is that whatever you code up, it should fit into one, and only one, of these categories (model, view, or controller). And it should be true to the spirit of ILE: each module should have basically one and only one logical idea behind it.

After that, it is a bit of a judgment call.

For example, some people feel very strongly that you need to separate out your data access logic from the model code. And in some ways that makes sense, particularly if there is much chance that you will change the database or platform that you are on. If you separate the data access, it makes the app much more portable. For us on the IBM i, that is probably not going to happen (unless you switch from DDS to DB2), and so for simplicity I believe in keeping the data access code in with the logic statements in the model. But that’s just me. And most normal people.

Other people get fixated on the relationship between the model and the view.

In theory, the controller should be the intermediary between the model and the view, but the fact that MVC is always shown as a triangle lets some people build connections between the logic and the display.

There is a version of MVC called MVP—Model-View-Presenter—that is specifically designed to not allow communication between the model and the view. Everything has to go through the controller. I sort of prefer this.

There is also MVA—Model-View-Adapter, which to me seems identical to MVP, although I would never suggest that to a partisan of either.

There is also the MVVM—Model-View-ViewModel, which allows the unthinkable to happen, for the model and the view to communicate with each other via slightly different types of view modules.

I hate to be blunt about this, but I really find it very hard to care. I guess I am more in favor of not allowing the logic (model) and the display (view) to talk to each other, but if it happens I am not going to consider my life a failure. I’ve got a lot of other reasons besides that. No, we are going to look at a middle-of-the-road implementation of MVC and get on with our lives.

Why MVC Is Important

OK, MVC breaks things into specific pieces. Big hairy deal. Like the whole world is about to explode, and they changed the cast for the third Fantastic Four movie, and Peter Jackson just won’t stop making crummy movies that rewrite the two greatest books ever written, and you think I care about MVC? Hey, pal, I got real things to worry about.

But the truth is, we should care at least a little about MVC. And I’ll tell you why.

I do not believe in the wholesale rewriting of applications to make them “modern.” Or at least, I don’t believe in rewriting the whole thing in terms of some other language, such as Java. And yet, RPG is incapable of producing those cute little GUI screens that are so in today. What’s a body to do?

What I am in favor of is taking the big ol’ programs (BOPs) that are out there for an application and breaking them down into model, controller, and view modules. Then I would redo the model and controller RPG code using /Free and ILE. And I would then be free to redo the view portion in whatever Web language is the most exciting at the time.

And so, for me, MVC is a requirement for any modernization.

But it goes beyond modernization. MVC is a requirement to get your programs set so that they are maintainable into the future.

I mean, forget modernization. You don’t need to do that to have your RPG programs continue to function just as they are today. And you can add additional functionality to them to make them even more valuable. But doing that to many BOPs is easier said than done, and when you are done, the program will undoubtedly look even worse than it did before. Going to MVC simplifies and cleans up these programs.

Quite simply, MVC is the next iteration in i program structure. It is replacing the more or less single program app structure with the MVC format. That should be the basis of any modernization that you do.

Everybody Loves MVC, but ...

At this point, you may be tempted to go out to the Web, google MVC, and see what you get. I guarantee it will be a ton. MVC is very widely used, extremely well known, and very effective. And yet, most of the articles that you see will probably be some variation of “MVC is great, but this is what’s wrong with it.”

The reason for that is that most of the articles on MVC are written by Web language people. And things on the Web are more complicated than things in the i world. There are a lot of issues and situations that are standard for them, which just don’t come up for us. As a result, there are a lot more things that can “go wrong” with the relatively simple MVC design pattern. Things like inversion of control and dependency injection. Some of the implementations of MVC, like MVVM, tend to be slow and difficult to implement (don’t even get Dave Bush started). And, since MVC is a design pattern, it does not really force you to do anything. So you can use MVC and still write very complex and disjointed programs. Or have a high degree of data coupling. Or intermix functionality, so that view logic is somehow embedded in the model or model logic shows up in the controller.

OK, so MVC isn’t perfect. If you are determined to screw it up, you will. Common sense and basic programming disciplines are still necessary.

But, MVC does provide a framework to allow you to write applications with a couple of very important characteristics.

First, MVC does force you to be modular, and in an RPG world that is still heavily involved with BOPs, that is an important fact.

Second, it makes a pretty strong effort to separate business logic from the view process from the program flow (control) actions. Yes, you can mess that up, but if you are paying even just a little attention, that shouldn’t happen.

Third, it provides the only way to have flexibility to change your view method and maybe even your database, without having to go through a major rewrite.

And those are the things that I want us to focus on as we develop an example of a /Free-based, ILE-using MVC design pattern application: specifically, modularity and separation of function, two of the keys to developing truly modern applications.

What Ya Shoulda Learned

  • You should know how to spell MVC.
  • And you should know what it stands for: Model (business logic), View (the display of the data), Controller (application flow control).
  • The real thing you need to know is how to create an MVC application rather than doing a BOP. And that is what we will look at next.
David Shirey

David Shirey is president of Shirey Consulting Services, providing technical and business consulting services for the IBM i world. Among the services provided are IBM i technical support, including application design and programming services, ERP installation and support, and EDI setup and maintenance. With experience in a wide range of industries (food and beverage to electronics to hard manufacturing to drugs--the legal kind--to medical devices to fulfillment houses) and a wide range of business sizes served (from very large, like Fresh Express, to much smaller, like Labconco), SCS has the knowledge and experience to assist with your technical or business issues. You may contact Dave by email at This email address is being protected from spambots. You need JavaScript enabled to view it. or by phone at (616) 304-2466.


MC Press books written by David Shirey available now on the MC Press Bookstore.

21st Century RPG: /Free, ILE, and MVC 21st Century RPG: /Free, ILE, and MVC
Boost your productivity, modernize your applications, and upgrade your skills with these powerful coding methods.
List Price $69.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: