Monday, 4 January 2010

Breaking away the data layer

I've decided, somewhat randomly, to start by breaking away the data layer. Time will tell if this is the best approach. It just seems the most logical to me as I'm looking at the form with data access components cluttering it up. I'll move all of these onto a new datamodule and refactor the form to use the data module. The datamodule will be the starting point for the model part of the MVC.

Steps:
  1. Create a new data module unit
  2. Remove datamodule from list of auto-create forms in project options | Forms
  3. Remove global variable in the datamodule unit
  4. Select all the data components including data source components
  5. Copy the selected components onto the new data module
  6. Add a private member reference to the datamodule on the form
  7. Add create datamodule on form create and free datamodule on form destroy
  8. Save and build
  9. Remove data components from form.
  10. Save and build - this will give errors for each instance where the data component can now non longer be found.
  11. Go through this list of errors, correcting the reference to the components on the datamodule.
  12. Relink the data aware controls to the relevant datasource components on the data module.
  13. Relink the data component event handlers to the components. I exposed the event handlers on the form as an interface so that the data module could delegate the handling back to the form.
  14. Save and Build.
Next, I need to convert the data-aware controls into non-data-aware controls. This will allow me to separate and take control of the data-capture and data-posting. I don't want the data-aware controls to post directly to the edit buffer of the query component that is used for posting new records. Instead I want to build an object representing the record(s) and then send these to the datamodule to post.

First off I'll convert a TDBEdit to TEdit. This should be done in two parts. The first part is to re-type the actual control. The second part will be replace functionality that would have been done by the data-aware events.

Retype steps:
  1. Make a note of the field that the data-aware control uses.
  2. View Form as Text
  3. Find the declaration of the TDBEdit to convert
  4. Change the type to TEdit.
  5. View form as Form
  6. Save. At this point the IDE will display messages saying that some of the additional property values of TDBEdit will be lost if you click 'Ignore'. This is what we want, so click 'Ignore All'.
Replace lost data-aware functionality steps:
  1. On Model change: enable/disable and populate control with current data field value
  2. On Post: populate insert/update query buffer data field value with the editor value.
Just be a little clearer here: On Model Change means any event on the data module that should trigger the form to be refreshed. On Post means committing the data on the form to either an update or insert.

No comments:

Post a Comment