简体   繁体   English

你在哪里放置验证逻辑?

[英]Where do you place your validation logic?

How would you structure your code in the following scenario: 在以下场景中,您将如何构建代码:

Several business objects (egPerson, House, etc), and between them, you need to validate the user inputs (which come in from textboxes). 几个业务对象(例如,People,House等),在它们之间,您需要验证用户输入(来自文本框)。 Would that go in either: 会是这样的:

  1. Each business object 每个业务对象
  2. The winforms codebehind winforms代码隐藏
  3. A seperate (static?) class. 一个单独的(静态?)类。

Thanks 谢谢

A very widely used approach, which I endorse in such scenarios, is to introduce the concept of a viewmodel: a class that aggregates all data to be displayed in a form, and which specifies (through attributes or some other mechanism) what type of validation should be performed on this data. 我在这种情况下认可的一种非常广泛使用的方法是引入视图模型的概念:聚合所有要在表单中显示的数据的类,并指定(通过属性或其他机制)什么类型的验证应该对这些数据进行处理。

This approach has several benefits which include: 这种方法有几个好处,包括:

  • Decoupling the validation logic from your models (perhaps there are validation scenarios which you want to enforce today but are not inherent in your data itself); 将验证逻辑与模型分离(可能存在您希望在今天强制执行但不是数据本身固有的验证方案); this way you are capable of defining distinct validation scenarios for distinct parts of your application where the same business objects come into play (eg in some part of the business logic each Person must have a Spouse, but having a Spouse is not an inherent property of every Person everywhere) 通过这种方式,您可以为应用程序的不同部分定义不同的验证方案,其中相同的业务对象发挥作用(例如,在业务逻辑的某些部分,每个人必须拥有配偶,但拥有配偶不是固有属性每个人到处都是)
  • Decoupling the validation from the presentation logic (your view's codebehind); 将验证与表示逻辑(您的视图的代码隐藏)分离; this way you are not forced to specifically tangle your presentation behavior with business object validation 这样,您就不会被迫通过业务对象验证专门纠缠您的演示行为
  • Validation code being isolated and each part of it targets a specific type of validation only; 验证代码被隔离,其中每个部分仅针对特定类型的验证; this way, validation code can be reused across your application wherever it is applicable 这样,验证码可以在您的应用程序中重复使用,只要它适用

The actual code that does the validation will typically be inside a separate validation class; 执行验证的实际代码通常位于单独的验证类中; your viewmodel would only dictate how each piece of validation should apply to each piece of data. 您的viewmodel只会指示每个验证应如何应用于每个数据。

Each business object. 每个业务对象。 I have classically made each one implement an IValidator interface that spits out all validation errors for the object. 我经典地让每个人实现一个IValidator接口,该接口为对象吐出所有验证错误。

In the business objects . 业务对象中 The reason is that you would like your validation rules to be testable. 原因是您希望验证规则可以测试。 If the rules are important you will want to cover them with tests, if they are not - Don't implement them in the first place. 如果规则很重要,你会想要用测试来覆盖它们,如果它们不是 - 不要首先实现它们。 Taking this to the extreme gives you code where the codebehind is extremely thin and does hardly have any logic in them. 将这种情况发挥到极致可以为代码提供极其薄弱的代码,并且几乎没有任何逻辑。 This is something to desire. 这是需要的东西。

Some good practise here: http://colinjack.blogspot.com/2008/03/domain-model-validation.html 这里有一些好的做法: http//colinjack.blogspot.com/2008/03/domain-model-validation.html

Once in place, in the domain model, the UI should be able to reflect the validation state/output from the model. 一旦到位,在域模型中,UI应该能够反映模型的验证状态/输出。

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

相关问题 你在哪里存储验证逻辑? - Where do you store validation logic? 如果您被迫使用Anemic域模型,那么您将业务逻辑和计算字段放在何处? - If you are forced to use an Anemic domain model, where do you put your business logic and calculated fields? 如何使用Linq-2-Sql将域逻辑与DB / Persistence Logic分开? - How do you keep your Domain Logic seperate from DB/Persistence Logic with Linq-2-Sql? 如何从ViewModel中分离ViewModel属性验证? - How do you decouple your ViewModel properties validation from ViewModel? ServiceStack,在哪里放置业务逻辑? - ServiceStack, where to place business logic? MVVM:在何处放置某些逻辑 - MVVM: Where to place certain logic 应用程序中验证的最佳位置在哪里? 经验法则? - Where is the best place in an app to do validation? Rules of thumb? 你在哪里把SQL语句放在你的c#项目中? - Where do you put SQL Statements in your c# projects? C#,Ninject:你把内核和模块放在哪里? - C#, Ninject: Where do you put the kernel and your modules? NInject:你在哪里保留对内核的引用? - NInject: Where do you keep your reference to the Kernel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM