For a long time, Object Relational Mapping (ORM) has proved to be a very good solution for data driven applications. Dynamic web applications are those who serve data store in a database; and this data is introduced by a (often not so friendly) user interface, hence the “dynamic” part. As design patterns emerged, one particular pattern became very used in frameworks and applications, which it was the Object Relational Mapping design pattern.
The basic idea behind the ORM pattern is to “map” each table (relation) to a class in our application. Following this notion, then a particular instance of a class was a row in the table, which could, at some point of the application’s lifecycle, be persisted or not. ORM frameworks handled the process of creating, retrieving, updating and deleting rows as objects, finding a way to know if a particular object has been saved or not. The basic layout of the behavior of a ORM tool/framework would be like this:
A class is generated for each relation we found in the database. We make our data model, then we generate the classes for it. Notice how I’ve made some underlining on generate, that is because most of the ORM frameworks has a built in class generator. After all, what good would they do if they don’t bring this?
The two major ORM frameworks I know for PHP, which are Propel and Doctrine, both have built in generators for our models. With these generators, we get a set of classes each handling a task on the tables we have on the data backend. So far, this sounds great! But the good part hasn’t yet arrived.
For the purpose of this application, let’s create a sample database with the Northwind database script, which has been used for these purposes for a long time due to the fact that has a few relationships, some good data and all the cool things to create a nice playground for testing an sort of data technology.
When we are developing a full blown application, we have some validation rules and some desired behaviors, like for instance, to know the full price of an order we use an equation, in a typical scenario, this would be a simple equation like this one:
Full Price = (Unit Price * Amount) + Shipping + Handling – Discount
This is what we call a business rule, and they are extracted from the client, which sometimes can be ourselves. Business rules define how our application responds to input and are the very core of it, however the can change at any point of our application. We could embed our business rules into the data extraction routine (although is not always possible); for instance, we could store a view in database returning the rows we need, something like this:
(`orderdetails`.UnitPrice * `orderdetails`.Quantity) AS RawPrice,
`orders`.Freight AS ShippingHandling,
`orderdetails`.Discount as Discount,
(`orderdetails`.UnitPrice * `orderdetails`.Quantity + `orders`.Freight) -
`orderdetails`.Discount AS Total
`orderdetails`.OrderID = `orders`.OrderID
ORDER BY Total DESC
This approach is (sadly) used by many online applications nowadays, where the business logic is embedded in the data source. With this approach, one main issue comes to scene: maintainability. Maintainability is the nightmare of many applications; in this case, the big trouble would occur if any of the calculating equations needs to be changed. To do so, we would require access to the database, change the view and use the new equation. This is a viable solution, because after all, it works! But attaching these application specific calculations to the data store is not good because you would have to change the way your data is stored to make changes, and practice demonstrates that whenever possible you should avoid messing with your database.
The other way to do this is by using the same classes generated by those ORM frameworks we talked about, like Propel (example are using Propel 1.5). The code doing the same function we just did would now be:
$query = OrderDetailsQuery::create();
`orderdetails`.Quantity + `orders`.Freight -
The outcome of this query is the same than before, but now our business rules are on the data layer, however, this is not quite yet the result we want. For this example, a development like this one might work since we are just making a simple and plain query, but suppose we have another table called Customer2Product to store the preferences of customers using products and we need to insert a row into this table every time we add a product into a customer’s orders. Obviously this behavior cannot be attached into the data store, but in some cases we might do it in the data access layer.
Having the business rules in the data layer is not good, because we need to keep in our architectures a clean separation of concerns. The data layer is concerned only on managing (CRUD) data, not handling logic rules regarding the application. To handle such rules, we need to create another set of classes, which are the business models.
The business layer should have a common gateway to our Data Access Layer, thus allowing communication with the data source. At first glance, the most important improvement would be the independency of data store. If we choose to change from Propel to Doctrine, or to anything else, we would only need to change our models, and the middle point between the Business classes and the Data Access Layer.
To communicate the Data Access Layer and the Business Layer we need to create a class working as a middle man, let’s call it an Adapter. The Adapter should be an interface, to allow the use of implementations. How this Adapter is implemented is a different problem on every architecture, but do keep in mind the development premise called YAGNI (You ain’t gonna need it). A sample interface would be like this:
public interface ITranslator
public function Select($criteria);
public function Count($criteria);
public function SelectOne();
public function Save($model);
public function Delete($criteria);
After this interface is built, we would just need to implement it for each Data Access Provider we might use regardless of the type of provider.
Another setback of having the business (logic) rules of our application in the Data Access Layer is that we are directly introducing a dependency on a 3rd party tool, which is our ORM framework (this applies for every ORM frameworks, even those made by yourself). If at some point of development, we choose to remove the ORM framework in use, because of any reason, we would need to rebuild/remake most of our classes, which would be awful!
You might think… My ORM framework is very strong; I won’t be in need to change it ever!
Well, if that’s the case, then think about the amount of projects failing each day due to dependency problems. Remember that there have been many software crises and that software is a field constantly in development, meaning that tools will evolve and patterns will evolve as well. Every day there are new patterns, new technologies and many new or improved resources available for developers, so we cannot tie our architecture to any specific tool/library just because is the hot thing.
Following the wise words of many designers and architects we need to keep in mind that in software history we have never seen such thing as a “Software Silver Bullet”.