This project is read-only.

DataAnnotations: on ContentPart model vs record class?

Topics: General, Writing modules
Feb 29, 2012 at 10:26 PM
Edited Feb 29, 2012 at 10:29 PM

In some Orchard code, for example AutoroutePartRecord, has [StringLength(2048)] on some properties. But the AutoroutePart model doesn't have any annotations. What are the annotations on the ...PartRecord class used for?

I created MyPart, and MyPartRecord classes, with a property "Name". I added [Required] to .Name in MyPartRecord. Dashboard tells me I saved successfully if I leave name blank, so that annotation doesn't seem to drive editor view validation messages. 

But if I put the annotation on the MyPart.Name, Dashboard gives a validation message if I try to save a part without a value for Name. This is as expected. 

Just wondering in general when to use annotations on the part model vs the record?

Feb 29, 2012 at 10:36 PM

I always add them to parts (not records). This way it works with the default setup as you experienced (if the attributes are on the record they can still work somehow simply, but I can't remember how I achieved it) and IMO they also should be on the part, since the part is effectively the view model used in the editors.

Mar 1, 2012 at 5:39 PM

The record will drive the database properties. For instance on Migrations code generation. If the Record is also used as a ViewModel then the annotations will be used for validation. Sometimes it's just some dead code which has been forgotten,