Jump to content


Photo

Transaction script = Antipattern


  • Please log in to reply
2 replies to this topic

#1 Hall of Famer

Hall of Famer

    OOP Fanboi

  • Members
  • PipPipPip
  • 315 posts
  • LocationIthaca

Posted 22 April 2013 - 10:00 AM

I've been reading Martin Fowler's book about enterprise architecture patterns for a while now. From what I find about Transaction script, it is not object-oriented at all. Its basically procedural code despite the fact that it may use objects in every line of its code.This code below, for instance, is purely procedural despite the fact that it works with objects in every line:


<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>

Yeah this is not a complete picture of Transaction script pattern, and you can actually wrap up the code inside a transaction object's method(if you care to design one such object for each transaction). Still, this does not change the fact that the approach as a whole is procedural, its not object oriented. For this reason, transaction script is an anti-pattern in the object oriented world. Its usage should be limited to really simple/small script and for newbies to get used to database extensions such as PDO and MySQLi classes.

 

The better solution is to use a domain model instead, coupled with a data mapper with unit of work/identity map/lazy load/query object and such. OOP is mostly about OOA(object oriented analysis) and OOD(object oriented design), which fits well with domain model and data mapper. To create object oriented script, we will need to take this necessary design approach. Fortunately most large application frameworks do have domain models and data mapper, which makes object oriented design much easier and less time consuming. Here is an example of Domain model with Data mapper, as illustrated by Anthony Ferrara. Its just a small fragment of the client code, with the detail of implementation hidden inside the domain object and mapper. It is truly object oriented:

$mapper = new PersonDataMapper(new MySQLi(...));
$people = $mapper->getAll();

Using transaction script, however, the code is actually procedural in disguise. The usage of object operator -> does not make it professional object oriented code:

$m = new MySQLi(...);
$res = $m->query($query);
$results = array();
while ($row = $m->fetch_assoc($res)) {
    $results[] = $row;
}


So what do you think? Should professional coders all stop using transaction script if they havent, since its actually an anti-pattern? Perhaps there are times when people are comfortable with using an anti-pattern, but I doubt there is any justification for at least large softwares to apply it.


Edited by Hall of Famer, 22 April 2013 - 10:12 AM.

Welcome to the world of OOPHP! In a perfect script, everything is an object. You cannot be perfect, but you can approach as close as can.

zog841.jpg


#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,033 posts
  • LocationWA

Posted 22 April 2013 - 12:19 PM

The fact that you aren't using one pattern does not make the one you are using an anti-pattern.

#3 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,249 posts
  • LocationBelgium

Posted 22 April 2013 - 03:40 PM

A pattern becomes an anti-pattern when the disadvantages outweigh the advantages. Just because "IT'S NOT OO" does not make it an anti-pattern. That said show me any OO project and I'll show you procedural coding.

TS is a simple pattern to solve simple problems (patterns actually solve problems you know, not just to put buzzwords in your project).

A TS can be a function, method, or even a class (Command pattern). A simple example of a TS
 
function bookHotelRoom(Room $room, Guest $guest, PaymentGateway $card) {
  // 1. check room availability
  // 2. calculate rates
  // 3. update the database
}
A simple transaction. A TS is not a good choice when your code is really complex and changes a lot, in these cases it's better to use a Domain Model. Which does not mean that FOR EVERY problem you should create a domain model instead you should put a little thought in your app. design and not just break out the warships to S.W.A.T. a fly.

Many people seem to believe that if their code is simple they are not professional programmers which is why the code on many projects is really complex (CS degree required) while their domain is really simple to understand (bubblegum dispenser). Praise yourself when you have been able to solve a problem in a very simple fashion.

Read this more detailed article on TS by MF http://www.informit.....aspx?p=1398617

Edited by ignace, 22 April 2013 - 03:58 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com