How to make a stateless ASP component (RPM)

Source: Internet
Author: User
Tags array arrays variables
How to make an ASP component without status


When we use components in the Web or other related stateless applications, we will lose all references to the component when we run the script. Of course, you can simply save a component's references in a session variable, but this is a waste of resources. A smarter way is to save some information about a component with a session variable or an implicit form label. When you recreate an instance of a component, you attempt to recover the state of the component with information saved in the session variable. But the downside of both approaches is that it is too expensive from a resource standpoint and even difficult to achieve.

Lucky to have the good news. We all know that the whole premise of sustainability is the ability to restore a variable to such a state that the component knows where it is in the last example. This helps the component now to do what it assumes to do. A typical example of this scenario is the paging of information on the Web page. When the user clicks Page DOWN, we need to know where we are on the previous page. Do not keep variables in memory (such as session variables), if we can save them in the file, and so on later when it is necessary to retrieve is not very good? It's easy to save data in the underlying data form (strings and numbers). What happens with objects and arrays? Objects and arrays are essentially binary data streams in memory. If we can read this data we can write it into a file for later retrieval. There's a good note about objects. Although the array is in a different situation. First you can't use an array to do component properties (VB6.0). You can use them as variables, but you'll run into roadblocks later. Luckily we found a workspace for this situation.

You can save all or part of a property in a PropertyBag object. The PropertyBag object controls the information that can be saved and restored during an object invocation. The content property of the PropertyBag object provides you with any binary data streams in the group. It's up to you to write these binary data streams into a file for later retrieval.

Instance:

For example, you have a class mycomp.clsmydept, which has two attributes mydepartment$ and myemployees (an ADO Recordset object).

The following is the code for this class module:

Option Explicit
Option Compare Text

Public mydepartment as String
Public myemployees as ADODB. Recordset

Dim Objbag as New propertybag

Private Sub class_initproperties ()
Set myemployees = New ADODB. Recordset
MyEmployees.Fields.Append "EmpName", adVarChar, 30
MyEmployees.Fields.Append "Empsal", adcurrency
Myemployees.open
End Sub

Public Sub savemyproperties ()
Dim intfile%, Bytrec () as Byte
Objbag.writeproperty "MyDepartment", mydepartment
Objbag.writeproperty "MyEmployees", myemployees
' Save this data in a file for later retrieval
Intfile = FreeFile
If Dir ("C:\MyData.txt", vbnormal) = "" Then
Else
Kill "C:\MyData.txt"
End If
Open "C:\MyData.txt" for Binary Access Write as #intFile
Bytrec = objbag.contents
Put #intFile, Bytrec.
Close #intFile
End Sub

Public Sub restoremyproperties ()
Dim intfile%, Bytrec () as Byte
' Read the saved data from the file.
ReDim Bytrec (FileLen ("C:\MyData.txt"))
Intfile = FreeFile
Open "C:\MyData.txt" for Binary Access Read as #intFile
Get #intFile, Bytrec
Objbag.contents = Bytrec
Close #intFile
' Propertbag restored. Lets restore the properties now.
MyDepartment = Objbag.readproperty ("mydepartment")
Set myemployees = Objbag.readproperty ("MyEmployees")
End Sub


Saving properties in a customer application

Private Sub Command1_Click ()
Dim objdept as New mycomp.clsmydept
Objdept.mydepartment = "the"
' ADD one employee
ObjDept.MyEmployees.AddNew
objdept.myemployees! EmpName = "Harry"
objdept.myemployees! Empsal = 2500
ObjDept.MyEmployees.Update
' ADD second employee
ObjDept.MyEmployees.AddNew
objdept.myemployees! EmpName = "Potter"
objdept.myemployees! Empsal = 3000
ObjDept.MyEmployees.Update
' Save the ' properties by calling the "method" from our component
Objdept.savemyproperties
Set objdept = Nothing
End Sub


Retrieve the Saved property

Private Sub Command2_Click ()
Dim objdept as New mycomp.clsmydept
' Restore properties by calling ' method from our component
Objdept.restoremyproperties
' Lets-what is restored
Debug.Print Objdept.mydepartment ' 'll Print

ObjDept.MyEmployees.MoveFirst
Debug.Print "" & objdept.myemployees! EmpName ' 'll print Harry
ObjDept.MyEmployees.MoveNext
Debug.Print "" & objdept.myemployees! EmpName ' 'll print Potter
Set objdept = Nothing
End Sub

Before you run this cool piece in your application, you must understand its limitations. The time to save depends on the size of the property and the data type. Note that most of the time is spent in ReadProperty and WriteProperty. The reason is simple, when we deal with structured data like the ADO recordset, the process is not as simple as copying a byte stream. The data must also be explained.

I observed the following when I saved a recordset:

1, save 100,000 rows each column has 25 characters of the recordset in 50 seconds.
2. It took 20 seconds to retrieve the same data.

There is a better way to save a Recordset object. They have their own method of save. Saving 100,000 recordsets in the Save method takes only 6 seconds. Retrieving a saved Recordset object with the Open method, retrieving 100,000 rows from the saved Recordset object in 20 seconds (as in the PropertyBag method)

Choose to use an array

An ongoing ADO recordset provides a good way to use arrays in your application. Think about it, if you use it you have all the easy ways to use ADO, like Find,sort,filter and so on. In fact, if you look closely, you will find that we have implemented an array of employee records with a continuous recordset in the above example. Purely theoretical people may object because you have to include ADO in your design, which will increase the amount of storage you have to distribute the disk.

Summary

The sustainability of components in a stateless application of a Web class is a very important feature. Use the PropertyBag object to save and retrieve properties. For Recordset objects, use the Save and Open methods of ADO to save and retrieve data, respectively. Use the example above as a touch board, and add variable file names to each instance to make your own components.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.