views:

83

answers:

5

Let's assume that an application has all necessary business rules in the model / presentation layer and that they work fine. My question is one of whether or not redundant business rules (i.e. a span of two dates cannot overlap any other existing spans) should be used in a repository like SQL Server.

Is it necessary/beneficial to add a constraint in SQL Server that enforces this rule? On the one hand, it prevents anyone (including DBAs) from inadvertantly breaking business rules when they bypass the application. In addition, we already have some forms of business rules in the repository via primary and foreign keys. On the other hand, the duplicated rules require additional time to develop and maintain.

This question spans many different technologies so I intentionally kept the tags generic.

A: 

Depends on your application - keep the logic in the application code if it's intended to be db agnostic. Otherwise, the business rules are better suited in the database.

OMG Ponies
+1  A: 

You said it yourself... what enforces the business rules on the back door (i.e. the DBA poking data). If you fear this will happen, then pay the price. If not, then leave it off so the rules enforced elsewhere will not be redundant.

dacracot
+4  A: 

You will generally find it very difficult to maintain business rules in multiple places by hand.

I have made good use of code generation on some projects to generate some types of rules from requirement models. For example, I might have the requirement "First name shall not exceed 50 characters". I model that requirement in UML in a structured manner, then generate UI code to cap the input at 50 characters, business logic to enforce that same limit (never trust the UI!), and DDL/ORM mapping file to specify the column width in the DB.

That same idea could be extended to model more complex rules, and generate appropriate enforcement code into each layer of the application.

Eric J.
+1 for "very difficult to maintain business rules in multiple places by hand". It's so often forgotten!
CesarGon
It's hard enough to keep the UI and Business Objects in sync for a large project, let alone having the rules in a third place.
Eric J.
+4  A: 

If you care about your data, you will put the required rules there in preference to anywhere else. Data is affected from many places beyond the applciations. Putting rules only inthe business layer is short-sighed and leads to serious data integrity issues atat can be horrible to try to fix. Rules concerning the data, belong in the database.

HLGEM
Are you saying that rules concerning data belong *only* in the database, or that they should be in both the business and data layers? If the later, what approach have you used to keep them in sync on large projects?
Eric J.
If they are required, I put them in the database first. It is more critical that these contraints go into the database than the front end. I let the application teams know if constraints have changed, so they can adjust if need be. Usually these project will have a database person and an application programmer assigned, so we keep things coordinated.If the rules may very depending on what the user has done, they belong in the business layer. This is data that is not that often entered through imports or ad hox queries and so it is appropriate in the business layer.
HLGEM
Exactly the type of feedback I'm looking for - thanks HLGEM (and others).
Mayo
+2  A: 

Business rule or data integrity rule?

My experience (mostly large corporate, we're not talking software house or hobby shop here) is that your database will outlast the application. And eventually have other apps talking to it. And someone, somewhere will break the database if it has no rules (of whatever kind) in place.

gbn