⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08.htm

📁 好书《C++ Builder高级编程技术》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	is tolerable, but I 
still feel the pinch when working at that low a resolution. 
<HR>


</BLOCKQUOTE>

<P>Wire the <TT>TDataSource</TT> to the <TT>TTable</TT> object, and set the <TT>DatabaseName</TT>
of the <TT>TTable</TT> object to <TT>BCDEMOS</TT> and the 
<TT>TableName</TT> to <TT>BioLife</TT>.
Set the <TT>Active</TT> property of the <TT>TTable</TT> object to <TT>True</TT>.</P>
<P>Right-click the <TT>TTable</TT> object and bring up the Fields Editor. Right-click
the Fields Editor, and bring up the 
AddFields dialog. Make sure all the fields in
the dialog are selected, which is the default behavior for the tool. Click the OK
button.</P>
<P>The Fields Editor now contains a list of all the fields in the <TT>BioLife</TT>
table. To add these fields 
to your form, simply click one or more fields, hold the
left mouse button down, and drag the fields onto the form. For instance, select the
Graphics field, making sure that it is the only one highlighted. Now drag it onto
the form. When you let go of 
the left mouse button, the Graphics field will automatically
display itself in a <TT>TDBImage</TT> component. Drag over several other fields,
and create a form that looks something like the image in Figure 8.7.<BR>
<BR>
<A NAME="Heading19"></A><A 
HREF="08ebu07.jpg" tppabs="http://pbs.mcp.com/ebooks/0672310228/art/08/08ebu07.jpg">FIGURE 8.7.</A><FONT COLOR="#000077">
</FONT><I>Some of the fields of the <TT>BioLife</TT> table arranged in visual controls
on a <TT>TForm</TT> object.</I></P>
<P>If you want to drag multiple fields over at the same time, 
perform a multiselect
operation in the Fields Editor, just as you would in a list box. Now drag all the
fields over to the main form at once. Most likely they will scroll on past the bottom
of your form when you insert them, but you can fix this 
easily enough by aligning
them as you like with the mouse.</P>
<P>The <TT>TDBImage</TT> component, where the picture of the fish is displayed, may
be in some disarray when you first insert the components on the form. To straighten
things out, select 
the <TT>TDBImage</TT> component, go to the Object Inspector, and
set the <TT>Stretch</TT> property to <TT>True</TT>.</P>
<P>A sample program illustrating these principles ships on the CD-ROM that accompanies
this book. It is called SimpleDataModule.

<H3><A NAME="Heading20"></A><FONT COLOR="#000077">The Purpose of TDataModule</FONT></H3>
<P>Now that you know how to use the <TT>TDataModule</TT>, let me add a few words
on its significance. This component's primary purpose is to provide a place where

you can define the means for accessing a set of tables. However, it is also a place
to put business rules, and a place to create reusable means of accessing data.</P>
<P>Client/server database programmers often want to put all the rules for accessing

data on the server side. Indeed, BCB supports this paradigm, and you can use stored
procedures, views, and other advanced database technologies to whatever degree you
want when accessing SQL databases. However, you also have the ability to define a

set of rules that live on the client side, inside a <TT>TDataModule</TT>. You can
then use the Object Repository from the File | New menu choice to store this form
in a place where it can be reused by multiple programmers. To put a form in the Object

Repository, right-click it and chose Add to Repository from the menu. I discuss the
Object Repository in more depth later in this chapter.</P>
<P>There is no simple way to decide when it is best to put rules on the server side
or when it is best to 
put them inside a <TT>TDataModule</TT>. Often the best solution
is to use a combination of the two techniques. Put things on the server side when
that is simplest, and store things on the client side when you think the power of
C++ and its strong 
debuggers will be useful to you.</P>
<P>There is considerable complexity in the whole set of related subjects that involve
designing databases, creating business rules, and creating metadata such as stored
procedures on a server. Many of these topics 
will be explored in considerable depth
in the next few chapters. However, a thorough examination of these topics requires
considerably more scope than I have in this book.
<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Conventions Regarding the Use 
of
TDataModules</FONT></H3>
<P>When working with database code, I often prefer to put my <TT>TTable</TT>, <TT>TQuery</TT>,
and <TT>TDatasource</TT> objects in a <TT>TDataModule</TT>, rather than placing them
on a form. In other words, I think there 
are architectual reasons for using <TT>TDataModules</TT>
rather than placing tables directly on a form.</P>
<P>There are several interrelated advantages to this scheme, most of which have to
do with proper design issues. In particular, the scheme 
outlined previously enables
me to do the following:

