![]() |
Datamanagers: Objects as Data and Methods
If you haven't read this article on how to create custom data managers or perhaps you read it and don't see what the big deal is - please read further!
Why go through the complexity of a data manager when I can just manage my data with direct queries and procedural code? vBulletin's data managing classes can look pretty complicated if you are unfamiliar with OO programming - but the benefits far surpass the few hurtles of adopting a more OO approach to your programming. A Live Example We'll start off with a project simulation. A client wants an application that keeps tracks of restaurant reviews. In order to achieve this, we need to set up a database tables that would look something like this: Table: restaurantAs you can, each review has a rating and user comments and links to a particular restaurant in the database. A Procedural Approach Tackling the project is a pretty standard task - the most obvious coding tasks being the insertion, updating, and deleting of restaurant reviews. (Of course, there could be other aspects too this type of project, but the overall focus will be narrow only to supplement the article's message.) Here is sample code for restaurant review insertion: PHP Code:
PHP Code:
But what does this have too do with OO programming and objects? Can't we just code functions to prevent this code duplication? The OO Mentality: Objects as Data and Methods With procedural programming, data and behavior are two completely separate entities. You input data into a function and get specific data back. This may not be a problem at times but the data manager classes bring up a specific benefits when handling data. Since objects are both data and methods, you can avoid messy situations with verifying data. The vBulletin Data Managers offer a nice clear and encapsulated way to check inputted variables. The class contains an array of valid fields which specify methods to verify the inputted data. Our restaurant review would look something like this: PHP Code:
Here would be some of the basic verification methods, all short and sweet: PHP Code:
Also, with the object - you get increased portability. Adopting to the original insertion and update code with the data manager. PHP Code:
The intention of this article was to shed to light the benefits of OO programming through a tool utilized and available to you in vBulletin. This article is focused on custom data managers, but the sky is the limit for creating OO applications. Good luck, and don't be afraid to take the dive into the powerful paradigm of object oriented programming. Additional Reading: |
Thanks for sharing actually I'm not professional OOP, but I know about it as a philosophy I studied it & passed it in college but I don't have the skills to program in OOP style
so I'm not sure of how much have understood of this article, but I wonder why didn't you make the restaurantid auto increment besides when we check for existence how could we know the restaurantid if we have thousands of restaurants finally is it a must to use datamanger in inserting data into database via a form embedded in a powered by vbulletin page??? or we can just do procedurally ? |
Quote:
For instance: Say you want to write a query to create a reply by a specific user to a certain thread automatically. This could easily be done with a query, however you must also remember to do a slew of other things such as update the post count of the user making the reply, update the user's last reply dateline field, etc... The datamanager always handles these things behind the scenes which you don't have to worry about - otherwise if you don't do all these things your forums can easily get out of whack. When creating a datamanager for yourself, the key is to code these issues into it to avoide complications later on. |
Thanks Antivirus
your answer added a lot to knowledge, now the picture is clearer thanks a gain |
All times are GMT. The time now is 10:04 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|