Re: Question regarding OOP and database access
- From: "Daniel Parker" <danielaparker@xxxxxxxxxxx>
- Date: 7 Jul 2005 07:56:54 -0700
leodippolito@xxxxxxxxx wrote:
>
> This way, if the Wheel table changes I am ok with the sql code. I don't
> need to track anything in DB layers. If I ask for a car or truck
> object, it will return a car or truck with the new-version wheel.
>
There may be specific cases where you might want to do something like
this, but not in general. You don't want to approach software design
in a way that rules out using features of the RDBMS. In general,
prefer joins.
> The cost of this is the n-round-trips that I have to make to the DB
> layer in the business layer. I lose the power of SQL join.
>
Right, not that it always matters. But there are examples of projects
that have failed because less gifted developers made all database
access work this way.
> Another possibility I can see would be to use an O/R mapping library.
> The cost would be runtime processing.
These tools bind result sets to program variables, which does incur
runtime processing, but is useful, and the cost is small relative to
the cost of the query. Unfortunately, they often do a lot more which
is not helpful, like caching, mapping data members directly to tables,
generating SQL which can not then be tuned, etc.
>
The following approach is widely used, and works well.
One, move all of your SQL text out of your software and into
configuration files. Parameterize it with placeholders for arguments.
In Java, the simplest approach is to use property files, which support
parameterized values. Access the SQL values by name in your
application.
Two, make sure that the queries and updates in your app use names
rather than position, and that insert statements specify the column
names to receive values.
You now have a certain amount of flexibility where you can isolate
changes to the configuration file, without immediately changing your
code. You can rename columns in select statements, since you can alias
them to the old names. You can modify and fine tune the SQL statement
as long as select statements return no fewer columns. You can add
columns to select statements. Insert and update statements can be
modified to take default values for new columns, if table structures
change.
If you can find an O/R mapping tool that allows you to specify your SQL
statements in XML files, and doesn't do much more apart from binding
the results by name to data members, that would probably be the best
choice.
Regards,
Daniel Parker
.
- References:
- Question regarding OOP and database access
- From: leodippolito
- Question regarding OOP and database access
- Prev by Date: Re: Design problem
- Next by Date: Re: OOP/OOD Philosophy
- Previous by thread: Re: Question regarding OOP and database access
- Next by thread: Re: Question regarding OOP and database access
- Index(es):
Relevant Pages
|