0

Trying to work out where certain responsibilities lie with the following example. We have a Project object and a Project can have Time entries booked against it. Each Time entry will have a no. of Hours and a Rate. We then want to create a report that shows a list of projects, the amount of hours booked and the value of that time.

So my question is where should the Time entries come from for the calculations:

public class Project{
   ......
   public IList<Time> TimeEntries {get;set;}
   ......

   public double HoursBooked(){
       this.TimeEntries.Sum(x=>x.Hours);
   }
}

OR

public class Project{

   ......

   public double HoursBooked(IList<Time> timeEntries){
       timeEntries.Sum(x=>x.Hours);
   }
}

Along similar lines we have some Time calculations or a Project that will need to be converted to another Currency should we be passing the Currency in as a parameter or should we have another class purely responsible for Project calculations that has properties for all collections that maybe needed in order to get the final result?

2

The second function looks weird. It has no reason to be a member of project, since it calculates the hours in an arbitry list of TimeEntries, whether they have anything to do with the project or not. The project should be able to handle questions about the project.

Having a setter for the list of TimeEntries is a bit strange as well. Because normally the project would handle TimeEntries added and sometimes being removed from it, and then perhaps using a list to store all those entries, but not visible to the public. The way you do this, someone else has to create and set the list of time entries, which seems to be a project responsibility.

Ask yourself: Who is going to call these functions, and what knowledge should the caller of these functions have?

  • +1. To the OP: Consider the consuming client using these -- how much work do you expect them to do? Your Project should provide a directly usable abstraction for the consuming programmer, not something they have to manipulate in detail. – Erik Eidt Jan 31 '18 at 2:35
  • Time entries can belong to other things besides projects and their addition is handled elsewhere. What we are trying to work our is if we have calculations that get a value for the project that rely on several other lists of data (time, rates, currency exchange rates) where should (in the perfect world) this kind of calculation live. The result is technically a property of a Project (time costs so far) but we need to pull data from other repositories in order to work this out. – Gaz Jan 31 '18 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.