tags:

views:

249

answers:

4

I was wondering what everyone thinks of this. Is the code easy to follow? Or is there a better way to do this? By the way, this is how I am currently doing validation at the moment with ASP.NET MVC. I can follow it, but I am the one who wrote it. For some reason SO is removing the line breaks between the validators.

     public override Validation<MemberCreate> ValidationRules()
 {
  var validation = new Validation<MemberCreate>();

  validation.Add(x => x.Name)
   .LengthBetween(
    Config.Member.NameMinLength, 
    Config.Member.NameMaxLength, 
    Resources.Errors.LengthBetweenNotValid.Fmt(
     Resources.Titles.Name, 
     Config.Member.NameMinLength, 
     Config.Member.NameMaxLength))
   .Characters(Resources.Errors.CharactersNotValid.Fmt(Resources.Titles.Name));

  validation.Add(x => x.EmailAddress).Email(
   Resources.Errors.EmailNotValid.Fmt(
    Resources.Titles.EmailAddress));

  validation.Add(x => x.VerifyEmailAddress).Equal(
   x => x.EmailAddress, 
   Resources.Errors.CompareNotValid.Fmt(
    Resources.Titles.VerifyEmailAddress, 
    Resources.Titles.EmailAddress));

  validation.Add(x => x.PassWord).LengthGreaterThan(
   Config.Member.PassWordMinLength, 
   Resources.Errors.LengthGreaterThanNotValid.Fmt(
    Resources.Titles.PassWord, 
    Config.Member.PassWordMinLength));


  validation.Add(x => x.VerifyPassWord).Equal(
   x => x.PassWord,
   Resources.Errors.CompareNotValid.Fmt(
    Resources.Titles.VerifyPassWord,
    Resources.Titles.PassWord));

  return validation;
 }
+2  A: 

I'm not a C# guy by any means, but it appears straightforward. It seems to be putting a bunch of rules into a structure of some sort, and I assume it would then apply then to validate messages of some sort. Application of the Command pattern, I'd think.

Charlie Martin
+1  A: 

As long as you keep it nicely formatted like that I don't have a problem with it.

PhoenixRedeemer
A: 

I'll vote yes on puritanical grounds - the formatting is inconsistent and there isn't a comment in sight.

Pragmatically one can follow what you're doing with a little effort though.

annakata
well comments are the last thing I add while programming, I know I should add them as I write the code but since my eyes are only seeing it usually I do that last.
Mike Geise
Writing them as you write the code is debatable. You should only do that if you're extremely diligent about updating comments as you update code, which many people are not.
John Sheehan
topic for a whole other thread there...
annakata
A: 

I'm a PHP guy and I can still get what's going on. You're grouping validators for a registration page. When the member is created, it validates the data. Some of the validators are shortcuts (like email). It could still use some comments.

thrashr888