用数据说明自定义验证 [英] Custom validation with Data annotations

查看:73
本文介绍了用数据说明自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用数据说明检查的被输入的数据,但我坚持,当涉及到更多的自定义的方式来验证数据。

I'm using data annotations to check data that's being entered, but I'm stuck when it comes to more custom way to validate data.

我需要运行对数据库查询,看看是否存在的东西有或没有,然后报告给用户,如果自定义DB-检查错误出现,比如公司名称已经存在

I need to run queries against database to see if stuff exists there or not, and then report back to user if a "custom db-check error" appears, such as "The Companyname already exists"

我如何与dataannotations一起实现这样的事情?

How can I implement such a thing together with dataannotations?

我所有的查询使用LINQ和Entity Framework随3.5SP1完成等。

I have all the queries done etc using linq and entity framework that comes with 3.5sp1

/ M

推荐答案

您必须编写您自己的属性,会做你的对象实例验证针对数据存储。

Custom attributes that extend data annotations

You will have to write your own attributes that will do the validation of your object instance against data store.

请确保您的类继承 System.ComponentModel.DataAnnotations.ValidationAttribute 类:

public class MustNotExist: ValidationAttribute
{
    ...
}

注意

当我需要验证的对象是数据存储中是唯一的,我碰到了类似的情况。但是,这种验证是不可能对实体类,因为它应该只对正在创建而不是当你从数据存储已经回到你的实体的实体工作。

Caution

I've run into a similar situation when I needed to validate that the object is unique within data store. But this kind of validation wasn't possible on the entity class itself, since it should only work for those entities that are being created but not when you return your entity from the data store already.

我的解决办法是有一个单独的接口,类和属性。

My solution was to have a separate interface, class and attribute.

public interface IExternalValidator ...

class DBUniqueValidator: IExternalValidator ...

class ValidateExternallyAttribute: FilterAttribute, IActionFilter
{
    ...
    public ValidateExternallyAttribute(Type validatorType, Type entityType) ...
    ...
}

我能够把我的属性上得到实体参数的控制器动作。筛选器操作属性,然后检查控制器操作的参数(它可以很容易地访问他们的类型和值)和(在属性定义提供类型)违背正确的参数外部验证,并在验证失败时填充的ModelState错误。

I was able to place my attribute on controller actions that get entity parameters. Filter action attribute then checks controller action parameters (it can easily access their types and values) and runs external validator against correct parameters (provided types in attribute definition) and populates ModelState errors when validation fails.

[ValidateExternally(typeof(DBUniqueValidator), typeof(User))]
public ActionResult RegisterUser(User newUser)
{
    if (!this.ModelState.IsValid)
    {
        // act accordingly - probably return some error depending on model state errors
    }
    // register new user in data store
}

这样我可以只在真正需要它这些动作运行外部验证,这种技术也帮助我的控制器动作code,以保持清洁和短。所有我必须做的是检查是否有任何模型状态的错误。

This way I was able to run external validation only on those actions that actually needed it, and this technique also helped my controller actions code to stay clean and short. All I had to do is to check if there are any model state errors.

这篇关于用数据说明自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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