views:

60

answers:

5

If there are any DBAs out there, I'm making a fairly large piece of software and one of the biggest issues presently is where to put the business logic. While Stored Procedures would be easier to fix on the fly, the processing requirements would probably slow the DB down tremendously. I also don't want to have all of the business logic handled by the application because I want it to be a "self-sustaining entity" that doesn't require the user-front end to operate.

My idea, is to create a service to run on a central server somewhere, and have the clients connect through that. The service would maintain all the business logic and serve as a front-end for all the database operations.

Ideas? Yes? No?

I'm willing to accept that I'm also missing some key concepts and need to read some literature.

+1  A: 

Having all business logic on the server side is fine.
Not having it on the server side is fine, too.

In fact, it's up to you.
If a stored procedure tends to look not sql-ish, you can make a CLR stored procedure.

Here's a similar question.

GSerg
Wicked, thanks for the link.
instantmusic
+3  A: 

What do you mean by "business logic"?

I've seen cases where aggregations and other set-based operations have been done in client code, as well as horrible RBAR operations in SQL that should be somewhere else.

SQL is one tool that has it's place: if you're working through large datasets, JOINs, aggregations etc then SQL is the place to do it. Anything else is slavish obedience to an SOA ideal.

My approach is to consider what the stored proc or SQL is doing: is it part of the middle tier to avoid set based operations in procedural code, or is it lower as pure data integrity/persistence?

If your business logic is 100% set based then you don't need a middle tier (edit: client code based) arguably, unless it's very thin.

gbn
Ahh, to provide a little more info. I would have the database simply maintain data logic. Like, if this field can or cannot be null it would apply said restraint. If deleting this record should cascade to child records, it would handle that. However(for a messy example) if I have a delinquent payment, I might want to record its delinquency. This would be done by the middle-man application. If I have an inconsistency in my inventory, I would have the middle-main handle that. Assuming that's what you're referring to.
instantmusic
@instantmusic: yes. Randy's answer expresses it better than this one. A delinquent payment is just a payment with a different value in the status field (or something) that can not, and should not, be determined by SQL. However, working out the SUM of payments for a certain status would be: the client sends in what statuses to be SUMmed on.
gbn
Right right, so I could expose just the necessary stored procedures to provide information regarding the state of data. i.e. sp_GetCustomerDelinquentTotal(Customer) would simply return the total $$$ that a single customer had delinquent.
instantmusic
@instantmusic: I'd more say "sp_GetCustomerTotal(Customer, Status)" where Status could be overdue, written off, delinquent etc. This way the WHERE and SUM is SQL based, but only the client "understands" Status (just a value in a WHERE to the DB). So one generic stored proc can deal with many statuses. The knowledge/meaning of status is business/middle, but aggregate is SQL/set
gbn
Awesome, thanks tons for your help(and everyone who responded for that matter). I think I know where to start now. I'll do some more reading in general and I should know enough by the end of the day to make an informed decision. Thanks again!!!
instantmusic
@instantmusic: Thank you. One last thing: "sp_GetCustomerTotal" could be considered middle-tier that lives in the DB because it's the correct tool to filter and aggregate lots of data...
gbn
A: 

I'd highly recommend a traditional n-layer approach, where you have at least UI layer, business layer (like a C# assembly or Java equivolent), and data access. See: http://en.wikipedia.org/wiki/Multitier_architecture.

I worked for a company where all the business logic was in the procs, and maintence costs are much higher than they had to be, it limited us to a specific version of sql server, it wasn't scalable, etc. In short, unless your application is a simple throw away kind of thing, I'd not put any business logic in the database.

Andy
"slavish obedience to an SOA ideal."? You mean you did all your set based logic in a middle tier?
gbn
Very little, if any, business logic operates over large sets of data. At least, that's been my experience.I'm fine with letting databases do what databases do best, but if you're writing procs with lots of if statements doing checks on data or other branching, that's logic that is more maintainable if moved to a business layer. If you're in SSMS and adding a C# assembly to your database, you should think carefully about why you're doing so.
Andy
+4  A: 

i would suggest that you keep a keen eye on the difference between what you think of as business logic, and what are the referential integrity constraints.

Make sure all constraints that keep the data meaningfully related are in place at the database layer. i.e. if you need to cascade some deletes, or inserts - and when you need to validate some basic data values in order to have everything make sense... these should all be in the database.

Then decide if the Client, or the middle layer server, or the database is appropriate for any additional business logic.

Randy
+1 good phrasing of business logic vs referential integrity
gbn
Exactly, thats what I had in mind for the database's role. I added more info in gbn's comment that touched on that very thing. Thanks for the clarification though, good to know I'm on the right track.
instantmusic
+1  A: 

Over the years, I've seen client applications come and go, but the database is still there.

So nowadays I use stored procedures for most of the business logic. Three big advantages:

  • Bug fix deployment takes an instant, with no downtime
  • Multi-user by default
  • Far less plumbing code (no data access layer)
Andomar