I've been making my viewmodel properties nullable for quite some time now. My reasoning is that when a validation occurs, I don't want default values inserted into the fields that the user left empty, but are required.
I mark my required fields with required, for course, but this got me to thinking that i'm losing a great deal of fidelity in the object model by doing this.
Of course my domain classes are only nullable when they can actually be null.
Should my viewmodel properties be nullable when the domain model requires them?
Should my viewmodel properties be nullable when the domain model requires them?
Yes, they should in order to properly perform validation on the view model. When you ensure that the view model is valid and map this view model back to the actual domain model in your mapping layer you will be certain that a value would be provided for this property.
来源:https://stackoverflow.com/questions/7210548/should-my-viewmodel-value-type-properties-be-nullable