如果我的视图模型值类型属性可为空? [英] Should my viewmodel value type properties be nullable?

查看:102
本文介绍了如果我的视图模型值类型属性可为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经让我的视图模型属性现在可空的很长一段时间。我的理由是,当一个验证发生,我不想插入到用户留空的字段的默认值,但需要。

我记住我与时需提供的字段,当然,不过这让我来想,我对这样失去保真大量的对象模型。

当然,我的领域类只可为空时,他们其实是可以为空。

如果当域模型需要他们我的视图模型的属性可以为空?


解决方案

  

如果当域模型需要他们我的视图模型的属性可以为空?


是的,他们应该以正确的视图模型执行验证。当你确保该视图模型是有效的,这种映射视图模型回在映射层的实际域模型,你将是肯定的值将为这个属性来提供。

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.

这篇关于如果我的视图模型值类型属性可为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