CodeSmith Community
Your Code. Your Way. Faster!

Validation Application Block

Latest post 07-07-2008 2:11 AM by Polar. 4 replies.
  • 12-24-2007 9:41 AM

    • m1dst
    • Not Ranked
    • Joined on 12-24-2007
    • Posts 3
    • Points 75

    Validation Application Block

    Thanks to everyone who has helped get NetTiers to support the VAB.

    Overview of the project 

    I have an generic entity class created from my database.  This entity is used by several different WPF forms.  I need to apply a 2nd level of validation to the entity in some instances.

    For example if the entity is mapped to Form1 I need to add a regular expression to "PropertyX"  - If the entity is mapped to Form2 then I need to apply a minimum length to "PropertyY"

    All of the DB validation has been automatically added thanks to NetTiers so I need to keep that validation.

    Where is the best place to add this validation?  Form, Business Layer, Entity?  - Just to complicate things I want the control to be validated in realime as they type which makes me think that the service layer is out?  I did this validation in the past but the validation was done on the form in the textchanged event - not very slick.

    As I have control of the form code I wondered if I could create the additional Validators in the form and add them to a collection in the entity.  That way if there are additional validators I can add as required.  Not to sure how it all works at the moment though.

    I did an experiment where I created a validator method on the entity and called it from the WPF form and it worked but I know that is not best practice.  This is not very expandible either.

     Any advice for a someone keen to learn would be most appreciated.

     Merry Christmas.

     M1DST

    Filed under: ,
    • Post Points: 35
  • 12-24-2007 4:03 PM In reply to

    • katokay
    • Top 25 Contributor
    • Joined on 07-09-2006
    • Salt Lake City, UT
    • Posts 143
    • Points 3,190

    Re: Validation Application Block

    The easiest way IMO to perform validation using the application block is to use the config file. So if changes need to occur it won't require a recompile. The only scenario that doesn't apply is if using self validation on an entity (uses the SelfValidation attribute). In addition you can define multiple "Rulesets" to apply different rules for the same property. The application block also includes validation controls for asp.net, and winforms, and through a community contribution WPF, as well as hooks for WCF, etc. The easiest way is to just use the validation controls included with Enterprise Library and fire validation when necessary for your application.

    • Post Points: 35
  • 01-06-2008 11:22 AM In reply to

    • m1dst
    • Not Ranked
    • Joined on 12-24-2007
    • Posts 3
    • Points 75

    Re: Validation Application Block

    Hi Katokay,

     Thanks for your reply.  I am investigating some of the options you suggest and like the idea of multiple rulesets.  How do I add new rules and where do I put them?  I see that the generated class is decorated with rule attributes.  If I regenerate the class then any attributes I add will be lost. I must add that I don't wish to use config files as I don't want people to change them.

    Filed under:
    • Post Points: 35
  • 01-06-2008 5:59 PM In reply to

    • katokay
    • Top 25 Contributor
    • Joined on 07-09-2006
    • Salt Lake City, UT
    • Posts 143
    • Points 3,190

    Re: Validation Application Block

     If you prefer to add validation rules using attributes, add your own custom ones in the non .generated.cs files by overriding the property and specify as many rules on each property as your application requires. The validation block will use the subclass rules over the base class. The documentation for Enterprise Library has several thorough examples for various validation scenarios, I would recommend starting there to answer questions you may have with the validation block.

    • Post Points: 35
  • 07-07-2008 2:11 AM In reply to

    • Polar
    • Top 100 Contributor
    • Joined on 09-04-2007
    • Posts 51
    • Points 935

    Re: Validation Application Block

    Katokay, can you post a small example showing for to override a property and add a Decimal validator that requires the value to be greater than 0.

    Thanks

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems