Implementation

[Previous] [Next]

As the class diagram in the "Object Model" section of this chapter clearly illustrates, the Repository design pattern comprises two components: a Repository and a PersistableObject. The PersistableObject interface defines the methods required for a class to read and write data into and out of some specified type of package (such as a byte array). A concrete class that implements the PersistableObject interface will use the properties and methods of the interface as the means by which to read and write its state into and out of the package defined by the interface. This in effect makes a class persistable. The Repository interface defines methods for storing stuff (an object's state) in a package to a data store, retrieving the stuff from a data store, reconstructing a persistable object, putting the stuff back into the specific type of package, and initiating the persistable object's read process on the package. Here's one possible implementation.

Within an ActiveX component project, instead of defining my own PersistableObject interface I've decided to take advantage of the class persistence features available in Visual Basic 6. This approach requires less code to be written, it's easy to implement, it's well documented in the online help, and since class persistence is an inherent part of the product, this approach is accessible to all Visual Basic programmers. To this end, you could build a single repository to persist all Visual Basic persistable objects.

Class persistence is available only to public creatable classes in an ActiveX component project. To turn class persistence on in an ActiveX component project set the Instancing property to a public creatable value (such as MultiUse in an ActiveX DLL) and set the Persistable property value to Persistable. Doing so will add three new class event handlers to the persistable class module: InitProperties, ReadProperties, and WriteProperties. (See Figure 6-3 in Chapter 6.) In this implementation only the last two are used. Both the ReadProperties and WriteProperties events pass in a PropertyBag object parameter. As the name suggests, PropertyBag is conceptually a bag containing the property values of a persistable object. Property values are stored as a byte array inside the bag. The ReadProperties event handler implements the code to read the data out of the PropertyBag and into the persistable object's member variables that represent the properties (or state) of the object. The WriteProperties event handler implements the code to write a persistable object's property values (or state) into the PropertyBag. The byte array that's in the PropertyBag is what's actually stored in the repository. When you implement both event handlers, your job is done as the persistable object designer. It isn't your concern how the persistent event handlers are invoked.

NOTE
The Visual Basic class persistence features were also a major contributor to the Object By Value design pattern implementation. Refer to Chapter 6 for a more detailed explanation of that design pattern.

Next, add a class module to the project that defines a Repository interface. Set its Instancing property value to PublicNotCreatable since by definition an interface contains no implementation of its own. (Refer to Chapter 2 for in-depth coverage of interface implementations.) Finally, add a class module (ConcreteRepository) that implements the Repository interface. The concrete repository class will contain the code to invoke the ReadProperties and WriteProperties event handlers in a persistable object in order to store the object's state to a data store or to reincarnate an object with the state from a data store. The "Sample Application" section of this chapter includes code extracts that represent all participants of the Repository design pattern.



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net