views:

38

answers:

2

Hi
I have read and searched this issue with the Enterprise Library Validation mechanism. This very simple forms application demonstrates the issue. The Metadata class is ignored by the validator. I am trying to use it in a MVC application with an Entity Framework.

This is in .NEt 3.5 using VS 2008 on XP SP3.

namespace ValidationTest
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        string longname = "this is much too long to be a name";
        Customer2 cust = new Customer2(longname);

        ValidationResults r = Validation.Validate<Customer2>(cust);
        if (!r.IsValid)
        {
            throw new ArgumentException();
        }
    }
}


public partial class Customer2
{

    public string CustomerName;

    public Customer2(string name)
    {

        CustomerName = name;
    }

}

[MetadataType(typeof(CustMetadata))]
public partial class Customer2
{
}

public class CustMetadata
{
    [StringLengthValidator(0, 20)]
    public string CustomerName { get; set; }
}

}

They are both in the same file for presentation purposes. If I move the StringLengthAttribute to the main class it does work.

Any insight would be greatly appreciated.

Thanks

+1  A: 

The issue you are seeing is that in the Customer2 class CustomerName is a field but in the CustMetadata class CustomerName is a property.

If you change CustMetadata to:

public class CustMetadata
{
    [StringLengthValidator(0, 20)]
    public string CustomerName;
}

then it works as expected.

The Validation Application Block uses both the MemberType and the Name when matching the MetaData.

Tuzo
Thank you - I tried that and still have no go.
Jerry
@Jerry - I copied your code example and it works fine (it throws an ArgumentException)! The only thing I can think of is that you are using not using Enterprise Library 5. Could you be using EL 4.1?
Tuzo
A: 

Thank you for the response. I tried that as in the code below but it still does not work.

  public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        string longname = "this is much too long to be a name............";
        Customer2 cust = new Customer2(longname);
        cust.Address = "12 Berkley Street";

        ValidationResults r = Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate(cust);

        if (!r.IsValid)
        {
            throw new ArgumentException();
        }
    }
}


public partial class Customer2
{

    public string CustomerName;

    public Customer2(string name)
    {

        CustomerName = name;
    }

}

[MetadataType(typeof(CustMetadata))]
public partial class Customer2
{
    public string Address;
}

public class CustMetadata
{
    [StringLengthValidator(0, 20)]
    public string CustomerName;

    [Required]
    public string Address;
}
Jerry
@Jerry - If you have additional information you should add that as an update to your question instead of posting an answer.
Tuzo