Validation with Interfaces on ASP.Net

Topics: Validation Application Block
Nov 25, 2010 at 2:59 PM

Hi there,

I am trying to implement the Validation Block into my MVP-like datastructur Web-Application. Since I later want to use this on a large SharePoint 2010 Webpart.

In that Webpart I am using a lot of Interfaces (IContact, IBook, etc). Unfortunately Validation Block doesn't seem to validate correctly against Interfaces.

To explain further I show u the code of my example:

 

    public interface IBuch
    {
        string Titel { get; set; }

        string Autor { get; set; }

        string Isbn { get; set; }

        DateTime ErscheinungsDatum { get; set; }       
    }
    public interface IBuchView 
    {
        [NotNullValidator(MessageTemplate = "There has to be a title!")]
        [StringLengthValidator(3, 20)]
        string Titel { get; set; }

        string Autor { get; set; }

        string Isbn { get; set; }
     
        DateTime ErscheinungsDatum { get; set; }
    }
<!-- HTML generated using hilite.me -->
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    public class BuchControlPresenter
    {
        private readonly IBuchView _view;
        private Datenzugriff data;

        public BuchControlPresenter(IBuchView view)
        {
            this._view = view;
            data = new Datenzugriff();
        }

        public void Init()
        {
            IBuch b = data.getBuchB();
            _view.Autor = b.Autor;
            _view.ErscheinungsDatum = b.ErscheinungsDatum;
            _view.Isbn = b.Isbn;
            _view.Titel = b.Titel;
        }

        public void Save()
        {
            IBuch b = new Buch() {Autor = _view.Autor, ErscheinungsDatum = _view.ErscheinungsDatum, Isbn = _view.Isbn, Titel = _view.Titel};

            data.SaveBuch(b);        
        }
    }
<!-- HTML generated using hilite.me -->
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
        protected void SendButton_Click(object sender, EventArgs e)
        {
            r = Validation.Validate<WebAppValidationDemo1.IBuchView>(b);
            if (r.IsValid)
            {
                p.Save();
                errorLabel.Text = "";
            }
            else
            {
                string errors = r
                    .Select(res => res.Message).ToString();
                errorLabel.Text = errors;
            }
            p.Init();
        }

I hope this explains my example. On the ASP Part I have a few TextBoxes and one PropertiesProxyValidator bound to IBuchViews Title like this:

<cc1:PropertyProxyValidator ID="TitelValidator" runat="server" EnableClientScript="true"
                ControlToValidate="TextBox1"  PropertyName="Titel" SourceTypeName="WebAppValidationDemo1.IBuchView" >#</cc1:PropertyProxyValidator>

Now I insert a title like this "no", when I try to Save my Buch with SendButton_Click() the Validation returns isValid = true. I expected it to return false (since in IBuchView I have a StringLenghtValidator).

Interestingly my PropertyProxyValidator, which I described already, marks the TextBox Text as being wrong after postback!

So my question is: Why do the Validation.Validate and the PropertieProxyValidator behave so differently? And what do you think is best practice in this scenario?

If you have read up to here - thank you! :) (and please excuse my english skills)

Nov 26, 2010 at 1:32 AM
Edited Nov 26, 2010 at 1:33 AM

This is due to the change in Validation Application Block regarding on the Validation.Validate method.  It now validates the object based on its actualy type, in your case, it validates the object you passed based on the validation rules in the implementation of IBuchView and not on the validation rules defined in IBuchView.   This behavior is not the same with the PropertyProxyValidator because it doesn't use Validation.Validate but creates a validator for the property only.  I'm not sure if the EntLib Team is aware of the inconsistency, better log it in the Issue Tracker.

Anyway, to achieve the same results in both scenario, you can change your code to create a validator for IBuchView and calling Validate against the validator.

Validator validator = ValidationFactory.CreateValidator();
r = validator.Validate(b);

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 10:13 AM

Hello Sarah,

thank you very much for your answer. I successfully tested your solution. It works. :)

Since the behaviour is in my opinion still irritating, I created an issue and hope for an answer on that one.

Thanks again for your quick help.