Object Oriented Programming and Databases

0
141
Sending Emails in PHP
Control structures in C++

14Programming itself evolved from several stages and now we have Object Oriented Programming as the most mature and popular approach. On the other hand, interaction with database has always been tricky and still there is no best and well establish approach.
More, when doing programing and having object oriented programing techniques with its several advantages on disposal, it is still hard for beginners to write classes which are easy to handle.

In this tutorial, you will learn how to write classes with the following properties:

  • good structure
  • least repetition of code
  • easier data transfers to/from database

For this, you can use any programing language witch supports classes.

Database:
Lets suppose that we have a table for students in a School System.
Student (id, name, father-name, dob, gender, address, class, phone, status)
id is primary key for the table.

Next:
We want to write a class that will read data from and write to the table, and objects of this class are supposed to be used in several places in the School System. Our class will have the same fields as the Student table, and will provide CRUD (create, read, update, delete) operations on the table.

To fully understand the power of our approach, we will first examine the other ordinary approach.

Approach 1: Ordinary Approach:

The following is the code of the class with two constructors.

/* *******************************************
* Student Class
* *******************************************
*/
class Student
{
public Student()
{
// default constructor, used for new student
}

public Student(int id)
{
// one parameter constructor, used for existing student
}

public void Save()
{
// if new Student() was called, then insert.
// if new Student(id) was called, then update.
}
}
/* ******************************************* */

Now, first consider we use the default public constructor for object initialization. In this case, for a new Student object, we will use constructor with no parameters,  and for a student-id, we will use a constructor with one single parameter, student-id. The parameter-less constructor will initialize the object fields with default values  and when user assign values to the fields, a record will be inserted after some validations. In the second case of constructor, data in the object fields will be populated from database and on save, it will update the record in Student table.

But here are some problems with this approach.

  1. The new student object before saving is still a valid object which can be used (by mistake) for other operations related to student, like issuing a book from library, registration in a course, etc. Each time, we will either have to throw exceptions or there will be inconsistent data in database.
  2. If we pass an incorrect student-id to the one-parameter constructor, which should return an error. We will throw an exception to stop object from creation, which means we will be expecting an exception/error everywhere we try to get a student-object for a student-id. More, a class constructor is not a good place for throwing exceptions.

This shows that path to object-oriented programming is not as clear-cut as it seems.

Approach 2: Our Modified Approach:

In our modified approach, we will use static methods for getting objects of the class and we keep the constructor(s) private. The following is code for our new Student class.

/* *******************************************
* Student Class
* *******************************************
*/

class Student
{
private Student()
{
// default private constructor, used for new student
}

public static Student GetStudent(int id)
{
// if the id is not present in database,
// return null
// else if id exists in database
// use default constructor to get an object
// read all the fields for from database in to the object fields
// return the object.
}

public static Student GetNewStudentObject(int id, string name)
{
// first call GetStudent(id) method.
// if result is not null, then return the result.

// else if the result is null, then do the following
// get a new Student object by calling the only constructor
// assign id and name to the object
// save object by calling Save() method
// return the object.
}

public void Save()
{
// if GetNewStudentObject() was called, then insert a record in database.
// if GetStudent(id) was called, then update the record in database.
}
}

/* ******************************************* */

In the above code, we see that each time we will have only valid objects of Student class for operations. And now we have not to throw exceptions when creating objects. We can check the returned object for validation. if null, then object was not created and here was problem in the data. On the other hand, if the returned object is not null, then we have a valid object for operations.

We see that the exception throwing is changed in condition checking. Exceptions are not good because:

  • Exceptions make code very complex and cluttered
  • Exceptions reduces the performance of the code significantly.
Sending Emails in PHP
Control structures in C++

Leave a Reply