Simply because it’s easier to design, code, test and modify, regardless of whether it’s a large application or a small database driven website.
The n-tier model has been around over 10 years now, and I think it’s a winner. The layout is straightforward to understand and it promotes the use of code reuse, which unlike some organisation’s view is not the same as copy and paste.
By isolating technology, changing it becomes very straightforward. In the early part of the 2000’s I re-architected a large Intranet system written in ASP/VBScript, VB6 and Sybase to a n-tier model. Suffering from over-engineering in places, the bulk of the processing took place in one VB6 dll. This caused a bit of a performance bottleneck and also proved difficult for more than two people to work on development at any one time. Despite these problems the application was an important part of the business and it’s inception made a visible positive difference to the bottom line.
So gradually the system was moved to a 5 tier system: presentation layer(ASP), business layer (VB6), data layer (VB6), data access layer (VB6) and the physical database (Sybase). Each release of the application had changes for the business and technological improvements. I often hear from software houses that they are driven by customer input and not by technology. This is a big mistake. Obviously you can’t just update a product to suit changes in technology, but not including technological enhancements creates a void in the application that eventually becomes impossible to fill. Each tier or layer kept the details of the next layer down from the layer above. For example the business layer had no knowledge of how the data was stored, it could have been in text files for all it knew, it just knew what methods in the data layer it needed to call to satisfy its data requirements.
Naturally there is a trade off. In the world of COM it’s often known as DLL hell, where incorrect entries in the registry cause the wrong DLLs to be called resulting in errors and catastrophic failures all over the place. In essence this is a deployment management issue and DLL hell is often the result of poor implementation procedures and a lack of understanding of COM. With the increased number of DLLs deployment becomes a bigger headache, particularly if you plan to deploy only those that have changed. Test, test and test the rollout - it’s the only way.
Having completed the re-architecture, changes in the business meant that Sybase needed to be swapped for Oracle. With our 5 tier model, the only parts of the application that knows how and where the data is stored is the data, data access layers and the physical database. The data layer was included as it contained some embedded SQL for performance purposes, but most database activity was via stored procedures, which although the stored procedure would need to change, the data layer wouldn’t as the data access layer provided a generic interface to a specific database.
The most painfully part of the change was conversion of the stored procedures. The rest went well and was completed quickly. Because of the modular architecture we were able to extract and work on just the layers needed, whilst the rest of the team continued adding new functionality required by the business.
One last example. Recently we developed an online diary system for a TV camera crew company. The idea is that people booking a crew select their dates from the website and this sends an SMS message to the camera company, who may be on a shoot away from the office. We need the information entered to be correct partly due to the cost of sending SMS messages, so there’s a lot of validation. Typically we would add the validation into the PHP page, with calls to standard validation methods, so it was all in one place. This time we decided to create a class with a validation method working from an array created by the page. We do this already for database access and to be frank I don’t know why we didn’t do this for validation before. What a joy! Testing was so much easier, we simply had a page that created an array with the values we wanted to test and then displayed the return values on the page. Traditional solid black box testing. Fantastic!