Should my viewmodel value type properties be nulla

2019-04-10 23:49发布

问题:

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?

回答1:

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.