我正在查看Business Layer中的一些代码,如下所示:
Public Class Member
Inherits BusinessObject
Public Sub New()
' establish business rules
AddRule(New ValidateId("MemberId"))
AddRule(New ValidateRequired("Email"))
AddRule(New ValidateLength("Email", 1, 100))
AddRule(New ValidateEmail("Email"))
AddRule(New ValidateRequired("CompanyName"))
AddRule(New ValidateLength("CompanyName", 1, 40))
AddRule(New ValidateRequired("City"))
AddRule(New ValidateLength("City", 1, 15))
AddRule(New ValidateRequired("Country"))
AddRule(New ValidateLength("Country", 1, 15))
End Sub
Public Property MemberId() As Integer
Public Property Email() As String
Public Property CompanyName() As String
Public Property City() As String
Public Property Country() As String
Public Property NumOrders() As Integer
Public Property LastOrderDate() As Date
End Class
所有Business Objects看起来都像上面那样,即没有方法。然后有BusinessRule类,如下所示:
Public MustInherit Class BusinessRule
Public Property [Property]() As String
Public Property [Error]() As String
Public Sub New(ByVal [property] As String)
Me.Property = [property]
[Error] = [property] & " is not valid"
End Sub
Public Sub New(ByVal [property] As String, ByVal [error] As String)
Me.New([property])
Me.Error = [error]
End Sub
' validation method. To be implemented in derived classes
Public MustOverride Function Validate(ByVal businessObject As BusinessObject) As Boolean
' gets value for given business object's property using reflection
Protected Function GetPropertyValue(ByVal businessObject As BusinessObject) As Object
' note: reflection is relatively slow
Return businessObject.GetType().GetProperty([Property]).GetValue(businessObject, Nothing)
End Function
End Class
End Namespace
这是贫血模型还是域模型?它看起来很贫血,因为状态和行为似乎是分开的。但是,它在线描述为域模型。