Class Design

Posted on 2007-10-02
Medium Priority
Last Modified: 2010-04-15
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?
Question by:JRockFL
LVL 16

Expert Comment

ID: 19997673
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.

Accepted Solution

badbearontour earned 800 total points
ID: 19997689
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{


LVL 16

Assisted Solution

TSmooth earned 400 total points
ID: 19997798
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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 22

Assisted Solution

JimBrandley earned 400 total points
ID: 19998532
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.

LVL 22

Expert Comment

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


Assisted Solution

joesthebighmoe earned 400 total points
ID: 19998596
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.

Author Comment

ID: 20003402
Awesome replies, thank you for everything!

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question