Page

16.3.1- Writing a Component that Accesses a Data Store

  by NT Community Manager.
Last Updated  by Jim Minatel.  

PublicCategorized as 16. Building Script Components for ASP.

Not tagged.
<< 16.3.0- Components and Data AccessChapter1616.3.2- Setting up the Database >>

Writing a Component that Accesses a Data Store

In Chapters 12–15 we saw plenty of ASP pages that used ADO functionality to access a database or other data store, either to fetch data for use in our ASP page or to write data from the page back to the data store. And in this chapter, we used the WSC wizard to write a simple component that contains the logic for calculating the price for a piece of carpet – and we used that example in an ASP page too.

 

Of course, we can use these two techniques together, and write highly functional pages that use data access and also take advantage of functionality encapsulated into our own home-made components.

So let's look at another example that uses both techniques. We'll write a little example that simulates some of the workings of a bank account. We'll have a database which contains the current details of all the existing accounts (including account numbers and account balances), and we'll use the WSC to write a component called BankAccount.wsc that represents the bank account itself. The component will expose two properties:

 

Property name

Type

Purpose

AccountID

String, Read/Write

Contains the ID of the account.

Balance

Currency, Read-Only

Read-only balance of the account.

 

We could add lots of other properties, such as AccountType, AccountHolder, AccountHolderAddress – but we'll avoid these properties here for the sake of simplicity.

 

The component will also expose two methods:

 

Method name

Parameters

Purpose

AddMoney

CheckNumber, CheckAmt

Credits amount of check to target account, and places an entry in the check register.

 

Once again, recall what we mean when we say that the component exposes only these methods and properties. It means that, regardless of the internal workings of the component, any application that uses this component can only use the methods and properties listed in the tables above.

 

We'll also write a couple of ASP pages that use this component, to allow the user (in this case, a bank teller at the local branch of the bank) to transfer cash from one account to another. The ASP page will act simply as the user interface, and the component will do most of the work of transferring money around. Therefore, when transferring the money it is the component (not the ASP page) that will need to be able to retrieve data from the data store, and to write data to the data store – so it is the component's code that will use the ADO objects that we see in this example. In other words, we have a very simple three-tiered structure for the process of making a cash transfer:

 543636_pg749.jpg

There's a reason for choosing to demonstrate this technique using the 'cash transfer' scenario. As we'll see in the next chapter , there's more to the cash transfer than we meet in this example – we will need to make it more robust by exploiting a technique known as transaction processing. However, we'll work up to that; and ultimately, at the end of the next chapter, we'll pull together components and transactions to build a scalable robust version. First, in this example, we'll lay down some foundations by examining a less robust model.

<< 16.3.0- Components and Data AccessChapter1616.3.2- Setting up the Database >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |