Class Design

I asked a previous question in regards to class design. Jim, if you are reading this, you know which one.
I am planning out an application to create invoices and will have two classes, invoice and invoiceline.
Invoiceline will read the entries from the database.

qty desc rate total
1   code change 35 35
2   more code changes 35 70

My questions is in regards to the calculating the total, how should this be handled?
1. Have a method that executes a sproce and returns the total?
2. Loop over the line items when the data is being read from the database?
Would total be a property of the invoice class?
Who is Participating?
badbearontourConnect With a Mentor Commented:
Hi There

looking on the face of it, I would have a method in the InvoiceLines class that loops through the invoice lines totallng the values. Returning that value to the Invoice Class and storing it in an attribute of that class and presenting it throug the get() method of the attribute.

using double Total{


If you're building a collection of Invoice objects already from your database, your underlying .NET collection should have a "Count" property already which you can use. If you aren't necessarily building the collection of objects up front, then a sproc that returns the count is probably the fastest way to get the count.
TSmoothConnect With a Mentor Commented:
I completely misread the question, pls disregard my first post as I thought you were looking for a total count of items. Sorry about that.

In addition to badbearontour's suggestion above, you could also set the return value or an output parameter of your stored procedure that get's the invoices to the total and then set a member variable of your class to this value. In general, calculating an aggregate is going to be faster in SQL Server than looping through it in code I believe, but doing it that way would mean that you would need to explicity account for any invoices added to the collection in code without being stored and then requeried from the database.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

JimBrandleyConnect With a Mentor Commented:
I would say it depends on what you want to do with it.
Scenario 1: You just want a summary of what your billing is so far for the month. Now it is faster to do it on the DB server. You can use a stored procedure or simply a select to build you a client list with totals.

Scenario 2: It's the end of the month and time to do the billing. Now, it's much faster to add up the total from the list of line items already in your application's memory than going for another round trip to the database.

I missed the last bit. The cleanest place for the property would be the invoice class.

joesthebighmoeConnect With a Mentor Commented:
I would concur with the bad bear on tour (where might you be on tour? Sorry I digress).
Putting logic in your objects allows you to easily test your logic without a database which is nice if you are into that whole ease-of-testing-thing via OO design. It also easily allows you to use the same functionality when building an invoice from scratch, possibly before it is ever put in the database. I think TSmooth is also correct, however if this is a question about proper class design of what I am assuming to be a business object, then the object would be the right place to put this logic.
JRockFLAuthor Commented:
Awesome replies, thank you for everything!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.