简体   繁体   English

如何在不重复逻辑的情况下使用 MVC 和 WCF 进行客户端验证?

[英]How can I approach client-side validation with MVC and WCF without duplicating logic?

I may be looking for a non-existent holy grail here, but it's worth a shot.我可能在这里寻找一个不存在的圣杯,但值得一试。 For starters, here's a quick overview of our architecture:对于初学者,这里是我们架构的快速概述:

  • Data Access : Repository classes that interact with SQL Server via Entity Framework数据访问:通过实体框架与 SQL 服务器交互的存储库类
  • Business Logic : Manager classes invoke the data layer and map the data to Domain Models业务逻辑:管理器类调用数据层和 map 数据到域模型
  • Domain Models : POCOs that represent our domain领域模型:代表我们领域的 POCO
  • Service Library/Service Facade : Exposes CRUD operations for the POCOs服务库/服务外观:公开 POCO 的 CRUD 操作
  • Presentation : ASP.NET MVC (v2, but could be moved to v3 if needed -- we're still early in our project)演示文稿:ASP.NET MVC(v2,但如果需要可以移至 v3——我们的项目还处于早期阶段)

The problem we're looking to solve is how to automatically create client-side validation to handle basic issues like required fields, min and max length, numeric ranges, etc. -- just your basic first-line-of-defense stuff.我们要解决的问题是如何自动创建客户端验证来处理必填字段、最小和最大长度、数字范围等基本问题——这只是你基本的第一道防线的东西。

If we were to use DataAnnotations on the Domain Model POCOs (which sounds appealing at first), we could let jQuery's unobtrusive validation do the work for us.如果我们要在域 Model POCO 上使用 DataAnnotations(一开始听起来很吸引人),我们可以让 jQuery 的不显眼的验证为我们完成工作。 To make it work, we would have to reference the Domain Model library in both the Service and Presentation layers because DataAnnotations don't get passed over WCF.为了使它工作,我们必须在服务层和表示层中引用域 Model 库,因为 DataAnnotations 不会通过 WCF 传递。 Unfortunately, we need to re-use the WCF service in several application and if we went that route, we'd be likely to create version-lock issues.不幸的是,我们需要在多个应用程序中重新使用 WCF 服务,如果我们走这条路,我们很可能会产生版本锁定问题。

So we can't reference the Domain Model on both sides of the service boundary and we don't want to move the definition of the validation rules to the presentation layer, because future apps may consume the same services and validation will be needed there as well.所以我们不能在服务边界的两边引用域 Model 并且我们不想将验证规则的定义移动到表示层,因为未来的应用程序可能会使用相同的服务并且需要在那里进行验证出色地。

That leaves us looking for another way to pass validation rules (or validation metadata, if you prefer) that is defined with the Domain Models over WCF to the client app.这让我们寻找另一种方法来将使用域模型定义的验证规则(或验证元数据,如果您愿意)传递给客户端应用程序。

I know it sounds like we want to have our cake and eat it, too.我知道听起来我们也想吃蛋糕。 If there isn't a reasonable solution, we'll bite the bullet and duplicate validation logic.如果没有合理的解决方案,我们将硬着头皮重复验证逻辑。 I think that's more desirable than tightly coupling our application tiers.我认为这比紧密耦合我们的应用程序层更可取。

Given the scenario above, how would you handle client-side validation and still avoid duplicating logic?鉴于上述情况,您将如何处理客户端验证并仍然避免重复逻辑?

EDIT:编辑:

Thanks for the thoughts so far.感谢到目前为止的想法。 There's one more aspect to this that I realized I forgot to include when talking about DataAnnotations: We looked into using reflection to obtain the annotations and return them via a method call to a separate service, but that won't work because they aren't marked as Serializable and therefore can't be returned over WCF.还有一个方面,我意识到在谈论 DataAnnotations 时我忘了包括:我们研究了使用反射来获取注释并通过对单独服务的方法调用将它们返回,但这不起作用,因为它们不是标记为可序列化,因此不能通过 WCF 返回。

WCF doesn't deal with client-side validation, because it can't know the capabilities of the client on the other end of the service. WCF 不处理客户端验证,因为它无法知道服务另一端客户端的能力。 If you want to do something like this you're either going to need to:如果你想做这样的事情,你要么需要:

  1. Write extra functions into your WCF service that give your clients a way to request the validation rules in some format and then implement them using some custom code.在您的 WCF 服务中编写额外的功能,让您的客户可以以某种格式请求验证规则,然后使用一些自定义代码实现它们。

  2. Your client will need to implement its own validation logic.您的客户端将需要实现自己的验证逻辑。

It would be a killer feature if WCF could pass validation rules over to the client like you want, but it just can't.如果 WCF 可以像您想要的那样将验证规则传递给客户端,这将是一个杀手级功能,但它不能。 :( :(

An answer I provided the other day to another question may help you:我前几天提供的另一个问题的答案可能会对您有所帮助:

What is better way to validate business rules in ASP.NET MVC application with 3 layer architecture? 在具有 3 层架构的 ASP.NET MVC 应用程序中验证业务规则的更好方法是什么?

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

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