Re: How do you create a data access layer?
- From: "Bjørge" <bjorge@xxxxxxxxxxxxxxx>
- Date: Tue, 30 Aug 2005 13:10:37 +0200
swansnow wrote:
> Maybe this is another newbie question... :)
>
> I've read that you really should have a abstracted data layer, to
> decouple database accesses from the rest of your code. I'm not really
> sure what this works out to in practice. Here's a guess:
There are at least two good reasons for decoupling database access from the
rest of the application:
- The database itself is maintained outside the application itself, an
annoying fact that calls for collecting as much of database stuff as
possible into a limited number of units.
- Rewriting an application for using other storage formats or access
components is much simpler when it's all put in one place.
.....and, additionally: Dividing code into logical blocks is a good thing,
IMHO.
> Instead of putting a query in the FormCreate handler, you call a
> method in a data module, say modUser.GetDataForUser(user_id, self)
> and the method does the query, and fills in your components for you.
> But that doesn't seem quite right. Does Delphi have a record object?
The closest one is TDataset - which is the base class for a hierarchy of
data access classes. It may be connected to data-aware controls and report
generators, too. The second part of your question concerns the use of field
controls, right ? have a look at TDataSet/TDataSource. Or, of course, you
may replace the data-aware controls with a few methods linking controls to
fields.
> Could the method return a record containing the user's data, and then
> the form could use whatever pieces of it to fill in it's own
> components? Do you have to have a method for any possible query you
> might need? That doesn't seem right either. What about joins over
> multiple tables?
Typically something you'd use datasets for.
> I'm confused, I guess you can tell :) I don't have any experience
> with this, and I barely know what a data module is...
>
> Here's a hypothetical situation:
> I have a form, and it needs to access 3 tables with a join in order to
> get all the data it needs to fill out its components.
>
> OK, so say the components are data aware (currently using a
> master-detail relationship in the datasource properties), and the user
> needs to be able to edit the data. How do you do that with a data
> access layer?
The data access layer will be made up by components. Choosing the
appropriate access components will let you edit the data, and post
modifications back to the db. What kind of database do you have in mind ?
> Maybe this is too complex for a newsgroup. Is there a into-level book
> or online tutorial on this subject? I've mostly found articles, and
> of course articles don't go into much depth. Mostly, they've served to
> convince me that I need to change my ways. :)
It's not too complex if you manage to keep questions' complexity down to a
reasonable level.
--
Bjørge
.
- References:
- How do you create a data access layer?
- From: swansnow
- How do you create a data access layer?
- Prev by Date: Re: OOP style
- Next by Date: Re: terminal services compatibility
- Previous by thread: Re: How do you create a data access layer?
- Next by thread: HP 4100 duplexer
- Index(es):
Relevant Pages
|