0 Replies Latest reply on Dec 9, 2013 2:36 PM by Aegis Kleais

    Using CF + OOP and getting data from a database

    Aegis Kleais Level 3

      OK, even though I'm pretty new to OOP, I've been doing great things and coming to levels of comprehension with it at a pretty amazing rate.  My backend is CF10 (dev), MS-SQL 2012.

       

      How I used to get data : Before, I would create a CFC with a method like "executeSQL" and then I'd pass it some parameters, one of which was the name of a .SQL file that was subsequently loaded, executed and then the method would return the results to the caller.  I could pass it structures full of dynamic values in the event I wanted to pass "parameters" into the script so that it was dynamic.

       

      How I should get data: This is where I'm stuck.  I am familiar with Beans, Gateways and Services, but not to the extend or interacting with Data.  In my research, I hear people refer to Beans as singular records of data and Gateways as handlers of recordsets of data.  So let's use a blog post for example.

       

      • Post.cfc (Bean) - Represents a single blog post.  Used to perform CRUD-related functions on a post: deletePost(), getPost() (the equivalent of readPost()) and savePost() (a method that can perform createPost() and updatePost() actions)
      • PostGateway.cfc (Gateway) - Represents multiple blog posts.  One of the areas I'm confused.  Sounds like he would get called often when I want more than 1 record, or when I want to affect more than one, ie multi-record CRUD operations.
      • PostService.cfc (Service) - Provides high-level encapsualtion.  As I understand services, they are usually the middlemen you interact with and you give them a high-level command like "Get me the last 10 Blog posts", and it may run logic that calls Beans and Gateways in order to ultimately get that data returned to you.

       

      So, I call: PostService.getRecentPosts().  For sake of simplicity, I know a function like this could be passed parameters like 'numberOfPosts' to make it dynamic, but I'm just trying to wrap my head around the role of each object before I optimize them.

       

      OK, so PostService performs logic, and calls PostGateway.getPosts() function.  This is my first point of misunderstanding.  The GW should only be responsible for performing mass CRUD operations right?  So somehow the Service invoked the GW and the GW does what?  Does it simply run <cfquery> and supply hard-coded SQL in it?

       

      SELECT TOP 10

           p.post_id AS postID,

           p.post_title AS postTitle

      FROM posts AS p

      ORDER BY p.post_date DESC

       

      Before I make this thread too massive.  Let's just stop here and get my thinking straight.  If someone is familiar with the proper process of getting to this point, I'd love to pick your brain.