如何注入用于.NET MVC3验证依赖呢? [英] How to inject dependencies used for validation in .NET MVC3?

查看:120
本文介绍了如何注入用于.NET MVC3验证依赖呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有相当需要访问库/数据库做好自己的工作了几验证方法。到目前为止,我们已经使用了服务定位器模式(尽管有节制地),以自定义ValidationAttributes做到这一点:

 公众覆盖BOOL的IsValid(对象的值)
{
    //使用自定义的服务定位在我们的应用程序的基础设施
    VAR回购= DependencyInjector.Current.GetService< IXyzRepository>();
    ...
}

我知道这是在:(作为一种反模式,我们想用一个更正确的做法。我们用团结,我读的这个帖子,上面写着使用构造方法。然而中的链接接受的答案说,该文件已过期(退役内容)。

该解决方案并不需要使用验证属性,我想它可以使用IValidatableObject,但问题依然存在:如何注入依赖到模型中。我们是否需要一个自定义的模型绑定做到这一点?

另一种解决方案是在控制器,其中,依赖注入容易执行验证。对我来说,这虽然感到混乱。我想该模型通过它获取到action方法的时间来验证。

此外,我们有时会用[RemoteAttribute]执行一些客户端上的这些验证的。目前,这些方法用静态方法Validator.TryValidateObject构造一个视图模型和委托检验的模型。

你是如何完成的,需要注入的依赖来完成工作验证,而不使用SL反模式?


解决方案

  

你是如何完成的,需要注入验证
  依赖来完成工作,而无需使用SL反模式?


我使用 FluentValidation.NET 在我的应用程序执行验证。它可以让我<一个href=\"http://fluentvalidation.$c$cplex.com/wikipage?title=ValidatorFactory&referringTitle=Documentation\">inject 的依赖关系到我的验证。它有一个非常好的<一个href=\"http://fluentvalidation.$c$cplex.com/wikipage?title=mvc&referringTitle=Documentation\">integration在ASP.NET MVC 。它还支持自动客户端验证标准规则只是方法一样使用jQuery不显眼的验证数据的注释:


  • NOTNULL / NotEmpty

  • 匹配(正则表达式)

  • InclusiveBetween(范围)

  • 信用卡式

  • 电子邮件

  • EqualTo(交叉财产平等的比较)

  • 长度

我从来没有使用数据的注释进行验证。他们是绝对没用,当你需要处理一些更加的复杂的,你需要验证相关的属性,甚至使用一些服务验证方案。我把复杂的斜体在previous一句,因为我不认为<一href=\"http://stackoverflow.com/questions/8906228/how-to-validate-one-field-related-to-anothers-value-in-asp-net-mvc-3/8906529#8906529\">validating 需要2个属性,一个是一个非常复杂的验证方案,然而,刚刚签你为了使用数据注释来实现它写的基础设施垃圾量。望着这code,你已经不知道你是什么验证

We have quite a few validation methods that need to access repositories / database to do their work. So far we have been using the service locator pattern (albeit sparingly) to accomplish this in custom ValidationAttributes:

public override bool IsValid(object value)
{
    // use custom service locator in our app's infrastructure
    var repos = DependencyInjector.Current.GetService<IXyzRepository>();
    ...
}

I know this is :( upon as an anti-pattern, and we would like to use a more correct approach. We use unity, and I read this post that says to use a build-up method. However the link in the accepted answer says that the documentation is outdated (retired content).

The solution does not need to use a validation attribute, I suppose it could use IValidatableObject, however the problem remains: how to inject the dependency into the model. Do we need a custom model binder to do this?

Another solution would be to perform the validation in the controller, where dependency injection is easy. To me this feels cluttered though. I would like the model to be validated by the time it gets to the action method.

Also we sometimes use [RemoteAttribute] to perform some of these validations on the client. Currently those methods construct a viewmodel and delegate validation to the model by using the static Validator.TryValidateObject method.

How have you accomplished validation that requires an injected dependency to do its work, without using the SL anti-pattern?

解决方案

How have you accomplished validation that requires an injected dependency to do its work, without using the SL anti-pattern?

I use FluentValidation.NET to perform validation in my applications. It allows me to inject dependencies into my validators. It has a really nice integration with ASP.NET MVC. It also supports automatic client side validation for the standard rules just the same way as data annotations using jquery unobtrusive validate:

  • NotNull/NotEmpty
  • Matches (regex)
  • InclusiveBetween (range)
  • CreditCard
  • Email
  • EqualTo (cross-property equality comparison)
  • Length

I have never used data annotations to perform validation. They are absolutely useless when you need to handle some more complex validation scenarios where you need to validate dependent properties and even use some service. I put complex in italics in the previous sentence because, I don't think that validating that one of the 2 properties is required is a really complex validation scenario and yet, just checkout the amount of infrastructure crap you have to write in order to implement it using data annotations. Looking at this code you no longer know what you are validating.

这篇关于如何注入用于.NET MVC3验证依赖呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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