Jump to content
baeckerg

Database connection as parameter in class creation

Recommended Posts

Hi,

 

in a program I have a central FireDAC connection to my database in a datamodule. I intend to have all record related activities (CRUD) in a class that holds the record. My questions are:

 

1) is there any penalty if I pass the connection (as var parameter?) to the constructor of the class?

2) is this the best way to separate the data from the UI?

 

many thanks in advance

Gernot

Share this post


Link to post
1 hour ago, baeckerg said:

Hi,

 

in a program I have a central FireDAC connection to my database in a datamodule. I intend to have all record related activities (CRUD) in a class that holds the record. My questions are:

 

1) is there any penalty if I pass the connection (as var parameter?) to the constructor of the class?

2) is this the best way to separate the data from the UI?

 

many thanks in advance

Gernot

For 1: If you like to set up the connection at design-time in a datamodule the approach is OK, and you do not need to pass the connection object as a Var parameter. Objects are reference types, so you pass a reference to the connection object, and that can even be done as const, since you can work with the object's properties or pass the reference on this way as well. Just do not free the connection object in the "record class", it belongs to the datamodule and will be freed by it when the datamodule is destroyed.

 

For 2: "Best" is hard to define in this context. For a complete separation of the UI from the data it works with you need a bit more than just keeping connection and query/table objects out of the forms/frames and business logic code out of event handlers there. But writing a model-view-presenter framework or a full-blown object-relational mapper (ORM) framework from scratch is a lot of work. For smaller projects it may not pay to go to these length, especially if you only aim for one platform with a desktop client.

 

Share this post


Link to post

Thanks for the feedback and the valuable input

 

Free will be part of the datamodule when it is destroyed 🙂

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×