2

I am basically creating a flat View Model for a Timesheet page (ASP.NET MVC) that has a grid for the days of the week. The WorkTime properties should either be an existing WorkTime from the database or null if there is no existing one.

There will only ever be 1 week displayed (Saturday to Friday). and I have the individual properties there so that the syntax in my view should be simpler.

+------+-----+-----+-----+-----+-----+-----+-----+
| Desc | Sat | Sun | Mon | Tue | Wed | Thu | Fri |
+------+-----+-----+-----+-----+-----+-----+-----+
|______|_____|_____|_____|_____|_____|_____|_____|
|______|_____|_____|_____|_____|_____|_____|_____|
|______|_____|_____|_____|_____|_____|_____|_____|
|______|_____|_____|_____|_____|_____|_____|_____|

All of the dates that come from and are persisted to the database have no Time element (all midnight hour) and the Saturday to Friday DateTime properties are all set already.

The properties I am setting

public WorkTime SaturdayWorkTime { get; private set; }
public WorkTime SundayWorkTime { get; private set; }
public WorkTime MondayWorkTime { get; private set; }
public WorkTime TuesdayWorkTime { get; private set; }
public WorkTime WednesdayWorkTime { get; private set; }
public WorkTime ThursdayWorkTime { get; private set; }
public WorkTime FridayWorkTime { get; private set; }

Current iteration...

public DateTime Saturday { get; private set; }
public DateTime Sunday { get; private set; }
public DateTime Monday { get; private set; }
public DateTime Tuesday { get; private set; }
public DateTime Wednesday { get; private set; }
public DateTime Thursday { get; private set; }
public DateTime Friday { get; private set; }

_workTimes = _workTimeRepository.GetByWorkAssignmentID(WorkAssignment.ID, Saturday, Friday);
SaturdayWorkTime = GetWorkTimeForDay(DayOfWeek.Saturday);
SundayWorkTime = GetWorkTimeForDay(DayOfWeek.Sunday);
MondayWorkTime = GetWorkTimeForDay(DayOfWeek.Monday);
TuesdayWorkTime = GetWorkTimeForDay(DayOfWeek.Tuesday);
WednesdayWorkTime = GetWorkTimeForDay(DayOfWeek.Wednesday);
ThursdayWorkTime = GetWorkTimeForDay(DayOfWeek.Thursday);
FridayWorkTime = GetWorkTimeForDay(DayOfWeek.Friday);

with this helper method...

private WorkTime GetWorkTimeForDay(DayOfWeek dow)
{
    return _workTimes.FirstOrDefault(x => x.Date.DayOfWeek == dow);
}
4
  • I'd guess that Saturday and Friday are lower and upper ranges for the items you're retrieving. Why are they properties though, and what are all the other days used for? 0.o Commented Aug 5, 2009 at 22:22
  • 2
    Also, I'm somewhat puzzled on why someone with your SO track record and reputation would need others to make this better than it is now. No offense intended, I'm just surprised. Commented Aug 5, 2009 at 22:27
  • haha... i think I at the point where I'm satisfied with it for the purposes that I need. Sat to Fri are set because I need to reference them in my view when I post back to the server. Commented Aug 5, 2009 at 22:33
  • i may be having a brain fart as well... Commented Aug 5, 2009 at 22:36

5 Answers 5

4

Here's a small start:

private WorkTime GetWorkTimeForDay(DayOfWeek dw)
{
    return workTimes.FirstOrDefault(x => x.Date.DayOfWeek == dw);
}
Sign up to request clarification or add additional context in comments.

Comments

4

Why not create a Dictionary to store these worktimes?

private Dictionary<DayOfWeek, WorkTime> _workTimes;

Populating this dictionary, using _workTimeRepository.GetByWorkAssignmentID, should be very simple.

Comments

3

You could use DayOfWeek enum.

DateTime now = DateTime.Now;
var dayOfWeek = now.DayOfWeek;

link

4 Comments

just make sure you take into account first day of week and localization ;)
Even better(?): var dayOfWeek = DateTime.Now.DayOfWeek;
Huh? How does this answer the question? The OP is wanting to populate a bunch of properties with working hours keyed by day.
I'm aware of the DayOfWeek enum, but I didn't want a bunch of if's to turn into a switch statement =)
0

Instead of having the individual variables, could you have a map from DayOfWeek to WorkTime, and another map from DayOfWeek to DateTime? Sorry, my C# is rusty, so this probably isn't quite right, but if you had that, then it would look more like this:

for (DayOfWeek day : allDays) 
    workTimes(day) = repository.FirstOrDefault(dateTimes(day));

Comments

0

Why not instead just have one WorkTime property, and make it a Dictionary and return the dictionary (instead of your list) from your worktime repository.

3 Comments

although it is possible to do this, but I actually want there to be a null reference for days that do NOT have a work time.
Not in Dictionary = no work time? :) If you want though, you can add them with a null value. Keys cannot be null, but values can.
yea, but i'm thinking about the syntax in my View (using MVC). and I think with the above it would make the view simpler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.