Re: handling database procedures in an object

From: Jonathan Feinberg <jdf_at_pobox.com>
Date: Wed, 13 Mar 2002 17:20:56 GMT
Message-ID: <130320021223455686%jdf_at_pobox.com>


In article <3c8f73cd$0$17548$6e365a64_at_newsreader02.highway.telekom.at>, Rainer Hahnekamp <kilohard_at_hotmail.com> wrote:

> It is said, that there should be at least one object, that handles the input
> and output with the database.
> I do not understand, how this should work.

The idea, I think, is to isolate all of the code which refers to the database proper, so that if in the future you decide to use a different database engine, or if the database engine you're using changes, you will not have to modify code all through your project.

Indeed, you could have a single program that works with different databases, simply by declaring an *interface* which is common to all databases, and then *implementing* that interface appropriately for each database.

For an example, your interface might declare these methods

DatabaseInterface:

   open_database_connection
   get_name_for_id_number(integer id)
   update_size_for_id_number(integer id)    close_database_connection

Then you might have one class that knows how to do those four things in terms of MySQL, and another class that knows how to do them in terms of Oracle, etc.

Then, in the parts of your program that need access to the database, you simply create an instance of a MySQLDatabase or an OracleDatabase, and invoke the appropriate methods.

   DatabaseInterface m = new MySqlDatabase;    m.open_database_connection;
   string name = m.get_name_for_id_number(42);    m.close_database_connection;

This is all pseudocode, of course, and the details would vary, depending on the language. But I *think* that whatever you read was describing this sort of pattern.

Hope this helps.

-- 
Jonathan Feinberg  jdf_at_pobox.com  Inwood, NY, NY
http://MrFeinberg.com/
Received on Wed Mar 13 2002 - 18:20:56 CET

Original text of this message