SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unit of Work example

    Has anyone here successfully implemented a Unit of Work pattern?
    I've been reading Fowler's POEAA and that looks like a useful pattern, but his examples are Java which I don't know. I was interested in seeing how you would use it with php, maybe someone has a simple example code they could post?

  2. #2
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by MikeShank
    Has anyone here successfully implemented a Unit of Work pattern?
    We have done something similar and are about to do it again. Here is a rough sketch of the old version. First usage...
    PHP Code:
    $finder = &new ThingFinder();
    $transaction = &new Transaction();
    $transaction->attach($finder->findById(4));
    $transaction->attach($finder->findById(5));
    $transaction->commit(); 
    Here is the main method...
    PHP Code:
    class Transaction {
        ...
        function 
    commit() {
            
    $this->_connection->begin();
            for (
    $i 0$i count($this->_uncommitted); $i++) {
                
    $persistent = &$this->_uncommitted[$i];
                if (! 
    $persistent->commit()) {
                    
    $this->_setError($persistent->getError());
                    
    $this->_connection->rollback();
                    return 
    false;
                }
            }
            for (
    $i 0$i count($this->_uncommitted); $i++) {
                
    $persistent = &$this->_uncommitted[$i];
                
    $persistent->confirmCommit();
            }
            
    $this->_connection->commit();
            return 
    true;
        }

    We are rebuilding some persistence stuff as I write this, so I don't have a working version to hand.
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  3. #3
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Marcus - with you example, how do I get the results? I guess something like $finder->findById(4) corresponds to a SELECT - where to the results pop out?

  4. #4
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by HarryF
    Marcus - with you example, how do I get the results? I guess something like $finder->findById(4) corresponds to a SELECT - where to the results pop out?
    That is basically right. We actually have a SQL query class to build the query. At the time it was the usual ActiveRecord pattern. The base persistence class was...
    PHP Code:
    class Persistent {
        function 
    Person($row) { ... }
        function 
    getValue($field) { ... }
        function 
    getId() { ... }
        function 
    update($new_data_as_hash) { ... }
        function 
    commit() { ... }
        function 
    confirmCommit() { ... }
        function 
    createQuery() { ... }   // Abstract, static
        
    function create() { ... }   // Static
    }

    class 
    Finder() {
        function 
    Finder() { ... }
        function 
    findById($class$id) { ... }
        function 
    findByQuery($query) { ... }
    }

    class 
    Query {
        function 
    Query($table$alias false) { ... }
        function 
    addJoin($table$alias$local_key$foreign_key) { ... }
        function 
    addField($column) { ... }
        function 
    addLimit($max$offset);
        function 
    addOrder($column);
        function 
    mustEqual($column$value) { ... }
        function 
    like($column$match) { ... }
        function 
    withinDate($earliest$latest) { ... }
        ...
        function 
    asSql() { ... }

    To grab all of the people for example...
    PHP Code:
    class Person extends Persistent {
        ...
        function 
    createQuery() {
            
    $query = &new Query('people');
            
    $query->addField('name');
            ...
            return 
    $query;
        }
    }

    $finder = &new Finder();
    $iterator = &$finder->findByQuery(Person::createQuery());
    ... 
    The UnitOfWork is just a container as far as I can make out. The Transaction above is rather a specialist form of this. I am curious to see how our next attempt at this works out.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  5. #5
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for you comments.

    Here is what i came up with, any comments or criticisms are welcome.
    The code below is stripped down for example but i have actually tested this and it seems to work.

    PHP Code:
    class MyObject
    {
        var 
    $object NULL;
        
        function 
    MyObject($object)
        {
            
    $this->link $object;
        }
     
        function 
    _test()
        {
            echo 
    "<PRE>";
            
    print_r($this->object);
            echo 
    "</PRE>";
        }
            
        function 
    get($key)
        {
            return 
    $this->object[$key];
        }
        
        function 
    updateObjectValue($key$value)
        {
            
    $this->object[$key] = $value
        }


    PHP Code:
    class ObjectModel {

        var 
    $objectsMap NULL;
        var 
    $UnitOfWork NULL;
        
        function 
    ObjectModel()
        {
            if(!
    $_SESSION['UnitOfWork'])
            {
                
    $this->UnitOfWork =& new UnitOfWork;
                
    $_SESSION['UnitOfWork'] = serialize($this->UnitOfWork);
            }
        }
        
        function 
    addToObjectMap($object)
        {
            
    $this->getSessionObjects();
            
    $this->objectsMap[$object->get('object_id')] = $object;
        }
            
        function 
    getFromObjectMap($key)
        {
            return 
    $this->objectsMap[$key];
        }
        
        function 
    _loadObjectsFromDB()
        {
            
    /*
                make call to database and create an Object from each array returned
                
                $sql = "SELECT * FROM ..."
                
            */
                    
            
    while($object mysql_fetch_array($sql))
            {
                
    $newObject =& new Object($object);
                
    $this->addToObjectMap$newObject );
            }
        }
        
        function 
    getObjectMap()
        {
            
    $this->_loadObjectsFromDB();
            return 
    $this->objectsMap;
        }
            
        function 
    addObject()
        {
            
    $this->getSessionObjects();
        
            
    /*
                ... 
                create new object from $_POST values ($newObject)
            */
            
            
    $myNewObject = & new Object($newObject);

            
    $this->addToObjectMap($myNewObject);
            
    $this->UnitOfWork->registerNew($myNewObject);

            
    $this->updateSessionObjects();
            
            
    // redirect user 
        
    }

        function 
    editObject()
        { 
            
    $this->getSessionObjects();

            
    $object_id $_POST['object_id'];
            
    $object =& $this->objectsMap[$object_id];

            
    /*
            ...
            edit object here
            
            $object->updateObjectValue('my key', 'new value')
            
            */

            
    $this->UnitOfWork->registerDirty($object);
            
            
    $this->updateSessionObjects();
            
            
    // redirect user     
        
    }

        function 
    updateSessionObjects()
        {
            
    $_SESSION['UnitOfWork']    = serialize($this->UnitOfWork);
            
    $_SESSION['objectsMap']    = serialize($this->objectsMap);
        }

        function 
    getSessionObjects()
        {
            
    $this->objectsMap unserialize($_SESSION['objectsMap']);
            
    $this->UnitOfWork unserialize($_SESSION['UnitOfWork']);
        }
        
        function 
    cleanUpSessionObjects()
        {
            unset(
    $this->UnitOfWork);
            unset(
    $this->objectsMap);
            unset(
    $_SESSION['UnitOfWork']);
            unset(
    $_SESSION['objectsMap']);
        }

        function 
    commit()
        {
            
    $this->getSessionObjects();
            
    $this->UnitOfWork->commit();
            
            
    $this->cleanUpSessionObjects();
             
             
    // redirect user   
         
    }

    PHP Code:
    class UnitOfWork {

        var 
    $newObjects        = array();
        var 
    $dirtyObjects    = array();
        var 
    $removedObjects    = array();
        
        function 
    UnitOfWork()
        {
        }
        
        function 
    registerNew($obj)
        {
            
    $this->newObjects[$obj->get('object_id')] = $obj;
        }
        
        function 
    registerDirty($obj)
        {
            
    $this->dirtyObjects[$obj->get('object_id')] = $obj;
        }

        function 
    registerDeleted($obj)
        {
            
    $this->removedObjects[$obj->get('object_id')] = $obj;
        }

        function 
    commit()
        {
            
    $this->insertNew();
            
    $this->updateDirty();
            
    $this->deleteRemoved();
        }
        
        function 
    insertNew()
        {
            foreach(
    $this->newObjects as $object){
                
    $m = new SiteManagerMapper;
                
    $m->insert($object);
            }
        }

        function 
    updateDirty()
        {
            foreach(
    $this->dirtyObjects as $object){
                
    $m = new SiteManagerMapper;
                
    $m->update($object);
            }
        }

        function 
    deleteRemoved()
        {
            foreach(
    $this->removedObjects as $object){
                
    $m = new SiteManagerMapper;
                
    $m->delete($object);
            }
        }


    PHP Code:
    class DataMapper {
        
        function 
    insert($object)
        {
            echo 
    "insert into database:<br>";
            
    $object->_test();
        }
        
        function 
    update($object)
        {
            echo 
    "update database:<br>";
            
    $object->_test();
        }
        
        function 
    delete($object)
        {
            echo 
    "delete database:<br>";
            
    $object->_test();
        }


  6. #6
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    I now see what you are doing. You are using UnitOfWork for "long transactions". That is, multi page transactions. You are freezing state using the session management between requests. Is that correct?

    I haven't needed to do this myself (and would be interested to know how you get on). I try to make the page handlers atomic transactions and have yet to get stuck.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  7. #7
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To tell the truth I'm not sure what im trying to do .
    What I got from reading the chapter in POEAA on unit of work was if you were doing a lot of update/create/deletes on data, that that with this pattern you would load your data into memory, manuiplate it there and then commit it to the database when finished. Essentially cutting down on the database calls. I know it's overkill for small web apps, but I am really interested in learning the concept in case I ever have a need for it. I don't have any formal programming training and am just beginning to use OOP with php. Would you say I'm on the right track here?

  8. #8
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by MikeShank
    Would you say I'm on the right track here?
    I think it might be overkill right now. I would get the RowDataGateway under your belt first. At that point you have a fork in the road, going either to ActiveRecord/TransactionScript or DataMapper/DomainModel. The former is usually a more natural fit for PHP. At that point, the need to wrap everything in transactions will force you into the UnitOfWork. Trying to design ahead, by starting with the UnitOfWork, I think will be too difficult and abstract.

    You could try it though. Good book, isn't it?

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...DataMapper/DomainModel. The former is usually a more natural fit for PHP.
    Thought that myself as well Though I've still to look at a Unit of Work and get a feel for it I suppose...

    Interesting stuff here though ?

  10. #10
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This one caught my eye because I asked the same question a while back. (And looking at it, I realized I never answered Marcus's question about my DataMapper -- let me know if you still want to see it.)

    I have to agree with Marcus here -- Unit of Work is a really complicated pattern to implement in PHP. I only understood it (or at least I think I understood it) because I can read Java and I'd had some good experience implementing patterns in PHP. We used the DataMapper and DomainModel patterns at my last job and it worked pretty well for our purposes. Had I known more at the time, I might have opted for the Unit of Work because we were truly building enterprise PHP apps. That said, we did make it work without.

    I imagine you'll quickly run into the same problem I have with Unit of Work -- determining persistent vs. temporal object attributes. Somewhere in the application a decision needs to be made about what to pass along to persistent storage and what to ignore, and the question (for me, anyway) is which objects make that decision and how they make it. Marcus, in your last post to the thread I linked above, you mentioned "generated" persistent classes (apparently) as a way of sidestepping this whole issue. How are you doing the generating?
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.

  11. #11
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by brainpipe
    Marcus, in your last post to the thread I linked above, you mentioned "generated" persistent classes (apparently) as a way of sidestepping this whole issue. How are you doing the generating?
    All of the data properties are in XML. XSLT (via C++ Xalan) generates both the SQL and the PHP Persistence classes. We drifted into ActiveRecord with these, but our resident Java guru has pulled us back into the DataMapper approach as well. We are just putting back in the transaction stuff after some tinkering. I am a little constrained here in that I cannot post current copyrighted code.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  12. #12
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All those restriction... Umm...

  13. #13
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all of your input.
    This might sound dumb, but what exactly is the difference between Row Data Gateway and Active Record? Is Data Mapper just the insert/update/delete logic for an Active Record?

  14. #14
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by MikeShank
    Thanks for all of your input.
    This might sound dumb, but what exactly is the difference between Row Data Gateway and Active Record? Is Data Mapper just the insert/update/delete logic for an Active Record?
    As soon as you start to put extra methods into a RowDataGateway you start to get an ActiveRecord. It means you are mixing database mapping with domain logic. This is a whole different style from the DataMapper approach.

    With ActiveRecord approaches (the ultimate is "Naked Objects") you are assuming an isomorphic relationship between the data model and the domain model. If this is unsustainable then the ActiveRecord will get a lot of relationship handling code piling in. The advantage is it's simplicity. Both the domain code and the data mapping code just drop into their places.

    With the DataMapper things are more complicated. You get nice layering, all the database code ends up in the mapping layer, but you seem to get some horizontal bleed. If a couple of domain objects share a database object, the mapping code can end up duplicated. I think this is because the mappers can easily become rather god like upon the layer below.

    Just personal experience so far. Even I don't trust me .

    yours, Marcus.
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  15. #15
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeShank
    Thanks for all of your input.
    This might sound dumb, but what exactly is the difference between Row Data Gateway and Active Record?
    Definitely not dumb. Some of the geeks around here have the advantage of Java experience, which (IMO) makes it easier to understand some of the more complex patterns. For me, things like DataMapper and Unit of Work are difficult to wrap the brain around, but once you do it's pretty simple. Then again, as of a year ago all this was new to me, so take it for whatever it's worth.

    We used the DataMapper pattern because we really had no other choice. We didn't know most of our requirements, and what we did know changed almost daily. The primary project for my team was a reporting application... it kept getting out of hand as we'd add/change requirements, and worse still, we had to refactor the database schema several times (and once completely overhauled). We had to find a way to (a) minimize the impact of changes and (b) completely separate objects from database structure. The first one we solved by using OO methods and patterns; the second we solved using a simplisitic DataMapper.

    Basically, we had one primary DM and one secondary, a list wrapper for accessing multiple-row recordsets. For each object, we created a child class to the primary DM. So, if we had a User object, and the DM was named DataMapper, we created UserDataMapper as an extension. All UserDataMapper did was provide a map between the User object and the User table(s). This is where Marcus's comment about horizontal bleed comes into play -- if we'd also had a Customer object that used the same tables, we'd have needed to refactor. We didn't have that problem, I think because we had control of the database structure and we tried to keep that and the objects very simple. Anyway, when the User object needed to load or store persistent data, it used the UserDataMapper, something like this:
    PHP Code:
    $userDM = &new UserDataMapper();
    $userDM->setField('name''john');
    $userDM->save(); 
    Between that and the QueryBuilder, we managed to keep SQL almost entirely out of the application objects and make them independent of the database. This approach can get very complex though, particularly when you start talking about concurrency or just complex joins. Our particular implementation was modeled after an article in php|architect -- I forget which issue, but I'll look it up if you want to know. (Actually I figure this is already more info than you wanted. ) One of my teammates took the concept from the article and made it much simpler -- we'd never have made it work if he hadn't been able to do that.

    ActiveRecord is less abstract; it's basically just an object-per-row pattern, at least as I understand it. You've got a class that exactly matches the table structure, and for each row you create an instance of that class. I'll leave it at that since I don't really know it well. Regardless, I'd avoid DataMapper unless you're in a situation like the one I described (you know, when the project is really, horribly, awfully managed -- and also quite large).
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.

  16. #16
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please do look up the php|a issue that inspired you, if you can, I am very interested in looking at the article.

  17. #17
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to break this down to a really simple example.

    Say I want to make an application that simply allows a user to add/edit/delete "Names".

    Would I load all of the "Names" into memory as ActiveRecord objects?

    And when adding/editing/deleting objects I would need to do this in 2 places (the objects stored in memory and the database).

    I know im off somewhere here, if someone can take this example and show me how they would create this example app I think it would help greatly. Thanks in advance


    database:
    PHP Code:
        CREATE TABLE `Names` (
          `
    idINT NOT NULL AUTO_INCREMENT
          `
    nameVARCHAR(50NOT NULL,
          
    PRIMARY KEY (`id`)
        ); 

    Active Record:
    PHP Code:
    class Name{

        var 
    $id NULL;    
        var 
    $name NULL;

        function 
    Name($id$name)
        {
            
    $this->id $id;
            
    $this->name $name;    
        }    
        
        function 
    add()
        {
            
    // add to database
            // add to $names array
        
    }
        
        function 
    update()
        {
            
    // update database
            // update $names array        
        
    }
        
        function 
    delete()
        {
            
    // delete database
            // delete $names array        
        
    }
        
        function 
    find($id)
        {
            
    // find in $names
            // if not find in database        
        
    }


    Registry(dont know if this is the right term):
    PHP Code:
        
        $names 
    = array();
        
        
    $sql mysql_query("SELECT * FROM Names");
        
        while(
    $result mysql_fetch_array($sql))
        {
            
    $names[] = new Name($result['id'], $result['name']);
        } 

  18. #18
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    Please do look up the php|a issue that inspired you, if you can, I am very interested in looking at the article.
    Found it -- the January 2003 issue, in an article by Shawn Bedard called Implementing Database Persistence Layers in PHP. I think Shawn's example is more complicated than it needs to be, and it also leaves a lot of SQL in the application code, which IMO defeats the purpose. But it's a good overview of the concept and PHP implementation.
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.

  19. #19
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeShank
    I know im off somewhere here, if someone can take this example and show me how they would create this example app I think it would help greatly.
    I think that's pretty close, you'd probably just want to combine it with the RowDataGateway. (I'm stealing all of the ideas here out of PoEAA.)
    PHP Code:
    class NameGateway
    {
        
    // ...

        
    function &findById($id)
        {
            
    $sql 'SELECT id, name FROM Names WHERE id='.$id;
            
    $rs mysql_query($sql);
            
    $row mysql_fetch_array($rs);

            
    $oName = &new Name();
            
    $oName->setId($row['id']);
            
    $oName->setName($row['name']);
            return 
    $oName;
        }

        function &
    findAll()
        {
            
    $col = array(); // collection
            
    $sql 'SELECT id, name FROM Names';
            
    $rs mysql_query($sql);
            while (
    $row mysql_fetch_array($rs))
            {
                
    $oName = &new Name();
                
    $oName->setId($row['id']);
                
    $oName->setName($row['name']);
                
    $col[] = &$oName;
            }
            return 
    $col;
        }

        
    // ...

    Then access it like so:
    PHP Code:
    // for multiple records
    $nameCol = &NameGateway::findAll();
    // now $nameCol is an array of Name objects

    // for a single record
    $name = &NameGateway::findById($id); 
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.

  20. #20
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Registry
    Maybe I'd choose instead Archive ?

  21. #21
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So the Row Data Gateway is essentially used as a finder or to load records from the database and then return them as a single object or an array of objects? And as far as keeping an array of objects in memory, and having to modify both that array and the database when add/edit/deleting?
    I ask this because when looking at the examples in PoEAA about Row Data Gateway, although I don't know Java, it looks like when doing inserts/updates/finds he is doing something like Registry.getPerson(id) which i assume is some sort of an array that is holding objects already loaded.

  22. #22
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    Registry.getPerson(id
    But remember that there is actually one object created for every result returned from the database;

    If I remember this Pattern correctly - going to read up on this pattern in a moment - if so, why'd there be the need for an array of Persons within an Registry ?

    Umm...

  23. #23
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeShank
    So the Row Data Gateway is essentially used as a finder or to load records from the database and then return them as a single object or an array of objects?
    The RDG is simply a gateway to the database. All it does is provide database access and (I think) act as a TransferObject to hold the data from persistent storage for delivery to the application objects. In other words, yes.
    Quote Originally Posted by MikeShank
    And as far as keeping an array of objects in memory, and having to modify both that array and the database when add/edit/deleting?
    Yes, but you wouldn't be doing both in the client code. The RDG would handle inserts, updates and deletes.

    Quote Originally Posted by MikeShank
    I ask this because when looking at the examples in PoEAA about Row Data Gateway, although I don't know Java, it looks like when doing inserts/updates/finds he is doing something like Registry.getPerson(id) which i assume is some sort of an array that is holding objects already loaded.
    He's just combining a couple of other patterns (Registry and IdentityMap) with RDG, two more patterns I really haven't spent much time with. As I understand it, it's just a way of keeping track of objects currently in memory -- as you said, objects already loaded. My guess is that you won't normally have much need for that in PHP4, except perhaps when using UnitOfWork (which is way overkill for most PHP4 apps).

    You might take a look at some other pattern literature to get different perspectives. The Hillside.net Patterns Library has quite a lot of links to a wealth of information.

    Also, I've only worked with DataMappers (simple ones, at that), and I'm only just now beginning to work with ActiveRecords and such -- which is to say I'm not really qualified to give solid advice. I'm kind of hoping someone who knows better will step in and make sure we're on the right path here.
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.

  24. #24
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In brainpipe's example code it looks like a Data Access Object which is returning Value objects. Not sure if this is the case, I'm just trying to apply names I know to this. Is this making any sense at all?
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  25. #25
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that's correct. I think what I'm calling a TransferObject, you're calling a ValueObject. Both are used to marshal data from one location to another. If I'm misnaming the TransferObject, someone please correct me.
    I think there is a world market for maybe five computers.
    - Thomas Watson, chairman of IBM, 1943.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •