2013-01-17 26 views
0

请看看下面的代码:数据访问层和封装

'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的功能是公开的。这不会破坏封装规则吗?有没有解决的办法?在代码重用和封装之间似乎存在妥协。

回答

3

否 - 如果他们正在实现您的界面,则这些方法需要公开。如果你有一些内部工作GetAgeGetName,你不必公开允许,它会违反规则。

+0

感谢+ 1:“如果您有一些内部工作的GetAge或GetName,您不必公开允许”。我假设你的意思是直接访问访问,即dim objPerson As Person =新人objPerson.getAge() – w0051977