I'm writing a windows app that uses sql server. Some of my entities have
properties that must be unique. I'm not sure whether I should be performing
manual checks on save or use the unique constraints in sql server, or even
do both....
For example I have a Company entity with a Code property that should be
unique.
In my Company.Save method I could run a select to check whether there are
any existing rows with this code and only update / insert if nothing's
returned. This leaves the theoretical possibility that another user could
insert a duplicate code in the split seconds after my select, but my before
update.
Or, I could add a unique constraint on the underlying column in SqlServer
and get Company.Save to catch and interpret the specific SqlException that
will be thrown if the constraint is broken.
The second option seems safer, but is this overkill? Does anyone else have
other suggestions? Any comments are welcome...