Seeking advice on ASP.NET MVC model validation with data annotation attributes in VB.NET
So after some experimenting I was surprised to find that this is perfectly acceptable, in the sense that MVC does not complain about my protected inner abstract Metadata class, and the model validation still works:
Partial Domain Object, generated by a tool:
Partial Public Class SampleDomainObject
Private _id As Integer
Private _customCode As String
Private _description As String
Public Property ID() As Integer
Get
Return Me._id
End Get
Set(ByVal value As Integer)
Me._id = value
End Set
End Property
Public Property CustomCode() As String
Get
Return Me._customCode
End Get
Set(ByVal value As String)
Me._customCode = value
End Set
End Property
Public Property Description() As String
Get
Return Me._description
End Get
Set(ByVal value As String)
Me._description = value
End Set
开发者_运维百科End Property
End Class
Partial Domain Object, metadata implementation
<MetadataType(GetType(SampleDomainObject.Metadata))> _
Partial Public Class SampleDomainObject
Protected Friend MustInherit Class Metadata
<HiddenInput()> _
Public MustOverride Property ID() As Object
<Required(), _
StringLengthRange(4), _
DisplayName("Custom Code")> _
Public MustOverride Property CustomCode() As Object
<Required(), _
StringLength(255)> _
Public MustOverride Property Description() As Object
End Class
End Class
I did this because I didn't want to have to implement actual properties with backing fields, getters, and setters in VB.NET for the Metadata class to keep the maintenance to a minimum (I'm on .NET 3.5, no auto-properties for me).
What I'm worried about is that having a protected abstract inner class could be confusing to others using this domain object outside of MVC (my domain objects are part of a shared data access framework).
So my question is, in the world of MVC best practices, is this acceptable? Clever even? I'm an MVC newcomer, so I'll take any feedback I can get.
Thanks!
In Silverlight applications using RIA services, the metadata classes look very similar, minus the MustInherit. I suspect your practices follow suitably good practices, even outside of the context of MVC, considering that the behavior can be seen with MVC and domain services.
I asked a related question yesterday. The answer describes how this kind of a buddy class strategy works effectively in situations where the domain classes are code-generated. Buddy classes provide protection against code-gen-overwrites. (See Why are buddy classes used for validation?.)
Acceptable? For some. Clever? Nope.
Using a ViewModel pattern is much better. Your business/domain/core logic should not point outward to the view and UI layer. The view and UI layers should point into your business/domain/core logic.
Even without auto properties the cost of making view models is still really low and really improves the quality of your code.
Also unless your application is trivial in nature I'm sure you'll find scenarios where buddy classes just don't cut it. A good example is when there are different requirements for Add and Edit scenarios. User password fields comes to mind.
精彩评论