Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Simple Object Validation?

Simple Object Validation?

Scheduled Pinned Locked Moved Design and Architecture
questiondesignregexarchitecture
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wauna
    wrote on last edited by
    #1

    Hi Everyone, Thanks again for helping me so much. I have another silly design question. Is there a good way to validate object data? I know that lots of people use the front-end to validate their data, but I was hoping to also validate the object data, just in case the front end fails. Perhaps this is overkill, but I assume it's a pretty common task. I'd rather not do a million if/then's. It would be great if I could write the logic only once. Is there a design pattern that could facilitate this? Thanks! Chad

    W D 2 Replies Last reply
    0
    • W Wauna

      Hi Everyone, Thanks again for helping me so much. I have another silly design question. Is there a good way to validate object data? I know that lots of people use the front-end to validate their data, but I was hoping to also validate the object data, just in case the front end fails. Perhaps this is overkill, but I assume it's a pretty common task. I'd rather not do a million if/then's. It would be great if I could write the logic only once. Is there a design pattern that could facilitate this? Thanks! Chad

      W Offline
      W Offline
      WillemM
      wrote on last edited by
      #2

      There are several methods for this. I created one using Extension methods in C#. The basic idea would be to create a class that implements a static method to implement a generic object. This method should accept the object to validate and a set of rules to validate the object against. If the validation fails the method should throw an exception containing the reason why the validation failed (Preferrably a message configured on a rule that failed validation). Implementation could look like this:

      public static class ObjectValidator {
      public static void Validate(object objectToValidate,ValidationRuleSet ruleSet) {
      foreach(ValidationRule rule in ruleSet) {
      if(!rule.Validate(objectToValidate)) {
      throw new ValidationException(rule.ErrorMessage);
      }
      }
      }
      }

      Of course you need to expand this to support validation of specific properties each with multiple rules to check required fields and validate the value of the field against some pattern for example. There should be some standard components out there that can be quite a timesaver here. Last time I checked Microsoft had a validation application block in their enterprise library which should fit quite nicely.

      WM. What about weapons of mass-construction? "What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson My blog

      1 Reply Last reply
      0
      • W Wauna

        Hi Everyone, Thanks again for helping me so much. I have another silly design question. Is there a good way to validate object data? I know that lots of people use the front-end to validate their data, but I was hoping to also validate the object data, just in case the front end fails. Perhaps this is overkill, but I assume it's a pretty common task. I'd rather not do a million if/then's. It would be great if I could write the logic only once. Is there a design pattern that could facilitate this? Thanks! Chad

        D Offline
        D Offline
        DavidNohejl
        wrote on last edited by
        #3

        Take a look at Validation Application Block (http://msdn2.microsoft.com/en-us/library/bb410105.aspx[^]).


        [My Blog]
        "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
        "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

        W 1 Reply Last reply
        0
        • D DavidNohejl

          Take a look at Validation Application Block (http://msdn2.microsoft.com/en-us/library/bb410105.aspx[^]).


          [My Blog]
          "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn
          "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe

          W Offline
          W Offline
          Wauna
          wrote on last edited by
          #4

          In my search for information on how to design validation, I did find this as well. The only problem is that I'm working with VS 2003. I don't think I can do the .NET 3.0 stuff with that. Can I? If I cannot do the 3.0 stuff, I'm torn, because it doesn't make any sense to re-work what's already been done with the Application Blocks. I'm already using the older application blocks for data access, logging, and exception logging.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups