Should I check for DB constraints in code or should I catch exceptions thrown by DB

前端 未结 8 448
别跟我提以往
别跟我提以往 2021-02-04 03:43

I have an application that saves data into a table called Jobs. The Jobs table has a column called Name which has a UNIQUE constraint. The Name column is not PRIMARY KEY. I wond

8条回答
  •  别那么骄傲
    2021-02-04 04:07

    If you are going to check the constraints yourself, do it in the data access layer. Nothing above that layer should know anything about your database or its constraints.

    In most cases I'd say leave it to the DAL to catch DB-originated exceptions. But in your specific case, I think we're talking about basic input validation. I'd opt for a name availability check call to the database, before submitting the whole form.

提交回复
热议问题