home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / vbof / demovbof.bas < prev    next >
Encoding:
BASIC Source File  |  1996-11-20  |  19.3 KB  |  662 lines

  1. Attribute VB_Name = "DemoVBObjectFramework"
  2. Option Explicit
  3.  
  4. ' These Collections are used by other Forms and
  5. '   .BASs
  6. Public publicCompany As New Company
  7. Public CompanyDatabase As Database
  8. Public pubPersons As VBOFCollection
  9. Public pubStates As VBOFCollection
  10. Public pubGenderCodes As VBOFCollection
  11. Public pubMaritalStatusCodes As VBOFCollection
  12.  
  13. ' This is the only instance of VBOFObjectManager
  14. '   which exists throughout the application.  It is
  15. '   shared by the entire application
  16. Private ObjectManager As New VBOFObjectManager
  17.  
  18. Private pvtPerson As Person
  19. Private pvtAddress As Address
  20. Private pvtPhone As Phone
  21.  
  22. ' In the non-visual BOM script herein, procedure
  23. '   "CreateTestData", these named Person objects
  24. '   are used simply for clarity.  The Persons also
  25. '   appear in the pubPersons Collection which is
  26. '   used across the application.
  27. Private George As Person
  28. Private Martha As Person
  29. Private Abe As Person
  30. Private Mary As Person
  31. Private BillClinton As Person
  32. Private HillaryClinton As Person
  33. Private ChelseaClinton As Person
  34.  
  35. Private Sub PopulateGenderCodes()
  36.     
  37.     Dim tempGenderObject As New Gender
  38.     
  39. ' instantiate pvtGenderCodes as a VBOFCollection
  40.     Set pubGenderCodes = _
  41.         ObjectManager. _
  42.             NewVBOFCollection
  43.                 
  44.     Set pubGenderCodes = _
  45.         pubGenderCodes.PopulateCollection( _
  46.             Database:=CompanyDatabase, _
  47.             SQL:="SELECT * FROM " & _
  48.                 tempGenderObject.ObjectDataSource, _
  49.             Sample:=tempGenderObject)
  50.         
  51.     pubGenderCodes.pvtCloseRecordSet
  52.  
  53. End Sub
  54.  
  55. Private Sub PopulateMaritalStatusCodes()
  56.     
  57.     Dim tempMaritalStatusObject As New MaritalStatus
  58.     
  59. ' instantiate pvtMaritalStatusCodes as a VBOFCollection
  60.     Set pubMaritalStatusCodes = _
  61.         ObjectManager. _
  62.             NewVBOFCollection
  63.                 
  64.     Set pubMaritalStatusCodes = _
  65.         pubMaritalStatusCodes.PopulateCollection( _
  66.             Database:=CompanyDatabase, _
  67.             SQL:="SELECT * FROM " & _
  68.                 tempMaritalStatusObject.ObjectDataSource, _
  69.             Sample:=tempMaritalStatusObject)
  70.         
  71.     pubMaritalStatusCodes.pvtCloseRecordSet
  72.  
  73. End Sub
  74.  
  75.  
  76. Private Sub PopulateStateCodes()
  77.     
  78.     Dim tempStateObject As New State
  79.     
  80. ' instantiate pvtStates as a VBOFCollection
  81.     Set pubStates = _
  82.         ObjectManager. _
  83.             NewVBOFCollection
  84.                 
  85.     Set pubStates = _
  86.         pubStates.PopulateCollection( _
  87.             Database:=CompanyDatabase, _
  88.             SQL:="SELECT * FROM " & _
  89.                 tempStateObject.ObjectDataSource, _
  90.             Sample:=tempStateObject)
  91.         
  92.     pubStates.pvtCloseRecordSet
  93.  
  94. End Sub
  95.  
  96.  
  97. Sub CreateObjectManager()
  98. ' instantiate the VBOFObjectManager
  99.     
  100.     Set ObjectManager = _
  101.         New VBOFObjectManager
  102.     
  103.     With ObjectManager
  104. ' play with these to your satisfaction
  105. '   (this is a subset of the available parameters)
  106. '        .AutoDeleteOrphans = True
  107. '        .SynchronousCommit = True
  108. #If NoDebugMode = False Then
  109. '        .DebugMode = True
  110. #End If
  111.     End With
  112.     
  113.     Set ObjectManager.Database = _
  114.         CompanyDatabase
  115.     Set ObjectManager.Workspace = _
  116.         Workspaces(0)
  117.  
  118. End Sub
  119.  
  120.  
  121. Sub CreatePersonsCollection()
  122.     
  123.     Dim tempNewPerson As New Person
  124.     
  125. ' instantiate pubPersons as a VBOFCollection
  126.     Set pubPersons = _
  127.         ObjectManager. _
  128.             NewVBOFCollection
  129.     
  130.     pubPersons.SetDatabaseParameters _
  131.         OrderByClause:="LastName ASC, FirstName ASC"
  132. ' play with these to your satisfaction
  133. '   (this is a subset of the available parameters)
  134. '        Database:=CompanyDatabase, _
  135. '        ANSISQL:=True, _
  136.  
  137.     pubPersons.PopulateCollection _
  138.         Sample:=tempNewPerson, _
  139.         Parent:=publicCompany
  140.  
  141. End Sub
  142.  
  143.  
  144. Sub CreateTestData()
  145. ' This is essentially a non-visual BOM Test Script.
  146. '   While developing the non-visual BOM, a script
  147. '   such as this should be used to verify objects
  148. '   and methods.
  149. ' The test script should (obviously) run withouth
  150. '   the procedurally-oriented GUI or DataControl
  151. '   objects.  It should also be able to run in a
  152. '   non-database mode, as well.  That is, objects
  153. '   should be instantiated, populated and work
  154. '   properly without concern for the underlying
  155. '   database.  When the object behavior has
  156. '   stabilized, the underlying database can be
  157. '   easily applied.  The VBOF fully supports this
  158. '   BOM development technique.
  159. ' In general, there should be at least one usage of
  160. '   each object and method represented in the test
  161. '   script.  Under some circumstances, there may be
  162. '   the need for several examples of executing the
  163. '   same method in order to test different
  164. '   parameters or cirsumstances.
  165. ' It is usually beneficial to run
  166. '   the test script at least daily, even though the
  167. '   BOM may be "stabilized", if only to confirm
  168. '   continued compatibility.
  169. '
  170. ' Removes any existing data from the database, then
  171. '   creates a full set of test data.  The test data
  172. '   implements a non-visual Business Object Model
  173. '   based on the Classes "Company", "Person",
  174. '   "Address" and "Phone".  In addition, there are
  175. '   "static" objects for "State", "MaritalStatus"
  176. '   and "Gender".
  177. ' Recommendation:  carefully review the contents of
  178. '   this method for numerous examples of utilizing
  179. '   the advanced object-oriented features for VB4,
  180. '   as provided by VBOF.
  181.     
  182.     Dim tempNewPerson As New Person
  183.     Dim tempNewAddress As New Address
  184.     Dim tempAddress  As Address
  185.     Dim tempRedundantAddressCollection As VBOFCollection
  186.  
  187.     On Local Error Resume Next
  188.     
  189.     ObjectManager.CompleteObjectCleanUp
  190.     Set publicCompany.Persons = Nothing
  191.     
  192. ' Start with a clean slate by deleting all
  193. '   existing data from the database
  194.     CompanyDatabase.Execute "DELETE FROM Persons "
  195.     CompanyDatabase.Execute "DELETE FROM Addresses "
  196.     CompanyDatabase.Execute "DELETE FROM Phones "
  197.     CompanyDatabase.Execute "DELETE FROM VBObjectFrameworkObjectLinks "
  198.  
  199. ' Example if instantiating and populating
  200. '   a Person object without any VBOF intervention.
  201.     Set George = New Person
  202.     With George
  203.         .CustomerNumber = "1363267"
  204.         .FirstName = "George"
  205.         .LastName = "Washington"
  206.         .SSN = 123456789
  207.         .DateOfBirth = #2/22/1732#
  208.         .DateOfDeath = #2/22/1799#
  209.         .MaritalStatus = "M"
  210.         .Sex = "M"
  211.     End With
  212.  
  213.     Set Martha = New Person
  214.     With Martha
  215.         .CustomerNumber = "1027497"
  216.         .FirstName = "Martha"
  217.         .LastName = "Washington"
  218.         .MaidenName = "Dandridge Curtis"
  219.         .SSN = 72951789
  220.         .DateOfBirth = #8/29/1731#
  221.         .DateOfDeath = #2/22/1802#
  222.         .MaritalStatus = "M"
  223.         .Sex = "F"
  224.     End With
  225. ' Debug.Print George.FormattedName
  226. ' end of example
  227.  
  228. ' Example of adding the Person to the
  229. '   Company.Persons VBOFCollection object
  230.     Set George = _
  231.         publicCompany.AddPerson _
  232.             (Item:=George)
  233. '    Set Martha = _
  234. '        publicCompany.AddPerson _
  235. '            (Item:=Martha)
  236. ' Debug.Print publicCompany.Count
  237. '   (methods such as Company.AddPerson are wrappers
  238. '   for the generalized VBOFCollection.Add method,
  239. '   which automatically takes care of the
  240. '   Parent:=Me parameter)
  241. ' end of example
  242.     
  243.     Set Abe = New Person
  244.     With Abe
  245.         .CustomerNumber = "205289"
  246.         .FirstName = "Abe"
  247.         .LastName = "Lincoln"
  248.         .SSN = 234567890
  249.         .DateOfBirth = #2/22/1809#
  250.         .DateOfDeath = #4/15/1865#
  251.         .MaritalStatus = "M"
  252.         .Sex = "M"
  253.     End With
  254.     
  255.     Set Mary = New Person
  256.     With Mary
  257.         .CustomerNumber = "256839"
  258.         .FirstName = "Mary"
  259.         .LastName = "Lincoln"
  260.         .MaidenName = "Todd"
  261.         .SSN = 23474010
  262.         .DateOfBirth = #11/3/1818#
  263.         .DateOfDeath = #11/3/1882#
  264.         .MaritalStatus = "M"
  265.         .Sex = "F"
  266.     End With
  267.     
  268.     Set Abe = _
  269.         publicCompany.AddPerson _
  270.             (Item:=Abe)
  271.     Set Mary = _
  272.         publicCompany.AddPerson _
  273.             (Item:=Mary)
  274. ' Debug.Print publicCompany.Count
  275.  
  276. ' Example of instantiating and populating a new
  277. '   Address object.  Note that the value of the
  278. '   State or StateCode is not specified.  This is
  279. '   because in the next emapl, the Address.State
  280. '   contained object is set to an instance of the
  281. '   State Class.
  282. ' Note: for VBOF to assist in this type of object
  283. '   containment, the new object must be initialized
  284. '   through ObjectManager.InitializeObject.
  285.     Set pvtAddress = New Address
  286.     ObjectManager.InitializeObject pvtAddress
  287.     With pvtAddress
  288.         .Line1 = "123 Elm"
  289.         .City = "Tempe"
  290.         .ZipCode = 80808
  291.         .ZipSupplement = 5591
  292.         .ZipExtension = 22
  293.         .Status = "Current"
  294.         .Usage = "Residence"
  295.     End With
  296.     
  297. ' Following is are two equivalent methods of
  298. '   instantiating a contained object with the
  299. '   assistance of VBOF (examples of contained
  300. '   objects are: Employee.Manager, Address.State,
  301. '   Product.Manufacturer, Vehicle.Loan,
  302. '   Person.Mother, Person.Father, Person.Spouse,
  303. '   etc.)
  304. ' Method A:
  305. '   Select the contained object from a collection of
  306. '   already instantiated objects:
  307.     Set pvtAddress.State = pubStates.Item("4")
  308. ' Method B:
  309. '   Have VBOF Object Manager instantiate the
  310. '   contained object, based on its Class type and
  311. '   the desired object's ObjectID:
  312.     Dim NewState As New State
  313.     Set NewState = _
  314.         ObjectManager. _
  315.             NewObject( _
  316.                 Database:=CompanyDatabase, _
  317.                 Sample:=NewState, _
  318.                 ObjectID:=4)
  319.     Set pvtAddress.State = NewState
  320. ' Either method is acceptable.  Method A
  321. '   requires that the collection of possible values
  322. '   be available.  This essentially means that all
  323. '   overhead would already have been expended
  324. '   up-front to gather the data and instantiate
  325. '   those possible objects for the Collection.
  326. '   In this way, all objects are instantiated in
  327. '   a single trip to the database.
  328. '   There is no way of knowing for sure whether or
  329. '   not it was worth gathering all of the data and
  330. '   instantiating the objects until all of the
  331. '   business object model has been processed.
  332. '   User insight of the application and the
  333. '   applicable data is recommended.
  334. ' Method B incurs overhead only as objects are
  335. '   needed.
  336. '   If there are numerous instances of singly-used
  337. '   objects, then the number of trips to the
  338. '   database can become an issue, since VBOF
  339. '   would make one trip to the database for each
  340. '   (unique) object.
  341. '   User insight of the application and the
  342. '   applicable data is recommended.
  343. ' Note that under no circumstances does VBOF
  344. '   Object Manager allow the identical object to be
  345. '   instantiated a second time.  So, even if the
  346. '   user elects to vary the selected Method -- even
  347. '   for the same object -- there is never a second
  348. '   trip to the database for the data for that
  349. '   object.
  350. ' The remainder of these examples use Method A
  351. '   because the States collection is fully
  352. '   available.
  353. ' Debug.Print pvtAddress.FormattedAddress
  354. ' end of example
  355.  
  356.     Set pvtAddress = _
  357.         George.AddAddress _
  358.             (Item:=pvtAddress)
  359. ' Debug.Print George.Addresses(1).FormattedAddress
  360.  
  361.     Set pvtAddress = New Address
  362.     ObjectManager.InitializeObject pvtAddress
  363.     With pvtAddress
  364.         .Line1 = "122 Pine"
  365.         .City = "Chicago"
  366.         .ZipCode = 60609
  367.         .Status = "Former"
  368.         .Usage = "Residence"
  369.     End With
  370.     Set pvtAddress.State = pubStates.Item("14")
  371.     
  372.     Set pvtAddress = _
  373.         George.AddAddress _
  374.             (Item:=pvtAddress)
  375.  
  376.     Set pvtPhone = New Phone
  377.     ObjectManager.InitializeObject pvtPhone
  378.     With pvtPhone
  379.         .PhoneNumber = "3035551400"
  380.         .Usage = "Residence"
  381.     End With
  382.     
  383.     Set pvtPhone = _
  384.         George.AddPhone _
  385.             (Item:=pvtPhone)
  386.  
  387.     Set pvtPhone = New Phone
  388.     ObjectManager.InitializeObject pvtPhone
  389.     With pvtPhone
  390.         .PhoneNumber = "8185551212"
  391.         .Usage = "Business"
  392.     End With
  393.     
  394.     Set pvtPhone = _
  395.         George.AddPhone _
  396.             (Item:=pvtPhone)
  397.         
  398. ' Example of containing one of George's
  399. '   instantiated Address objects within the
  400. '   Abe.Addresses VBOFCollection object.
  401. ' Note: there is no new object instantiated due
  402. '   to this operation -- both VBOFCollections
  403. '   contain references to the same object
  404.     Abe.AddAddress _
  405.         Item:=George.Addresses(1)
  406. ' Debug.Print Abe.Addresses.Count
  407.  
  408. ' Example of containing one of George's
  409. '   instantiated Phone objects within the
  410. '   Abe.Phones VBOFCollection object.
  411. ' Note: there is no new object instantiated due
  412. '   to this operation -- both VBOFCollections
  413. '   contain references to the same object
  414.     Abe.AddPhone _
  415.         Item:=George.Phones(1)
  416. ' Debug.Print Abe.Addresses.Count
  417.         
  418.     Set pvtAddress = New Address
  419.     ObjectManager.InitializeObject pvtAddress
  420.     With pvtAddress
  421.         .Line1 = "401 Maple"
  422.         .City = "Concord"
  423.         .ZipCode = 6709
  424.         .Status = "Former"
  425.         .Usage = "Residence"
  426.     End With
  427.     Set pvtAddress.State = pubStates.Item("19")
  428.     
  429.     Set pvtAddress = _
  430.         Abe.AddAddress _
  431.             (Item:=pvtAddress)
  432.  
  433.     Set pvtPhone = New Phone
  434.     ObjectManager.InitializeObject pvtPhone
  435.     With pvtPhone
  436.         .PhoneNumber = "2024441578"
  437.         .Usage = "Business"
  438.     End With
  439.     
  440.     Set pvtPhone = _
  441.         Abe.AddPhone _
  442.             (Item:=pvtPhone)
  443.  
  444.     Set pvtPhone = New Phone
  445.     ObjectManager.InitializeObject pvtPhone
  446.     With pvtPhone
  447.         .PhoneNumber = "9132242668"
  448.         .Usage = "Fax"
  449.     End With
  450.     
  451.     Set pvtPhone = _
  452.         Abe.AddPhone _
  453.             (Item:=pvtPhone)
  454.  
  455.     Set BillClinton = New Person
  456.     With BillClinton
  457.         .CustomerNumber = "1"
  458.         .FirstName = "Bill"
  459.         .LastName = "Clinton"
  460.         .SSN = 123456789
  461.         .DateOfBirth = #1/3/46#
  462.         .MaritalStatus = "M"
  463.         .Sex = "M"
  464.     End With
  465.     
  466.     Set BillClinton = _
  467.         publicCompany.AddPerson _
  468.             (Item:=BillClinton)
  469.  
  470.     Set HillaryClinton = New Person
  471.     With HillaryClinton
  472.         .CustomerNumber = "2"
  473.         .FirstName = "Hillary"
  474.         .LastName = "Clinton"
  475.         .MaidenName = "Rodham"
  476.         .SSN = 234567890
  477.         .DateOfBirth = #6/22/47#
  478.         .MaritalStatus = "M"
  479.         .Sex = "F"
  480.     End With
  481.     
  482.     Set HillaryClinton = _
  483.         publicCompany.AddPerson _
  484.             (Item:=HillaryClinton)
  485.  
  486.     Set ChelseaClinton = New Person
  487.     With ChelseaClinton
  488.         .CustomerNumber = "3"
  489.         .FirstName = "Chelsea"
  490.         .LastName = "Clinton"
  491.         .SSN = 345678901
  492.         .DateOfBirth = #8/29/80#
  493.         .MaritalStatus = "S"
  494.         .Sex = "F"
  495.     End With
  496.     
  497. ' Example of setting the value of a contained
  498. '   object.
  499. ' Note:  the Person Class has properties for Mother
  500. '   and Father contained objects.  In the following
  501. '   example, Chelsea's Mother object is set to the
  502. '   Hillary object and her Father object is set to
  503. '   the Bill object.
  504. ' Refer to the Person Class example for details
  505. '   on how this is automatically implemented over
  506. '   the database
  507.     Set ChelseaClinton.Mother = HillaryClinton
  508.     Set ChelseaClinton.Father = BillClinton
  509. ' end of example
  510.     
  511.     Set ChelseaClinton = _
  512.         publicCompany.AddPerson _
  513.             (Item:=ChelseaClinton)
  514.     
  515.     Set pvtAddress = New Address
  516.     ObjectManager.InitializeObject pvtAddress
  517.     With pvtAddress
  518.         .Line1 = "1600 Pennsylvannia Ave."
  519.         .City = "Washington"
  520.         .ZipCode = 12345
  521.         .ZipSupplement = 1001
  522.         .Status = "Current"
  523.         .Usage = "Primary"
  524.     End With
  525.     
  526.     Set pvtAddress.State = pubStates.Item("51")
  527.     
  528.     BillClinton.AddAddress _
  529.         Item:=pvtAddress
  530.  
  531. ' Example of how VBOFObjectManager prevents
  532. '   redundant instances of specific objects from
  533. '   being created.
  534. ' This example attempts to instantiate a new
  535. '   VBOFCollection of BillClinton's addresses,
  536. '   but what is actually returned is a
  537. '   VBOFCollection which references his previously
  538. '   instantiated address objects -- not a
  539. '   collection of new (and redundant) objects.
  540. ' By stepping through the code (if desired) the user
  541. '   will witness ObjectManager rejecting the
  542. '   attempts to redundantly instantiate each
  543. '   object; refering instead to the already
  544. '   existing instance of each.
  545.     Set tempRedundantAddressCollection = _
  546.         ObjectManager. _
  547.             NewVBOFCollection()
  548.     
  549.     tempRedundantAddressCollection. _
  550.         SetDatabaseParameters _
  551.             Database:=CompanyDatabase
  552.     
  553.     Set tempRedundantAddressCollection = _
  554.         tempRedundantAddressCollection. _
  555.             PopulateCollection( _
  556.                 Database:=CompanyDatabase, _
  557.                 Parent:=BillClinton, _
  558.                 Sample:=tempNewAddress)
  559. ' end of example
  560.  
  561.     Set tempAddress = BillClinton.Addresses(1)
  562.  
  563.     HillaryClinton.AddAddress _
  564.         Item:=tempAddress
  565.  
  566.     ChelseaClinton.AddAddress _
  567.         Item:=tempAddress
  568.     
  569. ' Example of an on-demand creation of a
  570. '   VBOFCollection of instantiated objects,
  571. '   based on the contents of the database.
  572. ' Gather all of the above individuals and
  573. '   place them in pubPersons
  574.     Set pubPersons = _
  575.         pubPersons. _
  576.             PopulateCollection( _
  577.                 Database:=CompanyDatabase, _
  578.                 Parent:=publicCompany, _
  579.                 Sample:=tempNewPerson)
  580. ' end of example
  581.  
  582.     Abe.AddAddress _
  583.         Item:=BillClinton.Addresses(1)
  584.         
  585. ' Example of VBOFCollection.Replace to
  586. '   replace an object in a VBOFCollection
  587.     Abe.Addresses.Replace _
  588.         Item:=Abe.Addresses(3), _
  589.         ReplaceWith:=George.Addresses(2)
  590. ' end of example
  591.     
  592. ' now that all Person.ObjectID's are known,
  593. '   hook-up spouses
  594.     Set BillClinton.Spouse = _
  595.         HillaryClinton
  596.     Set HillaryClinton.Spouse = _
  597.         BillClinton
  598.     
  599.     pubPersons.Replace _
  600.         Item:=BillClinton, _
  601.         ReplaceWith:=BillClinton
  602.     pubPersons.Replace _
  603.         Item:=HillaryClinton, _
  604.         ReplaceWith:=HillaryClinton
  605.     
  606.     Set Abe.Spouse = Mary
  607.     Set Mary.Spouse = Abe
  608.     
  609.     pubPersons.Replace _
  610.         Item:=Abe, _
  611.         ReplaceWith:=Abe
  612.     pubPersons.Replace _
  613.         Item:=Mary, _
  614.         ReplaceWith:=Mary
  615.     
  616.     Set George.Spouse = Martha
  617.     Set Martha.Spouse = George
  618.     
  619.     pubPersons.Replace _
  620.         Item:=George, _
  621.         ReplaceWith:=George
  622.     pubPersons.Replace _
  623.         Item:=Martha, _
  624.         ReplaceWith:=Martha
  625.  
  626. ' Example of emptying an entire collection
  627. '    ObjectManager.EmptyCollection _
  628. '       Collection:=Abe.Addresses, _
  629. '       NoDelete:=True
  630. ' end of example
  631. End Sub
  632.  
  633. Sub Main()
  634.  
  635.     Set CompanyDatabase = _
  636.         DBEngine.Workspaces(0). _
  637.             OpenDatabase(App.Path & "\DemoVBOF.MDB")
  638.         
  639.     CreateObjectManager
  640.     
  641.     Set publicCompany = New Company
  642.     Set publicCompany.ObjectManager = _
  643.         ObjectManager
  644.     
  645.     CreatePersonsCollection
  646.     
  647.     PopulateStateCodes
  648.     PopulateGenderCodes
  649.     PopulateMaritalStatusCodes
  650.  
  651. ' pass-along the ObjectManager
  652.     Set Intro.ObjectManager = _
  653.         ObjectManager
  654.     
  655.     Intro.Show
  656.  
  657. End Sub
  658.  
  659.  
  660.  
  661.  
  662.