Enter the concept of a multiple tiered architecture. The basic gist is this: group code into areas of similar functionality and limit the interaction between these groups. Each group (or tier) should do what its designed to do and have minimal dependencies . This concept is known as decoupling.
This is the part where most new developers get lost. I'll try my best to do this topic justice, but bear in mind that I will need to simplify an awful lot.
The first thing you need to do is to remove your business logic from your pages. Pages have two jobs - to pass user input relatively unmolested into your business layer and to display the data that it requests from said business layer - THAT'S IT. What does this mean?
The missing link for most beginners here is a foundational one - Object Orientation. It's not the intention of this article to explain OO - refer to this website to get your feet wet.
The central concept here is that of a black box. You pass it something. It gives you something back based on what you gave it. You don't care how it does it (the underlying implementation) or from what tables in what database. You just have an understanding, or a contract with the black box that if you gave it a parameter, it will give you something in return. This contract is known as an interface. If all you know is creating dynamic web pages, pages will be the center of your universe. You will think in pages and that will confuse you. STOP THINKING IN TERMS OF PAGES. The most basic unit of functionality in an application is not the PAGE, it's the CLASS. Read up on OO - it will give you the understanding you need.
Let's look at an oversimplified example. Suppose we needed functionality to save a new user into the following table
TABLE_USERS Id, integer First_name, varchar() Last_name, varchar()
The old 2-tier method you're used to using for this type of scenario probably has a page to handle the heavy lifting. /My.PageIf user clicks submit, do this:sql = "INSERT into TABLE_USERS id, first_name, last_name"execute sql .....HTML code to show your input formFirst Name: _______________________Last Name: _______________________[submit]
This will get tedious if you have this code on every page that needs to write to the USERS table.
With a 3-tiered approach, you introduce your business layer that handles the details writing to the database. Every page that needs to write USER data to the database just needs to call on this layer.
Take the following class/business object
Class UserManager { function newUser (first_name, last_name) { sql = "INSERT INTO TABLE_USERS id, first_name, last_name" execute SQL}}
Now, your page/form just has to do this:
/My.Page
If user clicks submit, do this:
UserManager.newUser(first_name, last_name)
.....
HTML code to show your input form
First Name: _______________________Last Name: _______________________[submit]
I'm trying to be language agnostic and just get the idea across, so continue to bear with me. The example above does not look like a big deal - after all, we're just executing a single sql statement. However, what if we begin adding more business rules? What if we need to first verify we don't already have another user with that same first and last name before actually inserting the record? Simply modify your userManager class and you're done - no more hunting through all your pages and adding that functionality over and over again. That's when you begin realizing the benefits of having multiple tiers. Your page, database, and business object begin functioning as discrete components , just like the bacon, lettuce, and tomato in your BLT sandwich. At any time, you can remove or replace any of these components and if you've designed it properly, you'll only have to make minimal changes to the rest of your application to cope with your change.
Thus ends my rather elementary watered-down explanation of multi-tiered architectures. The good news is that if you understand this, you can move on to understand some of the more complex concepts in application development - the kind the big boys understand. The bad news is: if you're relying on this particular article to understand this relatively rudimentary concept, you've got a long ways to go.