NHibernate的验证器的用法 [英] nhibernate validator usage

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

问题描述

说我有一个值对象类,全名,被用作在Employee实体类的属性。该全名可能有一个中间初始,昵称等;但是从一个域的角度来看,我想只有强制,无论是全名的名字和姓氏的属性值。

我想前preSS以此为EmployeeValidator的一部分。ValidationDef {}员工的对象,而不是一个属性。

我首先需要做一个验证器类的全名(即Fi​​rstAndLAstNameRequired),然后说,在员工FullName属性是有效的(使用ValidAttribute一些能说会道的形式)?

顺便说一句,似乎本文档仍是最好的了,但它确实在三岁的时候有些陈旧。有什么新的,我错过了?

干杯,结果
Berryl

更新

我还没有想通了这一点,但我发现什么是可能在这里NHib验证信息的最佳来源:的 http://fabiomaulo.blogspot.com/search/label/Validator

下面是一些伪code到前preSS的问题​​更好了:

  ///<总结>一个人的名字 -  LT; /总结>
公共类全名
{    公共虚拟字符串名字{获得;组; }
    公共虚拟字符串名字{获得;组; }
    公共虚拟字符串中间名{获得;组; }
    公共虚拟字符串昵称{搞定;组; }
}公共类EmployeeValidator:ValidationDef<员工>
{
    公共EmployeeValidator()
    {
        定义(X => x.FullName).FirstAndLastNameRequired(); //如何获得在这里!
    }
}

大卫

更新

 公共类FullNameValidator:ValidationDef<全名>
{
    公共FullNameValidator(){        定义(N => n.FirstName).NotNullable()And.NotEmpty()And.MaxLength(25)。。
        定义(N => n.LastName).NotNullable()And.NotEmpty()And.MaxLength(35)。。        //不是真的有必要,但很酷,你可以这样做
        ValidateInstance
            。通过(
                (姓名,上下文)=> !name.FirstName.IsNullOrEmptyAfterTrim()及&放大器; !name.LastName.IsNullOrEmptyAfterTrim())
            .WithMessage(既是一个名字和姓氏是必需的);
    }
}公共类EmployeeValidator:ValidationDef<员工>
{
    公共EmployeeValidator()
    {
        定义(X => x.FullName).IsValid(); // ***不编译!
    }
}


解决方案

要获得全名进行验证时,验证员工,我想你会做类似如下:

 公共类EmployeeValidator:ValidationDef<员工>
{
    公共EmployeeValidator()
    {
        定义(X => x.FullName).IsValid();
        定义(X => x.FullName).NotNullable(); //不知道你需要这个
    }
}

然后全名验证只会是这样的:

 公共类FullNameValidator:ValidationDef<全名>
{
    公共EmployeeValidator()
    {
        定义(X => x.FirstName).NotNullable();
        定义(X => x.LastName).NotNullable();
    }
}

另外我想你可以做这样的事情(还没有检查语法):

 公共类EmployeeValidator:ValidationDef<员工>
{
公共EmployeeValidator(){    ValidateInstance.By((员工,上下文)=> {
        BOOL的isValid = TRUE;
        如果(string.IsNullOrEmpty(employee.FullName.FirstName)){
            的isValid = FALSE;
            context.AddInvalid<员工,串>(
                请输入一个名字。,C => c.FullName.FirstName);
        } //用于姓氏相似
        返回的isValid;
    });
}
}

Say I have a value object class, FullName, that is used as a property in an Employee entity class. The FullName may have a middle initial, nick name, etc; but from a domain perspective I would like to only enforce that both the FirstName and LastName properties of the FullName are valued.

I want to express this as part of an EmployeeValidator : ValidationDef{Employee} object, as opposed to an attribute.

Do I first need to make a class validator for FullName (ie, FirstAndLAstNameRequired) and then say that the FullName property in Employee is Valid (using some loquacious form of the ValidAttribute)?

As an aside, it seems that this documentation is still the best out there, but it does look dated at three years old. Is there anything newer that I missed?

Cheers,
Berryl

UPDATE

I haven't figured this out yet, but I have found what is likely the best source of NHib Validator info here: http://fabiomaulo.blogspot.com/search/label/Validator

Here is some psuedo code to express the question better too:

/// <summary>A person's name.</summary>
public class FullName
{

    public virtual string FirstName { get; set; } 
    public virtual string LastName { get; set; } 
    public virtual string MiddleName { get; set; } 
    public virtual string NickName { get; set; } 
}

public class EmployeeValidator : ValidationDef<Employee>
{
    public EmployeeValidator()
    {
        Define(x => x.FullName).FirstAndLastNameRequired();  // how to get here!!
    }
}

UPDATE FOR DAVID

public class FullNameValidator : ValidationDef<FullName>
{
    public FullNameValidator() {

        Define(n => n.FirstName).NotNullable().And.NotEmpty().And.MaxLength(25);
        Define(n => n.LastName).NotNullable().And.NotEmpty().And.MaxLength(35);

        // not really necessary but cool that you can do this
        ValidateInstance
            .By(
                (name, context) => !name.FirstName.IsNullOrEmptyAfterTrim() && !name.LastName.IsNullOrEmptyAfterTrim())
            .WithMessage("Both a First and Last Name are required");
    }
}

public class EmployeeValidator : ValidationDef<Employee>
{
    public EmployeeValidator()
    {
        Define(x => x.FullName).IsValid();  // *** doesn't compile !!!
    }
}

解决方案

To get the FullName validated when you validate the employee, I think you'd do something like the following:

public class EmployeeValidator : ValidationDef<Employee>
{
    public EmployeeValidator()
    {
        Define(x => x.FullName).IsValid();
        Define(x => x.FullName).NotNullable(); // Not sure if you need this
    }
}

Then the FullName Validator would just be something like:

public class FullNameValidator : ValidationDef<FullName>
{
    public EmployeeValidator()
    {
        Define(x => x.FirstName).NotNullable();
        Define(x => x.LastName).NotNullable();
    }
}

Alternatively I think you could do something like (haven't checked the syntax):

public class EmployeeValidator: ValidationDef<Employee>
{
public EmployeeValidator() {

    ValidateInstance.By((employee, context) => {
        bool isValid = true;
        if (string.IsNullOrEmpty(employee.FullName.FirstName)) {
            isValid = false;
            context.AddInvalid<Employee, string>(
                "Please enter a first name.", c => c.FullName.FirstName);
        } // Similar for last name
        return isValid;
    });
}
}

这篇关于NHibernate的验证器的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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