我会尽量简化。在我的解决方案中,我有3个独立的项目:
Main - where i use Bal
Bal - business logic, eg. classes:
Artikel
Material
Dal - data layer logic, eg. classes:
DALArtikel
DALMaterial
现在Dal的类实现了接口IDAL,如下所示:
Public Interface IDAL
Function isExist(name As String) As Boolean
Function GetIdByName(name As String) As Integer
Function GetNameById(pId As Integer) As String
End Interface
然后我可以从Bal的项目中调用界面的方法。每个BAL的课程都有它的DAL课程,就像Artikel的DALArtikel一样。
现在每个BAL的类都继承自一个类,如下所示。此Base类实现类似于上面提到的IGetInformation
的接口Public Class Base
Implements IGetInformation
Property Id As Integer
Property DAL As DataLayer.IDAL
Protected Sub New()
End Sub
Protected Sub New(dal As DataLayer.IDAL)
Me.DAL = dal
End Sub
Protected Sub New(pId As Integer)
_Id = pId
End Sub
Public Overridable Function IsExist(name As String) As Boolean Implements IGetInformation.IsExist
Return DAL.isExist(name)
End Function
Public Overridable Function GetNameById(pId As Integer) As String Implements IGetInformation.GetNameById
Return DAL.GetNameById(pId)
End Function
Public Overridable Function GetIdByName(pName As String) As Integer Implements IGetInformation.GetIdByName
Return DAL.GetIdByName(pName)
End Function
提到的界面:
Public Interface IGetInformation
Function isExist(name As String) As Boolean
Function GetIdByName(name As String) As Integer
Function GetNameById(pId As Integer) As String
End Interface
因此,像Artikel这样的每个Bal类的构建如下:
Public Class Artikel
Inherits Base
Property Serie As String
Property Nummer As String
Property Name As String
Sub New()
MyBase.New(New DALArtikel)
End Sub
Sub New(pId As Integer)
MyBase.New(New DALArtikel)
Id = pId
End Sub
Sub New(pId As Integer, pSerie As String)
MyBase.New(New DALArtikel)
Id = pId
Serie = pSerie
End Sub
这样我可以在Main项目中实现artikel类,并将其称为例如isExist方法,而不指定与之关联的特定DAL类,如在Artikel类构造函数中已经指定的那样。现在的问题是,当我想添加新方法时,我不得不在IDAL界面中实现这样的方法,我必须在Artikel中实现:
Public Function IsExistBarcode(barcode As String) As Boolean
Return New DataLayer.DALArtikel().CheckIfBarcodeExist(barcode)
End Function
所以这次我必须在调用CheckIfBarcodeExist之前指定DALArtikel,因为我的属性DAL不知道。
一般来说,我不喜欢当前的方式,你会看到我使用两个完全相同的内容界面来实现bal和dal的项目以及背后的逻辑。你知道其他有效的方法,我可以改变当前的逻辑,让我们说“更好”&#39 ;?
根据我的情况欣赏可能的改进示例。很抱歉很长的帖子,但无法让它更少。如果不明确的事情让我知道。