I've a credit card applicatoin that saves data right into a table known as Jobs. The Jobs table includes a column known as Title with a UNIQUE constraint. The Title column isn't PRIMARY KEY. I question basically should look for duplicate records myself before I attempt in order to saveOrrevise a brand new entry or maybe it's easier to watch for the best tossed through the data access layer. I am using NHibernate with this Application whether it's associated with a importance

The reply is: both.

In case your database has constraints it may guarantee certain invariants concerning the data, for example originality. This can help in a number of ways:

  • For those who have a bug inside your application, breaking the constraint will flag something which might otherwise 't be observed.

  • Other customers from the database can assume much more about the behavior of the information because the DBMS makes sure invariants.

  • The database safeguards itself from incorrect updates that violate the constraints. If you discover you've another system or interface inhabiting the database lower the track, the constraints enforced through the database imply that anything caught through the constraints will not (or at best is not as likely to) break the body.

Programs and databases reside in a M:M relationship in almost any however the most trivial cases. The applying should have the right data and business rule validations however, you should still not arrange for the application being the only real customer from the data. Operate in data warehousing for any couple of many you will see the results of programs created by individuals with this mindset.

I'd leave that actually work entirely towards the database your code should concentrate on catching and correctly handling the exception.


  1. Performance- The database is going to be highly enhanced to enforce constraints inside a fast and efficient way. You will not have enough time to optimize your code too.
  2. Maintainability- When the constraints change later on, you will not have to change your code, or possibly you will have to give a new catch. If your constraint is dropped, you will not need to touch your code at all.

In case your design is nice (both database and BL), the database should not have constraints that would not be worked within the BL - i.e. you should not be showing the database with sporadic data. But there is nothing perfect.

I have discovered that limiting the database to data consistency constraints allows me handle all BL validation in procedural code, and also the only cases when I experience database exceptions are design and coding errors which could (and really should be) fixed.

Inside your situation, checking the reputation for originality is data content validation, correctly handled in code. Which most probably catches the mistake nearest the purpose of commission, in which you hopefully have user friendly UI assets to ask without presenting undesirable coupling between abstractions.

If you are planning to determine the constraints yourself, get it done within the data access layer. Nothing above that layer ought to know anything regarding your database or its constraints.

Generally I'd say allow the DAL to trap DB-came from exceptions. However in your particular situation, I believe we are speaking about fundamental input validation. I'd choose a title availability check call towards the database, before posting the entire form.

You need to certainly look for any exception tossed through the data access layer. The issue with checking if there's an archive with similar value is, required you to definitely lock the table for modifications before you place the brand new record to avoid race conditions.

It's generally better to look for exceptions/errors, even when you've checked everything yourself before. There's more often than not something which will go wrong or that you simply haven't considered inside your code but is enforced through the database.

Edit: Basically comprehend the question right, it's not about when the constraint ought to be enforced through the database or otherwise, but how to approach it within the application code. Obviously you need to always setup all constraints within the database to avoid bad data entering your database.