• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Object Oriented Programming (OOP) Question Regarding Bean Data Storage and Subsets of Data

Contributor ,
Jun 15, 2017 Jun 15, 2017

Copy link to clipboard

Copied

I just finished reading a book on introducing Object Oriented Programming (OOP) in ColdFusion.  I found the book to be very enlightening on many levels however I was left with some questions and there didn't seem to be anywhere to go after the book to learn more advanced topics or to seek clarification or more information.

I may be creating a few questions on this forum over the next few days/weeks with OOP related subjects and ColdFusion that I would love your feedback and opinions on - especially from people that have experience working with OOP.

This question pertains to bean objects and storing subsets of data within them.  I understand the basic concept of storing information within a bean to represent a single record of information. For example, you might have a "User" object bean which stores information like firstName, lastName, emailAddress, etc...

TBd2ip1.png

However I find that often times you may want to store a subset of data within your object. For example, you might let users store one or more mailing addresses to their record. I like to think of these subsets as "attachments" to the primary record.  They come from a different table in the database but they are very much a significant part of the root data.

In the past when I'm using procedural programming I store my user data in a structure object and then include a separate key for each subset of data which might contain a nested query like this:

JGJ3lI1.png

As you can see in the above screenshot I have an object containing the information on the user as well as a subset of data (attachments) for each address associated with the user.

Now I suppose this type of thing could be accomplished from within the bean object. Technically you can store anything you like in there... but does storing a sub-query of data violate the entire concept of a bean?  Or is there a graceful way to accomplish this that I haven't encountered yet?

One way I could accomplish this is by using a Service Layer to grab the user data from the DAO and then grab all of the addresses from a Gateway object and shove all of them into the bean like this:

eO9FLsD.png

I'm still new to using UML so please forgive me if I've used the wrong symbols.

In the above example the client would request getUserByID() from the UserService object which would return a bean object containing the data from the userDAO object and the AddressGateway object.  Is this the correct way to go about this type of data retrieval?

The only problem I can see with this type of bean comes when you want to update the user record using some type of DAO updateUser() method.  In this case, the bean contains an "addresses" query object which isn't part of the "users" table.  I suppose having extra data in the bean won't hurt an SQL update statement but it does involve passing extra data to a CRUD method that doesn't need to be there.

Views

290

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 18, 2017 Jun 18, 2017

Copy link to clipboard

Copied

LATEST

There is a much simpler object-oriented design. For a start, design for encapsulation. For example, use a minus sign for the variables (denoting 'private'), rather than a plus sign (denoting 'public').

You may store addresses as a collection in User, for example, as an array of foreign keys. You would then treat the array as you would any of the other variables in User.

user_design.png

In this design, the association from User to Address is 'has-a', so-called aggregation. Here, a user has 0 or more addresses. Also, an address belongs to at least one user.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation