数据访问层和封装

时间:2013-01-17 11:21:44

标签: vb.net design-patterns

请查看以下代码:

'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

DataLogicLayer.Person中的所有函数都是公共的。这不会破坏封装规则吗?有没有解决的办法?代码重用和封装之间似乎存在折衷。

1 个答案:

答案 0 :(得分:3)

不 - 如果他们正在实施您的界面,那些方法需要公开。如果你有一些GetAgeGetName的内部运作方式会让你不必要地被公开,那么它就会违反规则。