What we have looked at in the previous excerpt is a simple application structured using MVC. Actually, it’s probably actually MVA or MVP, but that’s not my problem. Read Part 1, Part 2 and Part 3 of this series.
By David Shirey
Editor's Note: This article is excerpted from chapter 26 of 21st Century RPG: /Free, ILE, and MVC, by David Shirey.
There is really only one thing wrong with it: it’s not very ILE, is it? And it sort of assumes that RPG is the only player in town, which certainly isn’t true.
It is written in a modular fashion using /Free, but it is not as ILE as it could be. It has no service programs and no sub-procedures—only straight programs that could just as easily be OPM.
Surprised? The fact is, you could have been doing MVC all along. It’s not a new idea. The problem is, in its present form (that is, the way I did it in the previous chapter), it would suffer from many of the problems that OPM programs normally have. Yes, it would be modular, but it would not be efficient. And it would not scale well if you applied this same MVC architecture to an application that had more traffic.
But worst of all, it used very few of the things that you learned in this book. And if you do not make use of what you have learned, what was the point? Although, you did buy the book, and I did get paid, soooo ....
No, no, the point is that after all this effort, you need to use some of the things you have learned to write an application that is not only MVC but also ILE as well. Now to fix this, I could rewrite my application using everything we have learned. But to be honest, that would be a fair amount of work for me, and I already know how to do it. So here is what I am going to do: I am going to let you do the rewrite and call it a learning experience for you. Whadda ya think?
So, what can we do to make this application truly representative of what we have learned in this book?
In General
First off, there are some things that you can do in general to the programs we have looked at to take advantage of what we have learned.
Control Statements
First and foremost, if you are at a level that supports the control statements, start using them. Replace your H-, F-, D-, and P-specs with the appropriate control statements.
Why? Because we need to move forward and leave the last vestiges of old positional RPG behind.
And you need to get used to seeing the control statements and being able to recognize in an instant what they are saying, just as you do today with the logic control statements. The only way to do that is to use them.
The end result should be to code /Free throughout and say goodbye, once and for all, to positional code.
Remove Delimiters
Second, if you are at a level where you can, remove the /Free, /end-Free delimiters. I have included them only because some people may not be there yet, but if you are, then take them out.
Activation Groups
What are you going to do about activation groups? You never had to think this about with OPM, but now you do. So, as you define your MVC application, how will you do your activation groups?
The obvious solution is that for the module and the view, you will use
*CALLER. But what about the controller? Will you let this be a named or system assigned group? The answer depends on how long you want it to live.
And how do you set the activation group? Remember, it is set in the compile command.
Binding Directories
Again, you will have to make decisions here. Probably not at first. If you have just one or two service programs, it’s not a big deal. But, as the number of service programs you have increases, you may want to buy into this. Or not. it’s up to you. But I would like you to at least think about it.
Binding Language
Same thing here. When you get a service program or two, think about this. And the signatures.
And maybe that is the essence of ILE. It is not really about how you will write a single program, except for being modular and using sub-procedures. It is more about how the application as a whole is structured and connected. How are things compiled? What activation groups tie everything together? What about binding directories and binding language? How will Frodo ever get into Mordor? Was Harry Potter bored after he destroyed He Who Must Not Be Named? Is it even realistic to think that Hermione, who was pretty hot, could get interested in someone who has to be one of the most boring people in the world? So many questions that need to be answered.
Model
We will start with the model because it is my favorite. How do we redo that in a more ILE format? Think carefully, and then raise your hand when you have an answer.
Service Programs
That’s right: service programs. What we need to do is rewrite this model program as a more general service program. There will only be one sub- procedure, the one to validate the product number. As I see it, as we expanded the fields involved in this display, we would add additional service programs. One for the vendor stuff, for example, another for something else. And, if there are two fields that need to be validated against the Product or Vendor Master, then we get into having multiple sub-procedures in that particular service program. I mean seriously, do I have to think of everything?
Of course, you could always add the other sub-procedures to this one service program. So, you could add a sub-procedure for verifying the vendor number. In practice, I would definitely put that sort of thing in a separate, vendor master-oriented service program, but in this case I will take either one as a serious attempt on your part to learn this stuff and would not reduce the grade you will be getting.
And that was actually your cue to develop that. The service program, that is.
Go on. Git. If you have learned anything at this point, you should be able to do that. If you can’t, then either you need to reread this book, or I need to rewrite it. Guess which of those two options will actually occur?
I/O-Oriented Service Program
One thing that I have seen some people suggest over the years is to insulate yourself from a potential database change by using separate I/O sub- procedures that are called by the sub-procedures used in the model.
That is, at this point I am sort of assuming you will use the RPG I/O as your database medium. If at some point you decide to switch to DB2, for example, all of your model programs would have to change to take out the RPG I/O logic and install either embedded SQL or the DB2 SQL Procedural Language. So, you would write a set of data access sub-procedures and call those from the service programs used in the model.
I know that sounds like extra code, and it is. And extra work, and it is. But it also gives you the ability to quickly swap out a new I/O method without affecting the edit logic. I’m not a big fan of this personally. If you were truly in a Web environment where you had to port your app on different platforms and using who knows how many different databases, it might be worth it, but in the IBM i world I think you are pretty much going to be RPG I/O or DB2. You should probably decide at the start of your modernization project which database environment you will use and then not look back. But remember that the I/O sub-procedures method is an option.
DB2 SQL/PL (Procedural Language)
And speaking of options, this is another one.
This book is about RPG, so we have sort of restricted ourselves to that. But when you look at developing the models, the use of a data-centric language like DB2 SQL/PL over a procedure-centric language like RPG is a serious option.
PL is a standalone declarative language (unlike embedded SQL that is inserted into an RPG program) that allows you to do control structures as well as bring the full power of DB2 data access into the picture. Using this, it would be quite practical—some people would say “quite sensible”—to do your model program entirely in PL.
If you are interested in this, I would suggest perusing the IBM Redbook SQL Procedures, Triggers, and Functions on DB2 for IBM i.
I have honestly not made up my mind yet about PL, not that anyone is waiting for me to do so. I admit, on one hand, it is an intriguing idea. But I have always had a certain morbid fear of two-page-long SELECTs. Your program can get pretty confusing pretty fast, and it looks positively awful. But, as Dan Lovell has reminded me on several occasions, that is more a result of bad coding practices, and you can make the same kind of mess in RPG. Although
/Free is a lot easier on the eye than SQL.
But my prejudices aside (I am working on that, by the way), PL versus RPG is a real option.
View
And now the view.
There is not much we can do to the RPG, but what if we put a PHP or Java module in here? You could, you know.
By moving all the display logic to a single module, it would be easy to replace that module with something written in a more Web-friendly language. For example, one of the advantages of ILE is that you can call a Java module from the RPG directly.
Or, you could use PHP, Ruby, or even just HTML or JavaScript. This is not as trivial as it may sound, however. For one thing, if you are using PHP or Ruby, those are server-side languages. As a result, you don’t really call the PHP script and run it on the i. Instead, you would need to open up a browser session and let the PHP script run on a server. Now you would probably do this via something like Zend Server running in your box, but you get the idea.
Historically, that has been an issue for MVC for the last few years—namely the difficulty in mating up RPG with other, especially server-side, languages. And that is probably one reason that some sites have opted to redo their systems either in Java or directly in PHP or another Web-oriented language. We will discuss that more in the next chapter. There are a number of options available, and work is being done constantly to make this process more seamless and less mysterious. I think the advantages of using your properly structured RPG code along with a true Web language are worth fighting for.
Controller
Finally, the controller.
I suppose you could take all the logic under the Enter key branch of the SELECT and put it in a sub-procedure embedded in this program. It’s not something I would do now with the small bit of logic that is there. However, as you add other fields that need to be edited, the code could become unwieldy, and putting it in a sub-procedure might be a good way to do that. I am a firm believer that people can only take in and process so much information at one time, and too large of a code section just becomes too big to handle. But maybe that is a limitation I have rather than humans in general. Certainly doing something like that has as many negatives as positives.
The main thing I would look at here is whether you want to do this in RPG at all.
That is, the main job of the controller is to call the view and the models. Now we know that the view is probably going to be in a scripting language. And the model with probably be RPG or PL. So what it boils down to is, what language should we write the controller in so that it can easily call both Web and native i languages and pass data back and forth? Will it be easier to do that from RPG (PL, great as it is, is not a candidate for this task) or a powerful scripting language like PHP or Java?
Many factors are involved in answering this question, and we will continue that discussion in the excerpt.
Next time: RPG and the Web. Want to learn more? You can pick up Dave Shirey's book, 21st Century RPG: /Free, ILE, and MVC, at the MC Press Bookstore Today!


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.
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:
LATEST COMMENTS
MC Press Online