简体   繁体   English

数据注释和MVC 1:1 ViewModel

[英]Data Annotations and MVC 1:1 ViewModel

When developing in ASP.NET MVC I use a 1:1 ViewModel setup where the viewmodel contains only the data required by the view. 在ASP.NET MVC中进行开发时,我使用1:1 ViewModel设置,其中viewmodel仅包含视图所需的数据。 I'm also using data annotations to validate on the viewmodel. 我也在使用数据注释在viewmodel上进行验证。

My concern is that this is not following the DRY principle as I find myself having to duplicate validation throughout my sites. 我担心这没有遵循DRY原则,因为我发现自己不得不在整个站点中重复进行验证。 I'd love to have validation in one place (my domain model) but because I never send the domain model to the view model this is not possible. 我很想在一个地方(我的域模型)进行验证,但是因为我从未将域模型发送到视图模型,所以这是不可能的。

I was wondering if anyone else has ran into this issue and found a suitable method of working around it or has a better solution? 我想知道是否还有其他人遇到过此问题并找到了解决该问题的合适方法,或者有更好的解决方案?

We had similar concerns when we adopted a 1:1 ViewModel approach. 当我们采用1:1 ViewModel方法时,我们也有类似的担忧。 We found that 1) the duplication is less than appears at first and 2) that the overall maintainability and power of the application is improved enough that any non-DRY costs are worth the price. 我们发现1)重复操作比开始时少,并且2)应用程序的整体可维护性和功能得到了足够的改进,以至于任何非DRY成本都值得。

We ended up thinking about validation for the ViewModel and domain models from different perspectives (separation of concerns). 我们最终从不同的角度(关注点分离)考虑了ViewModel和域模型的验证。 When implementing validation at the domain level model we're looking for comprehensive security to prevent attacks. 在域级模型上实施验证时,我们正在寻求全面的安全性来防止攻击。 We're also detecting serious "contract" violations that indicate bugs in the upper layers. 我们还检测到严重的“合同”违规行为,这些违规行为表明上层存在错误。 When implementing validation at the view model we're strictly concerned with usability. 在视图模型上执行验证时,我们严格考虑可用性。

If you approach the models from these different perspectives, there's less overlap then you might think. 如果您从这些不同的角度来研究模型,那么您可能会想不到重叠的地方。 The domain layer might allow certain operations that you want to restrict in the ViewModel for usability reasons. 由于可用性的原因,域层可能允许您希望在ViewModel中限制某些操作。 Or the ViewModel might assume that only inputs for the rendered view are posted, relying on the domain model for comprehensive security validation designed to prevent attacks. 或者,ViewModel可能假定仅发布用于呈现视图的输入,而依赖于域模型进行全面的安全性验证,旨在防止攻击。

1:1 ViewModels also greatly ease testability. 1:1 ViewModels也大大简化了可测试性。 Tests for the ViewModel can be written independently of the domain layer which makes unit tests lighter, more maintainable and in general more useful. 可以独立于域层编写ViewModel的测试,这使单元测试更轻便,更易于维护并且通常更有用。 Likewise, you can test the domain model without the mental contortions of how inputs flow through the user interface. 同样,您可以测试域模型,而不必担心输入如何通过用户界面流动。 This also adheres to the "defense in depth" security principle. 这也符合“纵深防御”安全原则。

We've also noticed that Views change much more frequently than the domain layer. 我们还注意到,视图的更改比域层更频繁。 There's a comfort in knowing that you've got really good security unit test coverage on your domain model and when you implement a new View, you only need to worry about validation from a usability perspective. 很高兴知道您的域模型具有非常好的安全性单元测试范围,并且当您实现新的View时,您只需要担心可用性方面的验证。

Lastly, if you anticipate having a web services layer, you'll find the separation of domain and view models pays dividends. 最后,如果您期望拥有Web服务层,则会发现域的分离和视图模型带来了好处。 Without it, the domain model inevitably gets written to support the UI. 没有它,域模型将不可避免地被编写来支持UI。 When you layer on the service interface you'll find there's an impedance mismatch. 在服务接口上分层时,您会发现阻抗不匹配。

If there does end up being significant duplication, you can always factor the validation out separately. 如果最终确实存在大量重复,则始终可以单独进行验证。 In our experience, that hasn't been worth it for all the reasons discussed above. 根据我们的经验,由于上述所有原因,这并不值得。

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

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