views:

74

answers:

2

Is this a good aproach to making sure the fields has valid values for a database?

    internal class Customer
{

    private string _CustomerId;
    internal string CustomerId
    {
        get
        {
            return (_CustomerId==null?string.Empty:(_CustomerId.Length>20?_CustomerId.Substring(0,20):_CustomerId));
        }
        set
        {
            _CustomerId = value;
        }
    }}

Regards Sven

+3  A: 

A cleaner technique would be to annotate your properties with validation attributes and use a library to validate the entities.

Examples include:

Then, depending on the library selected, your code would resemble:

public class Customer
{
    [StringLengthValidator(20)]
    public virtual string CustomerId { get; set;}
}
Ian Nelson
When your validations are simple, I'd go with DataAnnotations. When the requirements are more complex, go with the Enterprise Library Validation Application Block. It allows many complex scenario's.
Steven
+1 DataAnnotations is probably the only thing Sven needs. With the others, he'd have to worry about possibly conflicting licenses when he releases his library. Sidenote: another answer was posting while I typed this, and it somehow caused my comment to become an answer!
Jim Schubert
Agreed. I made the mistake recently of decorating WCF DataContracts with Enterprise Library Validation Block attributes, and now all consumers of the WCF service need to reference that particular version of various EntLib dlls. Grrr. In retrospect I should have gone with DataAnnotations.
Ian Nelson
I agree that DataAnnotations is probably his best pick (it's simple), but I don't agree on licensing. I use the configuration based validation of the Validation Application Block. Wiring is done in a configuration file and VAB has a configuration tool for this. My entities and DTO's are completely free of any validation attributes.
Steven
Thanks for the pointer Steven, I'll look into that approach.
Ian Nelson
If you're taking this route, it's best to put the VAB configuration in its own file. I've written an article about this here: http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=65. Let me know if you're having any problems with VAB, it can do a lot more than you might think :-)
Steven
OK, DataAnnotations looks like it's what I'm looking for. Simple and to the point. But how and where do I do the actual validation?
Zwempha
.NET 4.0 adds a Validator class to DataAnnotations. I'm not sure if DataAnnotations in .NET 3.5 has a build-in mechanism of validating. If switching to .NET 4.0 is not an option you can use Reflector to copy the implemenation of the .NET 4.0 Validator into your .NET 3.5 project. Could somebody confirm if my statement is correct?
Steven
I solved it based on dataannotations and checking for errors using a variant of http://www.davidmuto.com/Blog.muto/View/validation-with-dataannotationsThank you very much!!!
Zwempha
+2  A: 

Your way of validating input is very brittle. You are excepting any possible input (for the CustomerId in this case) and sanitize it when it is requested. This might work in this basic scenario, but in a lot of cases you can't sanitize the input. You are basically correcting the mistakes of the user and making assumptions of what he intended. How will you do that with an mail address? For instance, must the mail address 'stevenhotmail.com' be converted to '[email protected]' or to '[email protected]'. Besides this, there is also the possibility of a programming error. Would you want your program try to fix your own programming errors. This will give you headache. Or what will you do when two properties of the same entity have to be compared?

A better solution would be to allow the entity to become in an invalid state and check it's validity just before saving it to the database. When it’s state is invalid, don’t try to automatically correct the changes, but just throw an exception or communicate the errors back to the user.

There are multiple approaches of doing this. You can for instance implement an IsValid() method on each entity or use a validation framework.

Steven