SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2009
    0 Post(s)
    0 Thread(s)

    DAL (Data access layer) design question

    Lets i have two classes: Posts and Comments mapping the Posts and Comments tables from the database.

    I want to get all coments from a specific post.
    Should I make post->getAllComments() or comments->getByPostID(postid).
    I have one class representing each table and one class represent each row of that table.

    What do you think is the best way?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Lancaster University, UK
    2 Post(s)
    0 Thread(s)
    As the comments class is mapping comments from the database, I'd say you should use Comments->getByPostID(postid).

    Of course, if the Post class has access to the Comments class, then by all means use Post->getAllComments() as a helper method which simply calls Comments->getByPostID(Post->ID).

    Can't have too many helper methods :P


    It may be important to note, however, that it is useful to have the actual record class (i.e. Post, where Posts is the mapper class) unassociated from the database/mappers entirely. That way moving records to alternative database systems (e.g. a backup system) will take much less code. Post->getAllComments() implies that Post has access to the database connection, which (depending on your system design) may not be the best approach.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Posting Permissions

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