home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "DemoVBObjectFramework"
- Option Explicit
-
- ' These Collections are used by other Forms and
- ' .BASs
- Public publicCompany As New Company
- Public CompanyDatabase As Database
- Public pubPersons As VBOFCollection
- Public pubStates As VBOFCollection
- Public pubGenderCodes As VBOFCollection
- Public pubMaritalStatusCodes As VBOFCollection
-
- ' This is the only instance of VBOFObjectManager
- ' which exists throughout the application. It is
- ' shared by the entire application
- Private ObjectManager As New VBOFObjectManager
-
- Private pvtPerson As Person
- Private pvtAddress As Address
- Private pvtPhone As Phone
-
- ' In the non-visual BOM script herein, procedure
- ' "CreateTestData", these named Person objects
- ' are used simply for clarity. The Persons also
- ' appear in the pubPersons Collection which is
- ' used across the application.
- Private George As Person
- Private Martha As Person
- Private Abe As Person
- Private Mary As Person
- Private BillClinton As Person
- Private HillaryClinton As Person
- Private ChelseaClinton As Person
-
- Private Sub PopulateGenderCodes()
-
- Dim tempGenderObject As New Gender
-
- ' instantiate pvtGenderCodes as a VBOFCollection
- Set pubGenderCodes = _
- ObjectManager. _
- NewVBOFCollection
-
- Set pubGenderCodes = _
- pubGenderCodes.PopulateCollection( _
- Database:=CompanyDatabase, _
- SQL:="SELECT * FROM " & _
- tempGenderObject.ObjectDataSource, _
- Sample:=tempGenderObject)
-
- pubGenderCodes.pvtCloseRecordSet
-
- End Sub
-
- Private Sub PopulateMaritalStatusCodes()
-
- Dim tempMaritalStatusObject As New MaritalStatus
-
- ' instantiate pvtMaritalStatusCodes as a VBOFCollection
- Set pubMaritalStatusCodes = _
- ObjectManager. _
- NewVBOFCollection
-
- Set pubMaritalStatusCodes = _
- pubMaritalStatusCodes.PopulateCollection( _
- Database:=CompanyDatabase, _
- SQL:="SELECT * FROM " & _
- tempMaritalStatusObject.ObjectDataSource, _
- Sample:=tempMaritalStatusObject)
-
- pubMaritalStatusCodes.pvtCloseRecordSet
-
- End Sub
-
-
- Private Sub PopulateStateCodes()
-
- Dim tempStateObject As New State
-
- ' instantiate pvtStates as a VBOFCollection
- Set pubStates = _
- ObjectManager. _
- NewVBOFCollection
-
- Set pubStates = _
- pubStates.PopulateCollection( _
- Database:=CompanyDatabase, _
- SQL:="SELECT * FROM " & _
- tempStateObject.ObjectDataSource, _
- Sample:=tempStateObject)
-
- pubStates.pvtCloseRecordSet
-
- End Sub
-
-
- Sub CreateObjectManager()
- ' instantiate the VBOFObjectManager
-
- Set ObjectManager = _
- New VBOFObjectManager
-
- With ObjectManager
- ' play with these to your satisfaction
- ' (this is a subset of the available parameters)
- ' .AutoDeleteOrphans = True
- ' .SynchronousCommit = True
- #If NoDebugMode = False Then
- ' .DebugMode = True
- #End If
- End With
-
- Set ObjectManager.Database = _
- CompanyDatabase
- Set ObjectManager.Workspace = _
- Workspaces(0)
-
- End Sub
-
-
- Sub CreatePersonsCollection()
-
- Dim tempNewPerson As New Person
-
- ' instantiate pubPersons as a VBOFCollection
- Set pubPersons = _
- ObjectManager. _
- NewVBOFCollection
-
- pubPersons.SetDatabaseParameters _
- OrderByClause:="LastName ASC, FirstName ASC"
- ' play with these to your satisfaction
- ' (this is a subset of the available parameters)
- ' Database:=CompanyDatabase, _
- ' ANSISQL:=True, _
-
- pubPersons.PopulateCollection _
- Sample:=tempNewPerson, _
- Parent:=publicCompany
-
- End Sub
-
-
- Sub CreateTestData()
- ' This is essentially a non-visual BOM Test Script.
- ' While developing the non-visual BOM, a script
- ' such as this should be used to verify objects
- ' and methods.
- ' The test script should (obviously) run withouth
- ' the procedurally-oriented GUI or DataControl
- ' objects. It should also be able to run in a
- ' non-database mode, as well. That is, objects
- ' should be instantiated, populated and work
- ' properly without concern for the underlying
- ' database. When the object behavior has
- ' stabilized, the underlying database can be
- ' easily applied. The VBOF fully supports this
- ' BOM development technique.
- ' In general, there should be at least one usage of
- ' each object and method represented in the test
- ' script. Under some circumstances, there may be
- ' the need for several examples of executing the
- ' same method in order to test different
- ' parameters or cirsumstances.
- ' It is usually beneficial to run
- ' the test script at least daily, even though the
- ' BOM may be "stabilized", if only to confirm
- ' continued compatibility.
- '
- ' Removes any existing data from the database, then
- ' creates a full set of test data. The test data
- ' implements a non-visual Business Object Model
- ' based on the Classes "Company", "Person",
- ' "Address" and "Phone". In addition, there are
- ' "static" objects for "State", "MaritalStatus"
- ' and "Gender".
- ' Recommendation: carefully review the contents of
- ' this method for numerous examples of utilizing
- ' the advanced object-oriented features for VB4,
- ' as provided by VBOF.
-
- Dim tempNewPerson As New Person
- Dim tempNewAddress As New Address
- Dim tempAddress As Address
- Dim tempRedundantAddressCollection As VBOFCollection
-
- On Local Error Resume Next
-
- ObjectManager.CompleteObjectCleanUp
- Set publicCompany.Persons = Nothing
-
- ' Start with a clean slate by deleting all
- ' existing data from the database
- CompanyDatabase.Execute "DELETE FROM Persons "
- CompanyDatabase.Execute "DELETE FROM Addresses "
- CompanyDatabase.Execute "DELETE FROM Phones "
- CompanyDatabase.Execute "DELETE FROM VBObjectFrameworkObjectLinks "
-
- ' Example if instantiating and populating
- ' a Person object without any VBOF intervention.
- Set George = New Person
- With George
- .CustomerNumber = "1363267"
- .FirstName = "George"
- .LastName = "Washington"
- .SSN = 123456789
- .DateOfBirth = #2/22/1732#
- .DateOfDeath = #2/22/1799#
- .MaritalStatus = "M"
- .Sex = "M"
- End With
-
- Set Martha = New Person
- With Martha
- .CustomerNumber = "1027497"
- .FirstName = "Martha"
- .LastName = "Washington"
- .MaidenName = "Dandridge Curtis"
- .SSN = 72951789
- .DateOfBirth = #8/29/1731#
- .DateOfDeath = #2/22/1802#
- .MaritalStatus = "M"
- .Sex = "F"
- End With
- ' Debug.Print George.FormattedName
- ' end of example
-
- ' Example of adding the Person to the
- ' Company.Persons VBOFCollection object
- Set George = _
- publicCompany.AddPerson _
- (Item:=George)
- ' Set Martha = _
- ' publicCompany.AddPerson _
- ' (Item:=Martha)
- ' Debug.Print publicCompany.Count
- ' (methods such as Company.AddPerson are wrappers
- ' for the generalized VBOFCollection.Add method,
- ' which automatically takes care of the
- ' Parent:=Me parameter)
- ' end of example
-
- Set Abe = New Person
- With Abe
- .CustomerNumber = "205289"
- .FirstName = "Abe"
- .LastName = "Lincoln"
- .SSN = 234567890
- .DateOfBirth = #2/22/1809#
- .DateOfDeath = #4/15/1865#
- .MaritalStatus = "M"
- .Sex = "M"
- End With
-
- Set Mary = New Person
- With Mary
- .CustomerNumber = "256839"
- .FirstName = "Mary"
- .LastName = "Lincoln"
- .MaidenName = "Todd"
- .SSN = 23474010
- .DateOfBirth = #11/3/1818#
- .DateOfDeath = #11/3/1882#
- .MaritalStatus = "M"
- .Sex = "F"
- End With
-
- Set Abe = _
- publicCompany.AddPerson _
- (Item:=Abe)
- Set Mary = _
- publicCompany.AddPerson _
- (Item:=Mary)
- ' Debug.Print publicCompany.Count
-
- ' Example of instantiating and populating a new
- ' Address object. Note that the value of the
- ' State or StateCode is not specified. This is
- ' because in the next emapl, the Address.State
- ' contained object is set to an instance of the
- ' State Class.
- ' Note: for VBOF to assist in this type of object
- ' containment, the new object must be initialized
- ' through ObjectManager.InitializeObject.
- Set pvtAddress = New Address
- ObjectManager.InitializeObject pvtAddress
- With pvtAddress
- .Line1 = "123 Elm"
- .City = "Tempe"
- .ZipCode = 80808
- .ZipSupplement = 5591
- .ZipExtension = 22
- .Status = "Current"
- .Usage = "Residence"
- End With
-
- ' Following is are two equivalent methods of
- ' instantiating a contained object with the
- ' assistance of VBOF (examples of contained
- ' objects are: Employee.Manager, Address.State,
- ' Product.Manufacturer, Vehicle.Loan,
- ' Person.Mother, Person.Father, Person.Spouse,
- ' etc.)
- ' Method A:
- ' Select the contained object from a collection of
- ' already instantiated objects:
- Set pvtAddress.State = pubStates.Item("4")
- ' Method B:
- ' Have VBOF Object Manager instantiate the
- ' contained object, based on its Class type and
- ' the desired object's ObjectID:
- Dim NewState As New State
- Set NewState = _
- ObjectManager. _
- NewObject( _
- Database:=CompanyDatabase, _
- Sample:=NewState, _
- ObjectID:=4)
- Set pvtAddress.State = NewState
- ' Either method is acceptable. Method A
- ' requires that the collection of possible values
- ' be available. This essentially means that all
- ' overhead would already have been expended
- ' up-front to gather the data and instantiate
- ' those possible objects for the Collection.
- ' In this way, all objects are instantiated in
- ' a single trip to the database.
- ' There is no way of knowing for sure whether or
- ' not it was worth gathering all of the data and
- ' instantiating the objects until all of the
- ' business object model has been processed.
- ' User insight of the application and the
- ' applicable data is recommended.
- ' Method B incurs overhead only as objects are
- ' needed.
- ' If there are numerous instances of singly-used
- ' objects, then the number of trips to the
- ' database can become an issue, since VBOF
- ' would make one trip to the database for each
- ' (unique) object.
- ' User insight of the application and the
- ' applicable data is recommended.
- ' Note that under no circumstances does VBOF
- ' Object Manager allow the identical object to be
- ' instantiated a second time. So, even if the
- ' user elects to vary the selected Method -- even
- ' for the same object -- there is never a second
- ' trip to the database for the data for that
- ' object.
- ' The remainder of these examples use Method A
- ' because the States collection is fully
- ' available.
- ' Debug.Print pvtAddress.FormattedAddress
- ' end of example
-
- Set pvtAddress = _
- George.AddAddress _
- (Item:=pvtAddress)
- ' Debug.Print George.Addresses(1).FormattedAddress
-
- Set pvtAddress = New Address
- ObjectManager.InitializeObject pvtAddress
- With pvtAddress
- .Line1 = "122 Pine"
- .City = "Chicago"
- .ZipCode = 60609
- .Status = "Former"
- .Usage = "Residence"
- End With
- Set pvtAddress.State = pubStates.Item("14")
-
- Set pvtAddress = _
- George.AddAddress _
- (Item:=pvtAddress)
-
- Set pvtPhone = New Phone
- ObjectManager.InitializeObject pvtPhone
- With pvtPhone
- .PhoneNumber = "3035551400"
- .Usage = "Residence"
- End With
-
- Set pvtPhone = _
- George.AddPhone _
- (Item:=pvtPhone)
-
- Set pvtPhone = New Phone
- ObjectManager.InitializeObject pvtPhone
- With pvtPhone
- .PhoneNumber = "8185551212"
- .Usage = "Business"
- End With
-
- Set pvtPhone = _
- George.AddPhone _
- (Item:=pvtPhone)
-
- ' Example of containing one of George's
- ' instantiated Address objects within the
- ' Abe.Addresses VBOFCollection object.
- ' Note: there is no new object instantiated due
- ' to this operation -- both VBOFCollections
- ' contain references to the same object
- Abe.AddAddress _
- Item:=George.Addresses(1)
- ' Debug.Print Abe.Addresses.Count
-
- ' Example of containing one of George's
- ' instantiated Phone objects within the
- ' Abe.Phones VBOFCollection object.
- ' Note: there is no new object instantiated due
- ' to this operation -- both VBOFCollections
- ' contain references to the same object
- Abe.AddPhone _
- Item:=George.Phones(1)
- ' Debug.Print Abe.Addresses.Count
-
- Set pvtAddress = New Address
- ObjectManager.InitializeObject pvtAddress
- With pvtAddress
- .Line1 = "401 Maple"
- .City = "Concord"
- .ZipCode = 6709
- .Status = "Former"
- .Usage = "Residence"
- End With
- Set pvtAddress.State = pubStates.Item("19")
-
- Set pvtAddress = _
- Abe.AddAddress _
- (Item:=pvtAddress)
-
- Set pvtPhone = New Phone
- ObjectManager.InitializeObject pvtPhone
- With pvtPhone
- .PhoneNumber = "2024441578"
- .Usage = "Business"
- End With
-
- Set pvtPhone = _
- Abe.AddPhone _
- (Item:=pvtPhone)
-
- Set pvtPhone = New Phone
- ObjectManager.InitializeObject pvtPhone
- With pvtPhone
- .PhoneNumber = "9132242668"
- .Usage = "Fax"
- End With
-
- Set pvtPhone = _
- Abe.AddPhone _
- (Item:=pvtPhone)
-
- Set BillClinton = New Person
- With BillClinton
- .CustomerNumber = "1"
- .FirstName = "Bill"
- .LastName = "Clinton"
- .SSN = 123456789
- .DateOfBirth = #1/3/46#
- .MaritalStatus = "M"
- .Sex = "M"
- End With
-
- Set BillClinton = _
- publicCompany.AddPerson _
- (Item:=BillClinton)
-
- Set HillaryClinton = New Person
- With HillaryClinton
- .CustomerNumber = "2"
- .FirstName = "Hillary"
- .LastName = "Clinton"
- .MaidenName = "Rodham"
- .SSN = 234567890
- .DateOfBirth = #6/22/47#
- .MaritalStatus = "M"
- .Sex = "F"
- End With
-
- Set HillaryClinton = _
- publicCompany.AddPerson _
- (Item:=HillaryClinton)
-
- Set ChelseaClinton = New Person
- With ChelseaClinton
- .CustomerNumber = "3"
- .FirstName = "Chelsea"
- .LastName = "Clinton"
- .SSN = 345678901
- .DateOfBirth = #8/29/80#
- .MaritalStatus = "S"
- .Sex = "F"
- End With
-
- ' Example of setting the value of a contained
- ' object.
- ' Note: the Person Class has properties for Mother
- ' and Father contained objects. In the following
- ' example, Chelsea's Mother object is set to the
- ' Hillary object and her Father object is set to
- ' the Bill object.
- ' Refer to the Person Class example for details
- ' on how this is automatically implemented over
- ' the database
- Set ChelseaClinton.Mother = HillaryClinton
- Set ChelseaClinton.Father = BillClinton
- ' end of example
-
- Set ChelseaClinton = _
- publicCompany.AddPerson _
- (Item:=ChelseaClinton)
-
- Set pvtAddress = New Address
- ObjectManager.InitializeObject pvtAddress
- With pvtAddress
- .Line1 = "1600 Pennsylvannia Ave."
- .City = "Washington"
- .ZipCode = 12345
- .ZipSupplement = 1001
- .Status = "Current"
- .Usage = "Primary"
- End With
-
- Set pvtAddress.State = pubStates.Item("51")
-
- BillClinton.AddAddress _
- Item:=pvtAddress
-
- ' Example of how VBOFObjectManager prevents
- ' redundant instances of specific objects from
- ' being created.
- ' This example attempts to instantiate a new
- ' VBOFCollection of BillClinton's addresses,
- ' but what is actually returned is a
- ' VBOFCollection which references his previously
- ' instantiated address objects -- not a
- ' collection of new (and redundant) objects.
- ' By stepping through the code (if desired) the user
- ' will witness ObjectManager rejecting the
- ' attempts to redundantly instantiate each
- ' object; refering instead to the already
- ' existing instance of each.
- Set tempRedundantAddressCollection = _
- ObjectManager. _
- NewVBOFCollection()
-
- tempRedundantAddressCollection. _
- SetDatabaseParameters _
- Database:=CompanyDatabase
-
- Set tempRedundantAddressCollection = _
- tempRedundantAddressCollection. _
- PopulateCollection( _
- Database:=CompanyDatabase, _
- Parent:=BillClinton, _
- Sample:=tempNewAddress)
- ' end of example
-
- Set tempAddress = BillClinton.Addresses(1)
-
- HillaryClinton.AddAddress _
- Item:=tempAddress
-
- ChelseaClinton.AddAddress _
- Item:=tempAddress
-
- ' Example of an on-demand creation of a
- ' VBOFCollection of instantiated objects,
- ' based on the contents of the database.
- ' Gather all of the above individuals and
- ' place them in pubPersons
- Set pubPersons = _
- pubPersons. _
- PopulateCollection( _
- Database:=CompanyDatabase, _
- Parent:=publicCompany, _
- Sample:=tempNewPerson)
- ' end of example
-
- Abe.AddAddress _
- Item:=BillClinton.Addresses(1)
-
- ' Example of VBOFCollection.Replace to
- ' replace an object in a VBOFCollection
- Abe.Addresses.Replace _
- Item:=Abe.Addresses(3), _
- ReplaceWith:=George.Addresses(2)
- ' end of example
-
- ' now that all Person.ObjectID's are known,
- ' hook-up spouses
- Set BillClinton.Spouse = _
- HillaryClinton
- Set HillaryClinton.Spouse = _
- BillClinton
-
- pubPersons.Replace _
- Item:=BillClinton, _
- ReplaceWith:=BillClinton
- pubPersons.Replace _
- Item:=HillaryClinton, _
- ReplaceWith:=HillaryClinton
-
- Set Abe.Spouse = Mary
- Set Mary.Spouse = Abe
-
- pubPersons.Replace _
- Item:=Abe, _
- ReplaceWith:=Abe
- pubPersons.Replace _
- Item:=Mary, _
- ReplaceWith:=Mary
-
- Set George.Spouse = Martha
- Set Martha.Spouse = George
-
- pubPersons.Replace _
- Item:=George, _
- ReplaceWith:=George
- pubPersons.Replace _
- Item:=Martha, _
- ReplaceWith:=Martha
-
- ' Example of emptying an entire collection
- ' ObjectManager.EmptyCollection _
- ' Collection:=Abe.Addresses, _
- ' NoDelete:=True
- ' end of example
- End Sub
-
- Sub Main()
-
- Set CompanyDatabase = _
- DBEngine.Workspaces(0). _
- OpenDatabase(App.Path & "\DemoVBOF.MDB")
-
- CreateObjectManager
-
- Set publicCompany = New Company
- Set publicCompany.ObjectManager = _
- ObjectManager
-
- CreatePersonsCollection
-
- PopulateStateCodes
- PopulateGenderCodes
- PopulateMaritalStatusCodes
-
- ' pass-along the ObjectManager
- Set Intro.ObjectManager = _
- ObjectManager
-
- Intro.Show
-
- End Sub
-
-
-
-
-