Get information from the upper class?

I do not know how to formulate the question correctly, so please bear with me ...

I have 3 classes: server, database and table. Each class has a Name property. As I want it to work, each server can have multiple databases, and each database can have multiple tables. Therefore, in the Server class, I have this property.

Private _databases As List(Of Database)
Public Property Databases() As List(Of Database)
    Get
        Return _databases
    End Get
    Set(ByVal value As List(Of Database))
        _databases = value
    End Set
End Property

And I have something similar in the Database class for tables. Now this works fine, because I can do something like this to get all the databases on the server.

For Each db In s.Databases  being the server object
        Debug.Print(db.Name)

    Next

I would like to expand these classes. I want the server class to handle all the connection information, and I would like other classes to use the connection information with the server class in them.

, . , serverclass.connectionstring . . ?

, .

Public Class Server

Private _name As String
Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property


Private _databases As List(Of Database)
Public Property Databases() As List(Of Database)
    Get
        Return _databases
    End Get
    Set(ByVal value As List(Of Database))
        _databases = value
    End Set
End Property
End Class

'-----New class  

Public Class Database

Private _name As String
Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property


Private _tables As List(Of Table)
Public Property Tables() As List(Of Table)
    Get
        Return _tables
    End Get
    Set(ByVal value As List(Of Table))
        _tables = value
    End Set
End Property

'This is where I need help!
Private Sub LoadTables () 
    dim connectionstring as string = server.connectionstring 'Possible?

    'Do database stuff
End Class

!

+3
3

"" .

( )

Server
----------
Databases   -----> Database
Name               ------------
                   Tables       -----> Table
                   Name                --------------
                                       Name

, :

Server
----------
Databases   -----> Database
Name               ------------
  ^                Tables       -----> Table
  |                Name                --------------
   --------------- Server              Name
                     ^                 Database
                     |-------------------|

Server Database Database Table .

+2

. .

+1

I would personally make a database of the server on which it is stored so that the database knows where to connect, depending on which server it is listed on.

It will look something like this:

public class Database {
    public Database(Server server) {
        Server = server;
    }

    public Server Server { get; private set; }
}
+1
source

Source: https://habr.com/ru/post/1748162/


All Articles