简体   繁体   English

什么是更好的验证方式/地点?

[英]What is the better way/place for validation?

In my asp.net mvc application i have service layer, which operated with business object, pass its to repository layer and return to controller. 在我的asp.net mvc应用程序中,我具有服务层,该层与业务对象一起运行,将其传递到存储库层并返回到控制器。 No i can't decide where i need to validate object. 不,我无法决定需要在哪里验证对象。 First place - use data annotation validation with attribute of component model annotation in business objects class, for example: 第一名-在业务对象类中将数据注释验证与组件模型注释的属性一起使用,例如:

[AcceptVerbs("POST")]
    public ActionResult Edit(Source src)
    {
        if(!ModelState.IsValid){            
            return View("EditSource", src);

        _sourceService.SaveSource(src);

        return RedirectToAction("Index");
    }

[MetadataType(typeof(Source.MetaSource))]
public class Source
{
    private class MetaSource
    {
        [Required]
        public string Name { set; get; }
        [Required]
        public string Url { set; get; }
    }

    public int? ID { set; get; }
    public string Name { set; get; }
    public string Url { set; get; }

    public Source()
    {
        ID = null;
    }

Second way - validate objects in service layer, by passing validation dictionary to service layer, for example: 第二种方法-通过将验证字典传递到服务层来验证服务层中的对象,例如:

 [AcceptVerbs("POST")]
    public ActionResult Edit(Source src)
    {
        if (!_sourceService.ValidateSource(src)){           
            return View("EditSource", src);

        _sourceService.SaveSource(src);

        return RedirectToAction("Index");
    }

public bool ValidateSource(Source srcToValidate)
    {
        if (string.IsNullOrEmpty(srcToValidate.Name))
            _validationDictionary.AddError("Name", "Name is required.");
        else
            if (srcToValidate.Name.Trim().Length == 0)
                _validationDictionary.AddError("Name", "Name is required.");

        if (string.IsNullOrEmpty(srcToValidate.Url))
            _validationDictionary.AddError("Url", "Url is required.");
        else
            if (srcToValidate.Url.Trim().Length == 0)
                _validationDictionary.AddError("Url", "Url is required.");

        return _validationDictionary.IsValid;
    }       

I think of create client side validation, and add localization to validation errors, also i need create custom rules with calls to database, etc. What pros and cons of this 2 way, or maybe I need choose another way ? 我想创建客户端验证,并将本地化添加到验证错误中,我还需要创建带有对数据库的调用的自定义规则,等等。这两种方式的优缺点,或者也许我需要选择另一种方式?

The asp.net website offers guidance for three cases: asp.net网站为三种情况提供指导:

These are probably worth reading before making any decisions. 在做出任何决定之前,这些可能值得阅读。

Definitely worth reading up on the various options - choose whichever you think best suits your needs and style. 绝对值得阅读各种选项-选择您认为最适合您的需求和风格的那一种。

However, you will almost certainly end up creating a validation function on your service at some point to cope with business rules, so that may be the tie-breaker :-) 但是,几乎可以肯定,您最终会在某个时候在服务上创建一个验证功能来应对业务规则,因此这可能是平局:-)

Heres a few extra links which may be useful too: 这里有一些额外的链接可能也有用:

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

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