编程语言
首页 > 编程语言> > c# – 如何将参数传递给ValidationAttribute?

c# – 如何将参数传递给ValidationAttribute?

作者:互联网

我有一个自定义ValidationAttribute,它检查是否已存在另一个用户.
为此需要访问我的数据访问层,Unity将一个实例注入我的控制器

如何将此(或任何相关内容)作为参数传递到我的自定义验证器中?

这可能吗?即在我创建Dal的地方,应该是一个参数

public class EmailIsUnique : ValidationAttribute
    {
        private string _errorMessage = "An account with this {0} already exists";

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            DataAccessHelper Dal = new DataAccessHelper(SharedResolver.AppSettingsHelper().DbConnectionString); //todo, this is way too slow
            bool isValid = true;
            if(value == null) {
                isValid = false;
                _errorMessage = "{0} Cannot be empty";
            } else {
                string email = value.ToString();
                if (Dal.User.FindByEmail(email) != null)
                {
                    isValid = false;
                }
            }

            if (isValid)
                return ValidationResult.Success;
            else
                return new ValidationResult(String.Format(_errorMessage, validationContext.DisplayName));
        }
    }

解决方法:

我不太确定你能否将运行时参数传递给你的属性.

您可以使用DependencyResolver.Current.GetService< DataAccessHelper>()来解析您的dal(假设您已注册DataAccessHelper)

您可能更有可能将DataAccessHelper注册为IDataAccessHelper或其他什么?在这种情况下,您将调用GetService< IDataAccessHelper>

public class EmailIsUnique : ValidationAttribute
{
    private string _errorMessage = "An account with this {0} already exists";

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        IDataAccessHelper Dal = DependencyResolver.Current.GetService<IDataAccessHelper>(); // too slow
        bool isValid = true;
        if(value == null) {
            isValid = false;
            _errorMessage = "{0} Cannot be empty";
        } else {
            string email = value.ToString();
            if (Dal.User.FindByEmail(email) != null)
            {
                isValid = false;
            }
        }

        if (isValid)
            return ValidationResult.Success;
        else
            return new ValidationResult(String.Format(_errorMessage, validationContext.DisplayName));
    }
}

要么

public class EmailIsUnique : ValidationAttribute
{
    [Dependency]
    public IDataAccessHelper DataAccess {get;set;}

    private string _errorMessage = "An account with this {0} already exists";

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        bool isValid = true;
        if(value == null) 
        {
            isValid = false;
            _errorMessage = "{0} Cannot be empty";
        } 
       else 
       {
            string email = value.ToString();
            if (DataAccess.User.FindByEmail(email) != null)
            {
                isValid = false;
            }
        }

        if (isValid)
            return ValidationResult.Success;
        else
            return new ValidationResult(String.Format(_errorMessage, validationContext.DisplayName));
    }
}

标签:c,asp-net-mvc,data-annotations
来源: https://codeday.me/bug/20190624/1280840.html