Home | Overview | How Do I | Tutorial
This article explains when to use document objects and serialization for file-based input/output (I/O) and when other I/O techniques are appropriate — because the application reads and writes data on a per-transaction basis, as in database applications. If you don’t use serialization, you also won’t need the File Open, Save, and Save As commands. Topics covered include:
Whether you use file-based I/O or not depends on how you respond to the questions in the following decision tree, which begins with the question:
Does the primary data in your application reside in a disk file?
Does the application read the whole file into memory on File Open and write the whole file back to disk on File Save?
Use CDocument serialization.
The MFC Advanced Concepts sample CHKBOOK is an example of this case. You update the file on a per-transaction basis and don’t need CDocument serialization.
Does the data reside in an ODBC data source?
Use MFC’s database support. The standard MFC implementation for this case includes a CDocument object that stores a CDatabase object, as discussed in the article What Is the MFC Database Programming Model?. The application might also read and write an auxiliary file — the purpose of the AppWizard “both a database view and file support” option. In this case, you’d use serialization for the auxiliary file.
Examples of this case: the data resides in a non-ODBC DBMS; the data is read via some other mechanism, such as OLE or DDE.
In such cases, you won’t use serialization, and your application won’t have Open and Save menu items. You might still want to use a CDocument as a “home base,” just as an MFC ODBC application uses the document to store CRecordset objects. But you won’t use the framework’s default File Open/Save document serialization.
To support the Open, Save, and Save As commands on the File menu, the framework provides document serialization. Serialization reads and writes data, including objects derived from class CObject, to permanent storage, normally a disk file. Serialization is easy to use and serves many of your needs, but it may be inappropriate in many data-access applications. Data-access applications typically update data on a “per-transaction” basis. They update the records affected by the transaction rather than reading and writing a whole data file at once.
For general information about serialization, see the article Serialization (Object Persistence).
If you create an MFC database application and don’t use serialization, how should you interpret the Open, Close, Save, and Save As commands on the File menu? While there are no style guidelines for this question, here are a few suggestions:
AppWizard supports creating an application with no document-related File menu commands. Select the “A database view, without file support” option on the database options page.
To interpret a File menu command in a special way, you must override one or more command handlers, mostly in your CWinApp-derived class. For example, if you completely override OnFileOpen (which implements the ID_FILE_OPEN command) to mean “open database”: