简体   繁体   English

对于模型是必需的,但最初为View - MVVM的空字段

[英]Required for model but initially empty field for View - MVVM

please help me with this: 请在这件事上给予我帮助:

I have model with property of Enum type. 我有Enum类型属性的模型。 This property is required, but on View initial value must be empty. 此属性是必需的,但在View初始值必须为空。 Will show watermark.. 会显示水印..

If I understand correct than model will have not nullable property. 如果我理解正确,那么模型将不具有可空的属性。 But viewModel will have nullable. 但是viewModel将具有可空性。 Is this right way? 这是对的吗?

Looks like in this case: 在这种情况下看起来像:

  • need create viewModel 需要创建viewModel
  • viewModel and model will have different data sometimes viewModel和model有时会有不同的数据
  • viewModel need logic for set and get correct values to/from model or get null in first time and leave as is model viewModel需要设置逻辑并从模型中获取正确的值或在第一次获取null并保留为模型
  • viewModel need one more validation rule viewModel还需要一个验证规则

Other way is: 其他方式是:

Model will have nullable property and required validation: Model将具有可空属性并需要验证:

  • can bind to model 可以绑定到模型
  • model does not "correspond to reality" 模型不“对应现实”
  • we need always expose data from nullable enum and forgot about null? 我们需要始终从可空的枚举中公开数据并忘记null吗? or handle it (convert to DTO or something like this) 或处理它(转换为DTO或类似的东西)
  • nullable property is only UI requirement and can be changed in future 可空属性只是UI要求,将来可以更改

Having the (domain) model use a non-nullable enum suggest that the value MUST have a valid value for use case and / or data integrity. 让(域)模型使用不可为空的枚举表明该值必须具有用例和/或数据完整性的有效值。

Having the requirement on the UI to allow a non-valid value (initial or otherwise) will result in 1 of 2 things: 在UI上要求允许无效值(初始或其他)将导致以下两件事之一:

When the View posts a null value (via a dedicated ViewModel) and checks are performed to ensure its not null (I assume that the user selects the "Please Select" value from a select box) the server will respond with an error message saying something like "Please select a valid value blah blah". 当View发布一个空值(通过一个专用的ViewModel)并执行检查以确保它不为null(我假设用户从选择框中选择“Please Select”值)服务器将响应一条错误消息说某事比如“请选择一个有效的值等等等等”。 Or, the server will allow the null from the View Model and default the domain model property to some predetermined sensible value. 或者,服务器将允许来自视图模型的空值,并将域模型属性默认为某个预定的合理值。

In my experience this kind of setup leads to a frustrating user experience because having the UI present an invalid selection option that only feedsback upon submit (or using some frontend / client validation) is rather annoying (especially on a long form, Etc). 根据我的经验,这种设置会导致令人沮丧的用户体验,因为让UI呈现无效的选择选项,仅在提交时进行反馈(或使用一些前端/客户端验证)是相当烦人的(特别是在长形式,等等)。

Perhaps you would be better rethinking that requirement so the UI only presents valid enum values for selection and optionally make the default one a sensible value. 也许您最好重新考虑该要求,以便UI仅为选择提供有效的枚举值,并可选择使默认值成为合理的值。

Having said that if you really want the UI to not have a valid initial value then make a view model with a nullable version and validate on the server as you suggest. 话虽如此,如果你真的希望UI没有有效的初始值,那么使用可以为空的版本创建一个视图模型,并按照你的建议在服务器上进行验证。 As far as changing the (domain) model with a nullable, you already seem concerned that it doesn't "correspond to reality" which is a good impulse considering you already mentioned that this is a UI only requirement, thus place the detail in that layer and keep your domain modals as close to reality as is needed to satisfy your use cases and / or business requirements. 至于用可空的方式改变(域)模型,你已经开始担心它不会“对应于现实”这是一个很好的冲动,考虑到你已经提到过这是一个只有UI的要求,因此将细节置于其中分层并保持您的域模态尽可能接近现实,以满足您的用例和/或业务需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM