The CDaoEnrolDoc Document Class

In ClassView, you can see that AppWizard created a class, CDaoEnrolDoc, derived from CDocument.

Suggested Reading

To view the document class

What is the document’s role in a database application? In most other applications, the document stores data and serializes it to a file on disk. Often the application reads the whole file into memory at once and writes it back to disk as a whole. In a database application, however, the data is stored in the database, and the user usually views the data as records. Such an application doesn’t need a file.

A document in a database application, then, isn’t normally used for its serialization support. So why does DaoEnrol have a document class?

The following code, at the beginning of DenrlDoc.h, reveals that the role of the document class in DaoEnrol is to own the recordset.

class CDaoEnrolDoc : public CDocument
{
   ...
   // Attributes
   public:
      CSectionSet m_sectionSet;
   ...
};

The recordset object, m_sectionSet, is embedded in the document object. Therefore, the recordset object is automatically constructed when the document object is constructed, and automatically deleted when the document object is deleted.

The document class can own any number of recordset objects in this way.  For example, Step 4 of DaoEnrol adds a second form and corresponding recordset; the document embeds this second recordset.

In a sense, then, the document class is a proxy for the database. This approach isn’t strictly necessary, but if you (or AppWizard) design your database application to use the document class this way, you can better take advantage of the framework’s document/view architecture. For example, if you have multiple views (forms) simultaneously showing some of the contents of the database, you can take advantage of the CDocument::UpdateAllViews mechanism to conveniently notify all views about an update that might have been initiated in one of the views.

If you look at the menu resource that AppWizard created when you chose the option Database view without file support, you’ll see that there are no New, Open, Save, or Save As commands on the File menu. The File menu has only the Print, Print Preview, Print Setup, and Exit commands. If you had chosen Database view with file support, AppWizard would have supplied the missing File menu commands.

To view the DaoEnrol menu resource

  1. From ResourceView, expand the DaoEnrol folder.

    This displays the resource browser, which shows the resources associated with a project.

  2. In the resource browser, expand the Menu folder.

  3. Double-click IDR_MAINFRAME.

    The Menu editor opens, displaying the default menu that AppWizard created for the DaoEnrol application.

  4. Click the File menu item to view its structure. Notice the absence of New, Open, Save, and Save As.

  5. Close the Menu editor when you’re finished.

Note   If you choose the option Database view with file support in AppWizard, the document class plays two roles. First, it serves as a proxy for the database. Second, it represents the file that is opened and saved via the New, Open, Save, and Save As commands on the File menu.