Generating VIPER modules with Boa
Like everyone else who ever tried to write iOS applications, after some point we were also getting fed up with the so-called ‘Massive View Controller’ pattern in the iOS world.
The standard MVC (Model-View-Controller) architecture usually results in very long ViewController subclasses: in a complex project classes over 1000 lines (even if you’re separating delegates and data sources) are not so rare if you do not introduce some higher level architectural pattern.
There were some initiatives to counteract the shortcomings in the standard MVC pattern, check out this article for example. But most of these patterns lacked the overall cohesion which could provide a proper architectural pattern, a guidance for putting the pieces together for the whole application.
VIPER takes the stage
This is why we were so excited when we first read the now kind of famous article about Architecting iOS Apps with VIPER. Then we read it again, and checked the sample code. Then we tried it in a dummy project. Then we implemented our first VIPER module in a customer project.
Even in the beginning the concept looked promising, and after taking all those steps we saw that this architecture could easily be used in practice, and does what it’s intended to do: taking the usual iOS app structure to a higher level.
I don’t want to praise VIPER too much, or elaborate on the SOLID principle, separation of concerns, etc, since a lot of people already wrote their observations, opinions and experiences. Even a meetup was arranged recently in Barcelona specifically for introducing VIPER for the iOS world.
Boa at your service
What is a minor inconvinience in VIPER is the high number of classes and the relatively complex folder structure what’s required to start implementing a module. You need to create the classes and separate folders for
- and ViewController classes
and even after that you would need to manually instantiate and connect these classes (doing the ‘dependency injection’) in the AppDelegate class. As you can see, this is a tedious process, but at least it can easily be automated.
That’s what our gem Boa is for. You can generate a basic project structure fit for VIPER, and then generate the modules just by specifying their names. Boa will take care of the rest.
You can install Boa with in the terminal by typing (only Ruby runtime is required, which can be found preinstalled on OSX)
$ gem install boa
Then in your project folder you can enter the following command to initialize a VIPER-based project:
$ boa init
You’ll be asked to input the basic details of your project (like a prefix for classes, and author name), then the basic folder structure and the AppDelegate and AppDependencies classes are generated. Starting from version 0.3.0 you can select Objective-C or Swift as the base language of your files.
Your first module (let’s call it Example) can be created with the command
$ boa module create Example
Which will generate the source files for all layers and put them in their respective place in the folder hierarchy. You are even presented with the lines required to instantiate your module in the AppDependencies class.
After using the generators you will need to add the files to your Xcode project.
That’s all: it’s simple but can already save you a lot of repetitive work. The tool is still under development and a lot of functions would be nice to have, like:
- integrating generators with Xcode
- generate model classes and handling CoreData persistence
- integrate basic navigation models to RootWireframe like tab-based navigation, slide-menu navigation, etc.
We continue to work and refine Boa as we ourselves explore the depths of VIPER. If you have any comments or issues with the tool feel free to start a discussion, or even contribute on GitHub!