<UL>
	<LI>Separate my database code and my GUI code. In an ideal situation, all the front-end
	code is in my form, and all the database code is in the <TT>TDataModule</TT>.
	<P>
	<LI>Protect the 
data in database code. It can sometimes be bad practice to directly
	access any of the methods or fields of an object from inside another object. Instead,
	you can present a series of public properties and methods as the interface for an
	object. 
There is, however, nothing wrong with accessing the properties of a <TT>TTable</TT>
	and <TT>TQuery</TT> object through pointer notation. In other words, the rules of
	aggregation state that it is acceptable and safe to write <TT>DMod-&gt;Table1</TT>,
	
rather than trying to hide <TT>Table1</TT> behind methods of <TT>TDMod</TT> that
	wrap the object and hide it from sight.
	<P>
	<LI>Promote reuse. By isolating the database code in the <TT>TDataModule</TT>, and
	by protecting the data associated with 
my database code, I am creating a robust object
	that can be reused in multiple programs. In particular, several programs may want
	to access data in the same manner. By using a <TT>TDataModule</TT> correctly, you
	can encapsulate your data, and its 
associated rules, inside a single related object
	that can be reused in multiple programs. This is one way to create a set of reusable
	business rules that can be shared by multiple members of your team, or by multiple
	programmers.
</UL>

<P>I will 
often use the constructor of a <TT>TDataModule</TT> or its <TT>OnCreate</TT>
event to open up the tables or data modules used in a project:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall TDModBioLife::DModBioLifeCreate(TObject *Sender)

{

  
BioLifeTable-&gt;Open();

}

</FONT></PRE>
<P>This is the proper and ideal way to do things. I want to stress, however, that
it is also correct from an OOP standpoint to access the same table from inside your
main form through the <TT>scoping</TT> 
operator:</P>
<PRE><FONT COLOR="#0066FF">void __fastcall TForm1::Button1OnClick(Tobject *Sender)

{

  DMod-&gt;BioLifeTable-&gt;Open();

}

</FONT></PRE>
<P>You can use either technique, depending on your needs. Most of the time, you will
use the 
second technique shown here, the one that uses <TT>scoping</TT> operators.
There are, however, some good arguments in favor of using the first method. In particular,
the first technique is pure from an OOP point of view in that it completely hides
the 
details of what goes on in the <TT>TDMod</TT> object.</P>
<P>One problem with the rigorous technique illustrated by the first example is that
it can add complexity to simple programs. If you only need to access one table, and
only need one 
<TT>TDataSource</TT> object, even the simple act of creating a data
module can seem like overkill. Going even further and insisting that the code for
manipulating the table also reside in the <TT>TDataModule</TT> can then seem almost
absurdly 
roundabout and abstruse.</P>
<P>In the type of simple database projects that you will see in this chapter, it
is possible to forgo the use of data modules altogether and to instead place the
<TT>TTable</TT> and <TT>TDataSource</TT> objects directly on 
your main form. However,
I will generally use a <TT>TDataModule</TT> even in such simple cases simply because
it is the best way to architect an application. The point is to get in the habit
of doing things the right way, because ultimately, in large 
scale projects, decisions
such as this do matter.

<DL>
	<DT></DT>
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Data modules can be used not only
	for data controls, but for all nonvisual controls. You can, for 
instance, place a
	<TT>TMenu</TT> object on a data module, and then add the data module's header file
	to your main form, thereby accessing the <TT>TMenu</TT> object through the Object
	Inspector. The problem with this technique, of course, is that 
the methods you want
	to access from the menu are not always going to be located in the data module. Another
	issue is that your form and the data module will then be bound together, at least
	to some degree. 
<HR>


</BLOCKQUOTE>

<P>Remember that 
one of the key features of data modules is that they provide a place
to store a set of business rules. You can create tables and queries, link them together,
and use code to define rules regarding the way they work. To replicate these rules
in 
multiple projects, simply reuse the data module that contains them.
<H3><A NAME="Heading23"></A><FONT COLOR="#000077">The Object Repository</FONT></H3>
<P>In the last section, I said that data modules can help promote reuse of code.
BCB has a specific 
mechanism called an Object Repository that can help with this
process. In particular, Object Repositories are a place where you can store data
modules and forms so that they can be reused in multiple applications. If you define
a set of business rules 
in a data module, you can save it to the Object Repository
and reuse it in multiple projects. This helps you propagate the rules and promote
conformity to them across a wide range of projects.</P>
<P>The simplest way to introduce you to the Object 
Repository is to just lead you
step-by-step through the process of using it. After you have seen how it works, I
will take a moment to explain its significance.</P>
<P>Save the program you created in the last section as follows:

