C# – Where to store formulas and formula “constants”

cobject-oriented

I want to implement a database driven payroll system using CSharp. I already did it once, but the implementation sucks so I want to do it again and do it right.

My first thought is to create a static class calculator which would simply hold all the formulas, one private method per formula. One main public method "Calculate" which would take a ref Employee type object as a parameter. Take data from the object, apply the formulas and set the objects properties, but my guess is this would probably suck.

One class with a bunch of properties for holding all the "(algebraic) constants". One class containing and managing the object. This class would deserialize the class holding all the data and serialize on app exit. The calculator would use this class to get data and apply the formulas to make changes to the "Employee". These "constants" would be changable from inside the application.

Thoughts ?

I am not good when it comes to structuring applications and a lot of my implementations end up sucking… I realize that I hate it when I need to make changes to the application.

Thanks

Best Answer

Here are a couple suggestions to help you think in new directions:

First, it seems like you're trying to tie your calculation functionality specifically to your Employee class. That seems unnecessarily specific. Why should your calculator class care whether the object it's working on represents an employee or something else? This seems like a natural place to use an interface to define the functionality that your calculator needs to apply a formula, without caring what the thing that implements the interface actually represents. And the functionality that your calculator needs is probably pretty simple: it just needs to be able to get named values and possibly also set named values. If you have a formula like:

annualVacationDays = 10 + yearsOfService + bonusDays

then it seems like you might need a Calculable interface that has a function like valueForkey(key) where key is a string, so that the calculator can fetch values for yearsOfService and bonusDays in order to do its work. And the interface should also have a setValueForKey(value, key) method so that it can store the result of the formula for the annualVacationDays key. But the only things the calculator needs to do its work are those methods -- it shouldn't care what kind of object it is.

Second, given that the point of OOP is that you can combine data and the operations on that data, separating the formulas from the class that applies them doesn't make a lot of sense to me. Let each formula be able to apply itself to an object that implements the Calculable interface.

Related Topic