Results 1 to 13 of 13

Thread: Yet another n-tier development question

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Yet another n-tier development question

    *** Note: I also posted this in the COM/AX forum ***

    I'm looking to get some opinions on this. What do think is the best way to get data to the presentation layer (i.e., your VB user interface) from the business object or application layer? For example, let's say you need to load up some combo boxes from various lookup tables.

    Of course bear in mind that the presentation layer must not directly access the database. The "GetRows" method seems like it would be most efficient, but I don't particularly like referring to fields by numbers and the reverse row/column structure of the array returned by GetRows can be a little confusing if I haven't had my coffee yet. What is your opinion on the use of UDTs or collections for this kind of processing?

    I must admit that although I have been using VB for quite a while, I'm not a heavyweight when it comes to OO development (but I'm working on it). And yes, I have some books that I need to dig into where this is concerned. What I'm looking for here is views from folks who have been down this road before.

    Thanks for your attention.
    "It's cold gin time again ..."

    Check out my website here.

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I would create a dll that handled all the business rules that your user interface goes thru to get to the DB.


    Is that what you ment?
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    I know that I would create one or more DLLs that contain the business rules. The classes in these DLLs would talk directly to the database (using ADO or whatever). My question is, when methods of these classes do a SELECT-type statement to pass data back to the presentation layer (i.e., a VB form), what techniques are preferred so that there is no reference to ADO in the form? For example, the business classes must pass back the data in a more of a generic-type structure (such as an array or collection) as opposed to an "ADODB.Recordset". Or alternatively, the business class could pass back one "row's worth" of data at a time, with the VB form calling a "MoveNext" or "GetNext" type method of the business class. (The latter seems less efficient.) So again, I ask your opinions on the best way of constructing a true "n-tier" application ...
    "It's cold gin time again ..."

    Check out my website here.

  4. #4

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Thoughts on this, anyone?
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Your form should call Business logic class only. In turn business class would call all DB operations from the DB class.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It's like Serge said you'd use the biz classes in place of lets say the recordset. So if you normally access a table with user info you'd probably have the properties of the CUser class would what was the fields of the table, kind of.

  7. #7

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    OK, I understand the concepts. Now, how about a practical example: say I want to load up a combo box with data from a lookup table. From my form, I invoke cBizClass.GetDepartmentData. The GetDepartmentData method access the database, using "SELECT DeptCode, DeptName FROM Departments". In that method, the database returns a recordset. I now want to load that recordset data into the form's combo box. What would you say is the best way for cBizClass to transfer that data back to the form?
    "It's cold gin time again ..."

    Check out my website here.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You have a couple different things you could be there:

    You could pass the combo/list control to a function in the class that fills it.

    You could call a function in the class that returns an array of the data you want. Or if not an array a collection or whatever you need.

    Or if you are working with a collection you can loop through its items and add them to the list.

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Ok, here it is. I've created a classic example where you would have everything in classes. So my tiers are CDB, CBusiness and Form1. Also for department I use CDepartments collection class and CDepartment class to hold the data for each Department.
    VB Code:
    1. '==========CDepartment Class=================
    2. Private m_lngDeptId As Long
    3. Private m_strDeptName As String
    4.  
    5. Public Property Let DeptId(ByVal p_lngDeptId As Long)
    6.     m_lngDeptId = p_lngDeptId
    7. End Property
    8.  
    9. Public Property Get DeptId() As Long
    10.     DeptId = m_lngDeptId
    11. End Property
    12.  
    13. Public Property Let DeptName(ByVal p_strDeptName As String)
    14.     m_strDeptName = p_strDeptName
    15. End Property
    16.  
    17. Public Property Get DeptName() As String
    18.     DeptName = m_strDeptName
    19. End Property
    20.  
    21.  
    22.  
    23.  
    24. '=============CDepartments Collection==========
    25. Private m_colDept As Collection
    26.  
    27. Public Sub Add(Item As CDepartment, key As Variant)
    28.     m_colDept.Add Item, key
    29. End Sub
    30. Public Property Get Count() As Long
    31.     Count = m_colDept.Count
    32. End Property
    33.  
    34. Public Sub Remove(index As Variant)
    35.     m_colDept.Remove key
    36. End Sub
    37.  
    38.  
    39. Private Sub Class_Initialize()
    40.     Set m_colDept = New Collection
    41. End Sub
    42.  
    43. Public Function Item(index As Variant) As CDepartment
    44.     Set Item = m_colDept.Item(index)
    45. End Function
    46.  
    47.  
    48. '===============CDB========================
    49. Private m_CN As ADODB.Connection
    50.  
    51. Public Function GetDepartmentRS() As ADODB.Recordset
    52.     Dim rs As New ADODB.Recordset
    53.    
    54.     rs.Open "Select * from department", m_CN, adOpenStatic
    55.     Set GetDepartmentRS = rs.Clone
    56.     rs.Close
    57.     Set rs = Nothing
    58. End Function
    59.  
    60. Private Sub Class_Initialize()
    61.     Set m_CN = New ADODB.Connection
    62.     'change this to any other connection
    63.     m_CN.Open "provider=microsoft.jet.oledb.4.0;data source=c:\MyDB.mdb"
    64. End Sub
    65.  
    66.  
    67. Private Sub Class_Terminate()
    68.     If m_CN.State = adStateOpen Then m_CN.Close
    69.     Set m_CN = Nothing
    70. End Sub
    71.  
    72.  
    73. '=============CBusiness Class================
    74. Private m_objDBOper As CDB
    75.  
    76. Private Sub Class_Initialize()
    77.     Set m_objDBOper = New CDB
    78. End Sub
    79.  
    80. Private Sub Class_Terminate()
    81.     Set m_objDBOper = Nothing
    82. End Sub
    83. Public Function GetDepartmentData() As CDepartments
    84.     Dim objDept As CDepartment
    85.     Dim rs As New ADODB.Recordset
    86.     Dim colTemp As New CDepartments
    87.    
    88.     Set rs = m_objDBOper.GetDepartmentRS()
    89.    
    90.     Do Until rs.EOF
    91.         'prepare Department object
    92.         Set objDept = New CDepartment
    93.         With objDept
    94.             .DeptId = rs.Fields.Item("DeptId").Value
    95.             .DeptName = rs.Fields.Item("DeptName").Value
    96.         End With
    97.        
    98.         'add department to the collection
    99.         colTemp.Add objDept, CStr(rs.Fields.Item("DeptId").Value)
    100.         rs.MoveNext
    101.     Loop
    102.    
    103.     rs.Close
    104.     Set rs = Nothing
    105.    
    106.     Set GetDepartmentData = colTemp
    107. End Function
    108.  
    109.  
    110.  
    111.  
    112. '===========Form Code==================
    113. Private Sub Command1_Click()
    114.     Dim objDepts As CDepartments
    115.     Dim objBus As New CBusiness
    116.     Dim i As Integer
    117.    
    118.     Set objDepts = objBus.GetDepartmentData()
    119.    
    120.     For i = 1 To objDepts.Count
    121.         List1.AddItem objDepts.Item(CStr(i)).DeptName
    122.     Next
    123. End Sub
    I hope this explains enough

  10. #10

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Thanks, Serge, I'll play with this when I get home.
    "It's cold gin time again ..."

    Check out my website here.

  11. #11
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    I'm working on a large N-Tier application as we speak, and all of our data is structured in XML. XML is great and easy to work with. We populate our business objects using it. When our app talks with an outside datastore, we speak via web service calls. The only means of communication is through HTTP requests and sending and receiving XML.

  12. #12

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Since I looked at this some more, I realize that by using an object (in Serge's example, the CDepartment class) for the Item property of the Collection class (as Serge used the CDepartment object for the CDepartments' Item), each field that you want to return from a recordset can be coded as a property in the object's class. So what I initially thought was a limitation of the Collection is actually not.

    Thank you all for responding; I learned quite a bit from all this.
    "It's cold gin time again ..."

    Check out my website here.

  13. #13
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Great

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width