views:

238

answers:

6

Currently we are using check constraints for business rules implementation, but I want to know should we implement business rules in sql or Business logic(c# code) layer. I have searched on the net and finds that check constraints are good to use. please let me know if somebody knows more int detailed abt it. One more thing is that data can be pump in my db using mobile app n also web app.

+3  A: 

YES it is good!

You should really always check your business rules both in your app code (in the business layer), but if ever possible also in your database.

Why? Imagine someone manages to submit some data to your database without using your app - if you have your checks only in the app, those checks are not being applied.

If you have your checks on the database as well, you can make sure the data in the database conforms to at least those simple checks that can be formulated in SQL CHECK CONSTRAINTS.

Definitely use those! You need to try and keep your data quality as high as possible - adding referential integrity, check constraints and unique constraints and so forth on the database helps you do that.

Do not rely on your app alone!

marc_s
but i hav one more is tht don't u think it will create perfomance issues and morever maintenance problem arise when u need to change ur BL and constraitns both, when logic change.
Punit
Any performance it might cost is well invested! And no - I don't think it should cause maintenance problems - any chances should basically start at the database level anyway.
marc_s
+2  A: 

As more "intelligent" is your database, more secure will be the integrity of the data it contains. So, yes, I think this is good and important to implement it.

This puts in lots of advantages: you can ensure that your data will be secure if there are more than one application modifying the data (ex: C# app + Web app + Mobile app ...) and it allow you to make less work in those "secundary" applications. If the database do all the work, apps are only a frontend for the database.

It will be easier in the future to migrate the applications, but will be more dificult to migrate the database. This is an important decision.

j.a.estevan
+2  A: 

You should definitely use CHECK constraints where possible, but I also wouldn't over do it. If there is no possibility of getting data into your database without using your applications, you can be safe with minimal CHECK constraints and heavy business validation.

It can be fairly difficult to define strict business rules in SQL. Stick to data validation in the database, and actual business rules in your application.

Also, try to arrange your schema in such a way that makes it difficult to enter bad data with foreign keys and the like.

Josh Smeaton
+1  A: 

Depends on the constraints

  1. It depends on the constraints
  2. You should also try to avoid (if possible) having the same constraint checked in 2 places - this would imply there is duplicated code in your system, leading to unnecesary complexity.

There are some constraints that can and should be applied in the database, for example foreign key constraints and uniqueness. The database will be able to apply these quickly and efficently.

Other more complex "business" constraints are better applied in the business logic layer. Examples of these might be "customer must have a validated email address before allowing a purchase". These would be complicated and onerous to apply in the database - you'd run the risk of coding your system in SQL which is A Bad Idea.

stevec
+1  A: 

C#. It's much easier to reuse logic in C# than SQL (in my experience) and generally maintain.

Quibblesome
+2  A: 

Yes, check constraints are a valid tool for business rules.

But are you sure you need to use check constraints, or use a supporting table with a foreign key relationship? If you find yourself defining similar check constraints in various places - the answer is yes, this should definitely be a supporting table.

Data integrity is key; there's not much value to a system that will allow a person to store something that is not per business rules if the application is circumvented. It also makes life a lot easier if the logic is in the database for situations where the original app is in C# and the higher-ups decided the market needs a Java/Ruby/Python/etc version.

OMG Ponies
You could easily turn around the argument. What if some of the higher ups decided it different Database is Cheaper, Nicer more trustworthy and wants to switch? Business-Constraints should be checked at your back end.
Robert
@Robert: You misread my answer - I advocate business rules being implemented in the database, rather than the application code.
OMG Ponies
@OMG Ponies, I am in favor of constraint and data integrity checking - but I believe that a database is not the correct place to apply business rules. Business logic belongs into the appropriate layer of software solutions.
Robert