引用变量包含命名空间 [英] Reference variables contain namespace
问题描述
我试图按照这篇文章的回答者的建议: DataAccess项目中的类的命名约定是什么?(jdk)。
I am trying to follow the advice of the answerer of this post: What's the naming convention for classes in the DataAccess Project? (jdk).
请看下面的代码: p>
Please take 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
The client (form) calls the business logic layer and the business logic layer calls the data logic layer. The name (String) is passed from the data logic layer to the business logic layer and back to the client.
我不喜欢在引用接口时我必须指定命名空间,例如私人IPerson作为DataLogicLayerShared.IPerson。我应该在引用中指定命名空间,还是可以修改模式以避免这种情况?
I don't like the fact that when referencing a interface I have to specify the namespace e.g. Private IPerson As DataLogicLayerShared.IPerson. Should I specify the namespace in the reference or can the pattern I have adopted be amended to avoid this?
推荐答案
你应该能够在源文件的顶部添加导入DataLogicLayerShared
。这将使您不必完全限定每个类与名称空间。
You should be able to add Imports DataLogicLayerShared
at the top of your source file. This will keep you from having to fully qualify each class with the namespace.
您可以了解有关VB .NET引用和命名空间的更多信息 here
You can learn more about VB .NET references and namespaces here
更新:如果您有多个具有相同名称的类或接口,在不同的命名空间中,您将必须通过在上面添加名称空间来限定您正在使用的类。
Update: If you have multiple classes or interfaces with the same name, in different namespaces, you will have to qualify which class you are using by adding the namespace before it as in your example above.
在您的情况下,您可能不需要在同一个源文件中包含业务层和数据层类。您的代码应该调用服务(业务逻辑)层,而层又调用数据层。
In your case, you probably don't need to include business layer and data layer classes in the same source file. Your code should call the services (business logic) layer, which in turn calls the data layer.
这篇关于引用变量包含命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!