开发者

nhibernate validator usage

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 docum开发者_开发百科entation 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;
    });
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