Where ORMs blur the lines between code and data, how do you decide what logic should be a stored procedure, and what should be coded?

Posted by PhonicUK on Programmers See other posts from Programmers or by PhonicUK
Published on 2012-10-24T10:44:59Z Indexed on 2012/10/24 11:16 UTC
Read the original article Hit count: 184

Take the following pseudocode:

CreateInvoiceAndCalculate(ItemsAndQuantities, DispatchAddress, User);

And say CreateInvoice does the following:

  • Create a new entry in an Invoices table belonging to the specified User to be sent to the given DispatchAddress.
  • Create a new entry in an InvoiceItems table for each of the items in ItemsAndQuantities, storing the Item, the Quantity, and the cost of the item as of now (by looking it up from an Items table)
  • Calculate the total amount of the invoice (ex shipping and taxes) and store it in the new Invoice row.

At a glace you wouldn't be able to tell if this was a method in my applications code, or a stored procedure in the database that is being exposed as a function by the ORM. And to some extent it doesn't really matter.

Now technically none of this is business logic. You're not making any decisions - just performing a calculation and creating records. However some may argue that because you are performing a calculation that affects the business (the total amount to be invoiced) that this isn't something that should be done in a stored procedure and instead should be in code.

So for this specific example - why would it be more appropriate to do one or the other? And where do you draw the line? Or does it even particular matter as long as it's sufficiently well documented?

© Programmers or respective owner

Related posts about design-patterns

Related posts about database-design