Invest in people before investing in tools

Il blog di Matteo Baglini
posts - 118, comments - 95, trackbacks - 697

[Spring.NET #25] Spring.Core, Impostare le regole di validazione in maniera programmatica

Come già detto in un post precedente possiamo definire il set di regole di validazione anche in maniera programmatica utilizzando le API fornite da Spring.NET. Vediamo come creare via codice le regole dichiarate nel file XML nell'esempio del post precedente:

   1:  using System;
   2:  using Spring.Validation;
   3:  using Spring.Expressions;
   4:   
   5:  namespace SpringSeries.Core.ValidationObjectModel {
   6:      class Program {
   7:          static void Main(string[] args) {
   8:              RequiredValidator _reqVal1 = new RequiredValidator();
   9:              _reqVal1.Test = Expression.Parse("FirstName");
  10:   
  11:              ConditionValidator _conVal = new ConditionValidator();
  12:              _conVal.Test = Expression.Parse("DateOfBirth != DateTime.MinValue");
  13:   
  14:              RegularExpressionValidator _regexVal = new RegularExpressionValidator();
  15:              _regexVal.Test = Expression.Parse("FirstName");
  16:              _regexVal.Expression = "[A-Za-z]*";
  17:   
  18:              RequiredValidator _reqVal2 = new RequiredValidator();
  19:              _reqVal2.Test = Expression.Parse("AddressInfo.City");
  20:              _reqVal2.When = Expression.Parse("AddressInfo.Address!=String.Empty");
  21:   
  22:              RequiredValidator _reqVal3 = new RequiredValidator();
  23:              _reqVal3.Test = Expression.Parse("AddressInfo.PostalCode");
  24:              _reqVal3.When = Expression.Parse("AddressInfo.Address!=String.Empty");
  25:   
  26:              ValidatorGroup _personVal = new ValidatorGroup();
  27:              _personVal.Validators.Add(_reqVal1);
  28:              _personVal.Validators.Add(_reqVal2);
  29:              _personVal.Validators.Add(_reqVal3);
  30:              _personVal.Validators.Add(_conVal);
  31:              _personVal.Validators.Add(_regexVal);
  32:   
  33:              Person _p01 = new Person(1);
  34:              _p01.FirstName = "Matteo";
  35:              _p01.LastName = "Baglini";
  36:              _p01.DateOfBirth = new DateTime(1982, 11, 8);
  37:   
  38:              Person _p02 = new Person(2);
  39:              _p02.LastName = "Baglini";
  40:              _p02.DateOfBirth = new DateTime(1982, 11, 8);
  41:   
  42:              Person _p03 = new Person(3);
  43:              _p03.FirstName = "Matteo";
  44:              _p03.LastName = "Baglini";
  45:   
  46:              Person _p04 = new Person(4);
  47:              _p04.FirstName = "Matteo";
  48:              _p04.LastName = "Baglini";
  49:              _p04.DateOfBirth = new DateTime(1982, 11, 8);
  50:              _p04.AddressInfo.Address = "Via dei Sassi Sgonfi";
  51:   
  52:              Person _p05 = new Person(5);
  53:              _p05.FirstName = "Matteo";
  54:              _p05.LastName = "Baglini";
  55:              _p05.DateOfBirth = new DateTime(1982, 11, 8);
  56:              _p05.AddressInfo.Address = "Via dei Sassi Sgonfi";
  57:              _p05.AddressInfo.City = "Livorno";
  58:              _p05.AddressInfo.PostalCode = "57100";
  59:   
  60:              Person _p06 = new Person(6);
  61:              _p06.FirstName = "1234567890";
  62:              _p06.LastName = "Baglini";
  63:              _p06.DateOfBirth = new DateTime(1982, 11, 8);
  64:              _p06.AddressInfo.Address = "Via dei Sassi Sgonfi";
  65:              _p06.AddressInfo.City = "Livorno";
  66:              _p06.AddressInfo.PostalCode = "57100";
  67:   
  68:              Console.WriteLine(_p01.ToString());
  69:              Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p01, null));
  70:              Console.WriteLine();
  71:              Console.WriteLine(_p02.ToString());
  72:              Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p02, null));
  73:              Console.WriteLine();
  74:              Console.WriteLine(_p03.ToString());
  75:              Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p03, null));
  76:              Console.WriteLine();
  77:              Console.WriteLine(_p04.ToString());
  78:              Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p04, null));
  79:              Console.WriteLine();
  80:              Console.WriteLine(_p05.ToString());
  81:              Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p05, null));
  82:              Console.WriteLine();
  83:              Console.WriteLine(_p06.ToString());
  84:              Console.WriteLine("Is Valid? {0}", _personVal.Validate(_p06, null));
  85:   
  86:              Console.Read();
  87:          }
  88:      }
  89:  }
Technorati Tag:

Print | posted on domenica 16 dicembre 2007 16:24 | Filed Under [ .NET OpenSource Spring.NET ]

Powered by:
Powered By Subtext Powered By ASP.NET