UI Validation with the MVP pattern. #Part01.

Sunday, March 15, 2009 6:44 PM | Leave a reply »

This is the first of 3/4 posts about the implementation of the UI Validation in a Windows Form application using the MVP Pattern.

In order to be able to follow my article you must have and know:

  1. Microsoft Enterprise Library UI Validation, available at this link.
  2. Basic knowledge of the MVP pattern.

Analysis.

First of all I want to explain why I’m going to validate the domain and not the UI. The concept is that a validation represent a constraint on the data. In most of the case the UI layer is never fixed and stable, but for the domain, at most, we have to change a field or an entity, so less effort.

Second point, why I should contact a DAL layer and validate the entity if I can do it before and save some bandwidth and resources??

If we add the validation in the domain, in the future, we will be able to carry our domain everywhere without tacking care of problem like the validation. If we add the validation in the UI, our presentation layer will be strictly connected to our domain, and this is something we don’t want.

The basic object.

One of the most helpful technique in the OOP (object oriented programming) is the inheritance. We can build a basic class with some useful methods and properties and use it like a contract for the other inherited classes.

In our case we want to build a simple domain object class that will represent all the instances of our domain.

image

Let’s view the simple code to build this class.

   1: using System;
   2: using System.Collections.Generic;
   3: using Microsoft.Practices.EnterpriseLibrary.Validation;
   4: using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
   5:  
   6: namespace XXX.Core
   7: {
   8:     public abstract class DomainObject
   9:     {
  10:         private ValidationResults Validate()
  11:         {
  12:             Validator validator = ValidationFactory.CreateValidator(Type.GetType(this.ToString()));
  13:             ValidationResults errors = validator.Validate(this);
  14:             IList<string> list = new List<string>();
  15:             foreach (ValidationResult result in errors)
  16:             {
  17:                 list.Add(result.Message);
  18:             }
  19:             this.ListResults = list;
  20:             return errors;
  21:         }
  22:         public virtual bool IsValid()
  23:         {
  24:             return this.Validate().IsValid;
  25:         }
  26:         public virtual IList<string> ListResults
  27:         {
  28:             get;
  29:             private set;
  30:         }
  31:     }
  32: }

I have just create the set methods as private because I don’t want to modify them outside the domain object class. Of course anyone of you will have a different solution here. I must use virtual because I hardly use the NHibernate Proxy.

At this point we have just to inherit from this class, any entity of our domain, also the entities that should not be persisted. Why? Because the validation we are going to do now it’s very straightforward. Then decorate the entity with some attributes.

Note: I don’t know why and I have found a lot of concerns about this solution but actually the Enterprise Library doesn’t have a nice way to implement the inheritance directly and the only way to do that is to use the constructor that uses the reflection:

  12:Validator validator = ValidationFactory.CreateValidator(Type.GetType(this.ToString()));

The entity.

