Generated ModelConnector Objects
As you build out more and more database-driven
Forms and
Panels, you'll notice
that you may spend quite a bit of time coding the same type of Control
definition, setup and data binding procedures over and over again. This becomes
especially tedious when you are talking about modifying objects with a large
number of fields.
An important part of the "Controller" functionality of the MVC architecture of QCubed is the ModelConnector.
It connects specific screen controls to the fields in a database table,
and includes the code to create the controls, populate them with data from the table, and save the user's changes
back to the database.
ModelConnectors include a code-generated base, and also a stub sub-class to allow you to override parts of the
ModelConnector for your own customizations.
For each field in a class, you can have the ModelConnector return for you a data bound
and setup QControl for editing, or a Label just for viewing. But because these ModelConnectors
are simply returning standard QControls, you can then modify them (stylizing, adding events, etc.) as you normally would
any other control.
As you request controls from the ModelConnector, it keeps track of which controls you have request, so that you can call
SavePerson() on the ModelConnector, and it will go through any controls
created thus far and bind the data for those controls back to the Person object. If your application needs to
scroll through a group of objects, you can use the Load method to load new data for a database record
into all of your requested controls automatically.
The ModelConnector Designer
The code-generated controls includes some basic options which QCubed reads from the database. For example, if your database field
is not allowed to be null, the code-generated control will automatically have the Required
attribute set to true. To further customize what will be generated in the base version of the ModelConnector,
you can use the ModelConnector Designer. To use the designer, do the following:
- Define the QCUBED_DESIGN_MODE constant in your configuration file.
- Right click on the Control you want to modify in your browser.
- Set your options, click the Save button, and the code-generate again.
You can set a large variety of options from this dialog (try it now by right clicking on a field in the example to the right), including
the ability to change the type of control generated for a database field.
Hover over any option in the designer to popup a description of that option.
The options for the ModelConnector Designer are saved in the "codegen_options.json" file in your configuration directory.
If you make a change that you cannot fix from the ModelConnector Designer (like hiding a control that you want to show),
you can always directly edit that file to recover from your mistake.
The Example
The example shows some basic controls so that you can try out the ModelConnector Designer. Right click
on any of the fields or the checkboxes to bring up a dialog that will let you specify the various options for
the codegen process.
Finally, note that since the ModelConnectors encapsulate all the functionality for a given
instance of a given object, and since it is able to keep track of and maintain its own
set of controls, you can easily have multiple ModelConnectors on any Form or Panel,
view or edit multiple objects of any class at the same time.