w0051977
w0051977

Reputation: 15807

Data Access Layer and Encapsulation

Please have a look at the code below:

'Form1.vb
Imports WindowsApplication1.BusinessLogicLayerShared

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim IPerson As IPerson
        IPerson = New BusinessLogicLayer.Person()
        Dim name As String = IPerson.getName()
    End Sub
End Class

'Person.vb
Imports WindowsApplication1.BusinessLogicLayerShared

Namespace BusinessLogicLayer
    Public Class Person
        Implements IPerson

        Private IPerson As DataLogicLayerShared.IPerson

        Public Function getName() As String Implements IPerson.getName
            IPerson = New DataLogicLayer.Person
            getName = IPerson.getName
        End Function
    End Class
End Namespace

Namespace BusinessLogicLayerShared
    Public Interface IPerson
        Function getName() As String
    End Interface
End Namespace

'Person.vb
Imports WindowsApplication1.DataLogicLayerShared
Namespace DataLogicLayer

    Public Class Person
        Implements IPerson

        Public Function getName() As String Implements IPerson.getName
            'Connect to database and get name
            Return "Ian"
        End Function

        Public Function getAge() Implements IPerson.getAge

        End Function
    End Class
End Namespace

Namespace DataLogicLayerShared
    Public Interface IPerson
        Function getName() As String
        Function getAge()
    End Interface
End Namespace

All the functions in DataLogicLayer.Person are public. Doesn't this break Encapsulation rules? Is there a way around this? There seems to be a compromise between code reuse and encapsulation.

Upvotes: 0

Views: 361

Answers (1)

Kye
Kye

Reputation: 6239

No - those methods need to be public if they are implementing your interface. It would break the rule if you had some internal workings of GetAge or GetName which you needlessly allowed to be public.

Upvotes: 3

Related Questions