Re: Decouple SQL queries from class in OOP design
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Thu, 10 Nov 2005 18:01:40 GMT
Responding to Hongyu...
To my knowledge, in OOP design it's not good to mix SQL queries with application codes in classes, because it limits the usability of the classes and bound them to the local database schema. But I am not sure what the best way is to decouple them. The method that I am using right now is to create a hash to store all the SQL statements and prepare them in the main program, then I will pass this hash as a variable to all the classes that are going to use it. I am not sure whether there is any better design. Thanks in advance!
You are correct that one wants to decouple the problem solution logic from the database. (CRUD/USER processing is an exception.) Typically that is done by encapsulating the DB access in a subsystem that has a generic data transfer interface based on the problem solution's data access needs.
Within that subsystem one provides SQL command construction. To do that one has to have a mapping of data identity between the subsystem interface messages and the RDB schemas. So if one has an interface message like:
saveAccount (accountID, value1, value2, ... valueN)
The subsystem will have to have a mapping to know that saveAccount maps to the "Account" table, accountID maps to the "Account Name" field, value1 maps to the "current balance" field, and so on. (Essentially one identifies the individual tuple fields positionally in the message.)
There are various mechanisms based on table lookups to do this, of which your hash scheme is one (though the granularity is usually at the field level). This allows a SQL string to be incrementally built up for the saveAccount interface method by plugging in text snippets to a standard format.
Such lookup tables can be hard-wired in the subsystem. Alternatively, one can initialize them from external configuration files. That's more infrastructure work but it allows the entire RDB access subsystem to be reused as-is across applications by simply substituting the configuration file.
************* There is nothing wrong with me that could not be cured by a capful of Drano.
H. S. Lahman hsl@xxxxxxxxxxxxxxxxx Pathfinder Solutions -- Put MDA to Work http://www.pathfindermda.com blog: http://pathfinderpeople.blogs.com/hslahman (888)OOA-PATH
.
- Follow-Ups:
- Re: Decouple SQL queries from class in OOP design
- From: Hongyu
- Re: Decouple SQL queries from class in OOP design
- References:
- Decouple SQL queries from class in OOP design
- From: Hongyu
- Decouple SQL queries from class in OOP design
- Prev by Date: Re: State machine/ Multiple state question
- Next by Date: Re: Strategy and visitor Design Pattern
- Previous by thread: Re: Decouple SQL queries from class in OOP design
- Next by thread: Re: Decouple SQL queries from class in OOP design
- Index(es):
Relevant Pages
|