<DL>
	<DD><B>1.</B> 
Save <TT>Unit1</TT> as <TT>Main.cpp</TT>.<BR>
	<BR>
	<B>2.</B> Save <TT>Unit2</TT> as <TT>DModBiolife.cpp</TT>.<BR>
	<BR>
	<B>3.</B> Save the project file as <TT>Biolifeapp</TT>.<BR>
	<BR>
	<B>4.</B> Rename the table and data source to 
<TT>BioLifeTable</TT> and <TT>BioLifeSource</TT>.<BR>
	<BR>
	<B>5.</B> Select the data module and use the Object Inspector to rename it from <TT>DataModule2</TT>
	to <TT>DModBioLife</TT>.
</DL>

<P>Right-click the data module and select Add To 
Repository. Fill in the Add To Repository
dialog by setting the Title, Description, and Author fields as you see fit. In the
Page drop-down combo, select Data modules. Use the Browse button to select an icon
from the <TT>..BCB\images\icon</TT> 
subdirectory, or from any place else where you
might have some icons stored.</P>
<P>Start a new project. Choose File | New. This time, instead of choosing the Data
module component from the New page, select the Data modules page and choose the 
<TT>DModBioLife</TT>
component that you just finished creating. When it appears on the screen, you will
see that it contains a <TT>TTable</TT> and <TT>TDataSource</TT> component. The components
are wired together, and the <TT>TTable</TT> object is set 
to the <TT>BioLife</TT>
table with its <TT>Active</TT> property set to <TT>True</TT>.</P>
<P>To access this table from <TT>Form1</TT>, you must first employ the Include Unit
Header menu option from the File menu to add <TT>Unit2</TT> to the uses 
clause in
<TT>Unit1</TT>. Go to the DataControls page of the Component Palette, and drop down
a <TT>TDBGrid</TT> object on <TT>Form1</TT>. In the Object Inspector, drop down the
<TT>DataSource</TT> property of the <TT>TDBGrid</TT> object, and you will 
see the
<TT>TDataSource</TT> object from the <TT>DModBioLife</TT> module listed. Select this
item, and the grid will automatically fill up with data.</P>
<P>If you drop down a <TT>TDBEdit</TT> control instead of a <TT>TDBGrid</TT> control,
you proceed 
the same way, except that you will need to fill in not only the <TT>DataSource</TT>
property in the Object Inspector, but also the <TT>DataField</TT> property. There
is no need to type information into the <TT>DataField</TT> property, because it will

automatically contain a list of the available fields in the <TT>BioLife</TT> table.</P>
<P>The true significance of the Object Repository is only hinted at by this example.
The importance of this tool is made more obvious if you have six or seven 
tables
dropped onto a data module. You might then define several relationships between the
tables and add other related code. For instance, you might have some one-to-many
relationships established, possibly a many-to-many relationship established, 
and
you might have several filters, lookups, and several calculated fields defined.</P>
<P>Altogether, a data module of this type might encapsulate several sets of business
rules defining exactly how tables should be accessed and how they relate to 
each
other. The ability to save all this work in the repository, and to then automatically
reuse it in multiple projects, is extremely valuable. I am, however, getting ahead
of myself. Discussions of filters, lookups, calculated fields, and other 
database
issues occur in various places over the next few chapters.
<H3><A NAME="Heading24"></A><FONT COLOR="#000077">The Database Explorer</FONT></H3>
<P>In addition to the data module, another key tool to use when working with databases
is the 
Database Explorer. You can access the Database Explorer by choosing the Database
| Explore menu item. The Explorer is a stand-alone executable, so you can also access
it from the Windows Start button on the taskbar. You can use the Explorer even if

BCB is not running, but BCB and the Explorer work best in conjunction with one another.</P>
<P>Once you have loaded the Explorer, make sure you have selected the Databases page
and not the Dictionary page. Click the <TT>BCDEMOS</TT> node to expose the 
<TT>Tables</TT>
node. Now click the little plus sign before the <TT>Tables</TT> node. A list of all
the tables in the database will appear. Select the <TT>BioLife</TT> table and choose

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -