强制数据库约束在应用code [英] Enforcing Database Constraints In Application Code

查看:130
本文介绍了强制数据库约束在应用code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的第一个真正的asp.net web应用程序和我有点难倒最好的地方和方法,以捕获并处理数据库约束违规。假设我有一列的唯一约束和用户输入的东西,就会违背唯一约束。难道我抓住它在业务层通过调用数据库来检查值的存在,该列还是我让他走,一路到数据库,让它抛出一个异常和处理,在我的应用程序?

I'm working on my first real asp.net web application and I'm kind of stumped on the best place and method to trap and handle database constraint violations. Suppose I have a unique constraint on a column and a user inputs something that would violate that unique constraint. Do I catch it in the business layer by making a call to the database to check of the value exists for that column or do I let it go all the way to the database and let it throw an exception and handle that in my application?

我不喜欢后者,因为答案 - 是有意义的处理尽可能多的,你可以在业务层到达数据库之前。但它也是我的应用程序是通过实现对所有表基本的CRUD操作驱动程序有点问题。所以,为了让我强制唯一性在业务层,我需要创建一个程序每一个可能的约束,所以我可以做一个更新之前,查找或插入。这似乎只是单调乏味......而我复制的业务规则,因为他们在数据库和业务层都存在,因此,如果有新的变化,我不仅要改变数据库,但我必须要改变的程序和应用code。

I don't like the latter answer- because it makes sense to handle as much as you can in the business layer before it reaches the database. But it's also somewhat problematic as my application is driven by procedures that implement basic CRUD operations on all tables. So in order for me to enforce uniqueness in the business layer, I need to create a procedure for every possible constraint so I can do a look up before an update or insert. That just seems tedious... and I'm duplicating business rules as they exist in both the database and the business layer, so if something changes, not only do I have to change the database, but I have to change the procedure and the application code.

那么,有没有一个正确的方式或强制执行申请code这些数据库约束或者我应该寻找一种方法来捕获由数据库和present他们抛出的异常的好方法人性化方法是什么?

So is there a right way or a good way to enforce these database constraints in application code or should I be looking at a way to catch exceptions thrown by the database and present them in a user-friendly way?

推荐答案

后者的答案是正确的。会使用什么数据库约束的目的,如果你在你的前端应用程序第一次尝试做一切?

The latter answer is correct. What would be the purpose of using database constraints if you try and do everything first in your front end application?

约束的全部点是有以一致的方式的中央位置(DB服务器),该处理尽可能多的数据的验证成为可能,不管如何/在数据到达那里。在前端应用程序的所有重新编写相同的逻辑是多余的,不必要的和复杂的。如果东西在DB(这是你的应用程序认为应该是有效的不再是)约束的变化?你会得到一个例外呢。

The entire point of constraints is to have a central location (the DB server) that handles as much of the data validation as possible, in a consistent fashion, regardless of how/where the data gets there. Writing the same logic all over again in your front end application is redundant, and needlessly complex. What if something changes in the constraint in the DB (something your app thinks should be valid no longer is)? You get an exception anyway.

让数据库处理验证约束和处理您的code中的例外。这让一切都意味着用于访问和更新的数据是一致的,因为数据库处理它。如果有新的变化,你的新的约束更新DB和你的应用程序中自动同步的。

Let the DB handle validating constraints, and handle the exceptions in your code. This allows all means you use to access and update the data to be consistent, since the DB handles it. If something changes, you update the DB with the new constraints and your apps are automatically in synch.

这篇关于强制数据库约束在应用code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