Unique value validation

Posts   
 
    
methodman
User
Posts: 194
Joined: 24-Aug-2009
# Posted on: 12-Feb-2010 11:00:32   

I have an User entity where the username value must be unique. The database table has a unique constraint on the propriate column.

The question is:

Should I create some kind of validator which will check if the given username already exists or just save the entity and wait if an exception will bubble up ?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Feb-2010 05:33:26   

Hi methodman,

First, sorry for the late reply, but your thread fell by accident in the less-priority queue.

Back to your question: In my opinion this is not strictly a validation rule, but of course it can be. I would implement both ways (fetch to validate and then catch if something wrong happens) because there are some scenarios when only fetch-to-validate could be tricky. Imagine that while your application is validating the existence of the record, another user save the same data. In both cases, you always have to catch to see if something went wrong and inform that to the user.

David Elizondo | LLBLGen Support Team