Results 1 to 2 of 2

Thread: Relationships in Data Access Layer

  1. #1

    Thread Starter
    PowerPoster Nitesh's Avatar
    Join Date
    Mar 2007
    Location
    Death Valley
    Posts
    2,556

    Relationships in Data Access Layer

    Hi Guys,

    I have 3 tables. One is called Sport(SportID, Description,Active)
    sample data:

    1 Soccer 1
    2 Cricket 1

    The second table is called Teams(TeamID, Description, Active)
    sample data:
    1 Under 14A 1
    2 First XI 1

    Since soccer and cricket can have an Under 14A team I have a table called Sport_Teams(ID, SportID(FK), TeamID(FK)) that allows me to have the many to many relationship.
    sample data:
    1 1(Soccer) 1(Under 14A)
    2 1(Soccer) 2(Fisrt XI)
    3 2(Cricket) 1(Under 14A)

    My DataAccessLayer returns datatables 90% of the time.

    Code:
    #Region "Sports"
            Public Function GetSports() As DataTable
    
                Using con As New MySqlConnection(strCon)
    
                    Dim cmd As MySqlCommand = New MySqlCommand("SELECT * FROM tblSport WHERE (iactive=1) ORDER BY descr;", con)
    
                    con.Open()
    
                    Dim reader As MySqlDataReader = cmd.ExecuteReader
    
                    _data.Load(reader)
    
                    con.Close()
                    reader.Close()
                    cmd.Dispose()
    
                    Return _data
                End Using
            End Function
    Similarly I have a function to return a datatable of teams.

    Then I have a class called Sports that gets the data from the DAL.

    Code:
    Public Class Sports
            Inherits Generic
    #Region "Constructors"
            Public Sub New()
                MyBase.New()
            End Sub
            Public Sub New(ByVal SportID As Long, ByVal Description As String, ByVal Active As Boolean?)
                MyBase.New(SportID, Description, Active)
            End Sub
    #End Region
    #Region "Methods"
            Public Shared Function GetSports() As DataTable
                Dim DAL As New MySchoolDataAccessLayer
                Dim dt As DataTable = DAL.GetSports()
    
                Return dt
            End Function
    I have a smilar class to retrieve teams information

    Finally my BLL looks like this. It interacts with the above class

    Code:
    Public Property AllSports() As DataTable
                Get
                    Return m_SportTable
                End Get
                Set(ByVal value As DataTable)
                    m_SportTable = value
                End Set
            End Property
    
            Public Function GetSports() As DataTable
    
                Me.AllSports = Sports.GetSports
    
                Return Me.AllSports
            End Function
    Now I need to write a class Called Sport_Teams. I've gotten this far

    Code:
    Public Class Sport_Teams
            Private m_ID As Long
            Private m_Sport As Sports
            Private m_Teams As Teams
            Private m_Active As Boolean?
    
            Public Sub New()
                m_ID = 0
                m_Sport = Nothing
                m_Teams = Nothing
                m_Active = False
            End Sub
        End Class
    I want to return Sport and Teams as types of Sport and Team. What is the best way to do this. I always get confused with relationships in my classes. Sonce sportid and teamid are foregn keys in Sport_Teams I don't know how to return their respective types in my sports_team class
    Last edited by Nitesh; Jun 26th, 2012 at 05:52 AM.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Relationships in Data Access Layer

    Wouldn't it be easier to use strongly typed data tables, or another ORM that creates the relationships for you?
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

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