Now let’s build the first entity that will inherit form our domain object. It should be something like that:

   1: namespace XXX.Core.USER
   2: {
   3:     public class User:DomainObject
   4:     {
   5:         ///Class implementation
   6:     }

After that what we need? Of course the fields of our entity! In this case I use the NET Framework 3.5 lazy creation where you don’t have to declare the private accesses.

   1: public virtual int Id { get; set; }
   2: public virtual string Username { get; set; }
   3: public virtual string Password { get; set; }

Now let’s define some business validation rules:

  1. Each of these fields cannot be null.
  2. The Username cannot be lower than 5 characters and longer then 20.
   1: [NotNullValidator(MessageTemplate="Id cannot be null")]
   2: public virtual int Id { get; set; }
   3:  
   4: [NotNullValidator(MessageTemplate="Username cannot be null")]
   5: [StringLengthValidator(5,20,MessageTemplate="Username has to be from 5 and 20 charactersS")]
   6: public virtual string Username { get; set; }

There we go!

The decoration attributes.

The enterprise library offers a very nice and configurable solution for the UI validation. This list is only a part of them and you can also build custom group of validation rules.

  • ContainsCharactersValidator - Performs validation on strings by verifying if it contains a character set.
  • DateTimeRangeValidator - Performs validation on DateTime instances by comparing them to the specified boundaries.
  • NotNullValidator - Logs a failure when validating a nullNothingnullptr reference.
  • RangeValidator - Performs validation on T instances by comparing them to the specified boundaries.
  • RegexValidator - Performs validation on strings by matching them to a Regex.
  • StringLengthValidator - Performs validation on strings by comparing their lengths to the specified boundaries.

The next post will show you how to integrate this Framework (If we can call him a framework …) with the Presentation Layer.


Comments

  1. Gravatar aqyatibr says:

    Re : # aqyatibr

    aqyatibr
  2. Gravatar wfqhnqwr says:

    Re : # wfqhnqwr

    wfqhnqwr
  3. Gravatar fyyzlopw says:

    Re : # fyyzlopw

    fyyzlopw
  4. Gravatar zocor says:

    Re : # zocor

    zocor
  5. Gravatar sildenafil says:

    Re : # sildenafil

    sildenafil
  6. Gravatar furosemide says:

    Re : # furosemide

    furosemide
  7. Re : # meridia online

    meridia online
  8. Re : # generic zyrtec

    generic zyrtec
  9. Re : # order xenical

    order xenical
  10. Gravatar ativan says:

    Re : # ativan

    ativan
  11. Re : # generic levitra

    generic levitra
  12. Gravatar ambien says:

    Re : # ambien

    ambien
  13. Re : # generic cialis online

    generic cialis online
  14. Re : # purchase soma online

    purchase soma online
  15. Re : # zoloft online

    zoloft online
  16. Gravatar atenolol says:

    Re : # atenolol

    atenolol
  17. Re : # cheap viagra online

    cheap viagra online
  18. Gravatar prevacid says:

    Re : # prevacid

    prevacid
  19. Gravatar lasix says:

    Re : # lasix

    lasix
  20. Gravatar clpqoqxe says:

    Re : # clpqoqxe

    clpqoqxe
  21. Gravatar ldrssdqj says:

    Re : # ldrssdqj

    ldrssdqj
  22. Gravatar bbeyrhue says:

    Re : # bbeyrhue

    bbeyrhue
  23. Gravatar xkuiekrl says:

    Re : # xkuiekrl

    xkuiekrl
  24. Gravatar bploxrro says:

    Re : # bploxrro

    bploxrro
  25. Re : # order carisoprodol

    order carisoprodol
  26. Re : # fluconazole

    fluconazole
  27. Gravatar imitrex says:

    Re : # imitrex

    imitrex
  28. Gravatar hoodia says:

    Re : # hoodia

    hoodia
  29. Gravatar vardenafil says:

    Re : # vardenafil

    vardenafil
  30. Gravatar zanaflex says:

    Re : # zanaflex

    zanaflex
  31. Gravatar celexa says:

    Re : # celexa

    celexa
  32. Gravatar alprazolam says:

    Re : # alprazolam

    alprazolam
  33. Gravatar esgic says:

    Re : # esgic

    esgic
  34. Re : # escitalopram

    escitalopram
  35. Gravatar retin-a says:

    Re : # retin-a

    retin-a
  36. Re : # esomeprazole

    esomeprazole
  37. Gravatar losec says:

    Re : # losec

    losec
  38. Gravatar xenical says:

    Re : # xenical

    xenical
  39. Gravatar paxil says:

    Re : # paxil

    paxil
  40. Re : # order xanax

    order xanax
  41. Gravatar lunesta says:

    Re : # lunesta

    lunesta
  42. Re : # hoodia online

    hoodia online
  43. Re : # purchase xanax

    purchase xanax
  44. Gravatar prilosec says:

    Re : # prilosec

    prilosec
  45. Gravatar advil says:

    Re : # advil

    advil
  46. Gravatar gqdwbatl says:

    Re : # gqdwbatl

    gqdwbatl
  47. Gravatar lkhgptye says:

    Re : # lkhgptye

    lkhgptye
  48. Re : # finasteride

    finasteride
  49. Re : # hydrocodone

    hydrocodone
  50. Re : # order fioricet

    order fioricet
  51. Gravatar lorazepam says:

    Re : # lorazepam

    lorazepam
  52. Re : # order vicodin online

    order vicodin online
  53. Re : # triamcinolone

    triamcinolone
  54. Gravatar augmentin says:

    Re : # augmentin

    augmentin
  55. Gravatar nasacort says:

    Re : # nasacort

    nasacort
  56. Gravatar kenalog says:

    Re : # kenalog

    kenalog
  57. Re : # generic prozac

    generic prozac
  58. Gravatar order soma says:

    Re : # order soma

    order soma
  59. Gravatar sonata says:

    Re : # sonata

    sonata
  60. Gravatar tadalafil says:

    Re : # tadalafil

    tadalafil
  61. Re : # generic norvasc

    generic norvasc
  62. Gravatar lexapro says:

    Re : # lexapro

    lexapro
  63. Gravatar whtayein says:

    Re : # whtayein

    whtayein
  64. Gravatar oiizddms says:

    Re : # oiizddms

    oiizddms
  65. Re : # fexofenadine

    fexofenadine
  66. Re : # cheap xenical

    cheap xenical
  67. Re : # buy vicodin

    buy vicodin
  68. Gravatar celebrex says:

    Re : # celebrex

    celebrex
  69. Re : # cheap carisoprodol

    cheap carisoprodol
  70. Re : # amoxycillin

    amoxycillin
  71. Gravatar cialis says:

    Re : # cialis

    cialis
  72. Re : # generic zoloft

    generic zoloft
  73. Gravatar bextra says:

    Re : # bextra

    bextra
  74. Re : # order ultram

    order ultram
  75. Re : # UI Validation with the MVP pattern. Part #03

    UI Validation with the MVP pattern. Part #03
  76. Re : # alprazolam online

    alprazolam online
  77. Gravatar omeprazole says:

    Re : # omeprazole

    omeprazole
  78. Re : # buy tramadol

    buy tramadol
  79. Re : # order soma online

    order soma online
  80. Re : # purchase soma

    purchase soma
  81. Re : # cheap phentermine online

    cheap phentermine online
  82. Re : # buy levitra

    buy levitra
  83. Gravatar bupropion says:

    Re : # bupropion

    bupropion
  84. Gravatar vicodin says:

    Re : # vicodin

    vicodin
  85. Gravatar soma says:

    Re : # soma

    soma
  86. Gravatar eypdfloy says:

    Re : # eypdfloy

    eypdfloy
  87. Gravatar hpskwjio says:

    Re : # hpskwjio

    hpskwjio
  88. Re : # purchase valium

    purchase valium
  89. Re : # ciprofloxacin

    ciprofloxacin
  90. Re : # nexium online

    nexium online
  91. Re : # tramadol online

    tramadol online
  92. Gravatar cipralex says:

    Re : # cipralex

    cipralex
  93. Gravatar ktnlnskp says:

    Re : # ktnlnskp

    ktnlnskp
  94. Re : # cheap propecia

    cheap propecia
  95. Gravatar xanax says:

    Re : # xanax

    xanax
  96. Gravatar ndhvvnxz says:

    Re : # ndhvvnxz

    ndhvvnxz
  97. Gravatar zvdbagsq says:

    Re : # zvdbagsq

    zvdbagsq
  98. Gravatar tttdoyvo says:

    Re : # tttdoyvo

    tttdoyvo
  99. Gravatar auiucdat says:

    Re : # auiucdat

    auiucdat
  100. Re : # allopurinol

    allopurinol
  101. Gravatar fosamax says:

    Re : # fosamax

    fosamax
  102. Gravatar ibuprofen says:

    Re : # ibuprofen

    ibuprofen
  103. Gravatar gabapentin says:

    Re : # gabapentin

    gabapentin
  104. Gravatar plavix says:

    Re : # plavix

    plavix
  105. Re : # buy carisoprodol

    buy carisoprodol
Comments have been closed on this topic.