|
-
Oct 4th, 2011, 10:17 AM
#1
Thread Starter
PowerPoster
From singular to collection but select single
Hi.
originally the "solution" had a singular class which had:
DateFrom
DateTo
StartAt
EndAt
properties. all DateTime values
constructor:
Code:
ID = Guid.Empty;
Start = StringUtils.GetBaseTime().Add(new TimeSpan(0, 0, 1));
Finish = StringUtils.GetBaseTime().Add(new TimeSpan(23, 59, 59));
FinishDate = StringUtils.GetBaseTime().Add(new TimeSpan(18250, 0, 0, 0));
StartDate = StringUtils.GetBaseTime().Add(new TimeSpan(0, 0, 1));
Thats fine.
There is also a method called "IsValid()" which does:
Code:
public bool IsValid()
{
DateTime now = DateTime.Now;
if (now.TimeOfDay < Start.TimeOfDay)
return false;
if (now.TimeOfDay > Finish.TimeOfDay)
return false;
var logicalDay = EndOfDayHelper.GetLogicalDay(now);
if (logicalDay.Start < StartDate)
return false;
if (logicalDay.Start > FinishDate)
return false;
switch (logicalDay.Start.DayOfWeek)
{
case DayOfWeek.Sunday:
return (Sunday);
case DayOfWeek.Monday:
return (Monday);
case DayOfWeek.Tuesday:
return (Tuesday);
case DayOfWeek.Wednesday:
return (Wednesday);
case DayOfWeek.Thursday:
return (Thursday);
case DayOfWeek.Friday:
return (Friday);
case DayOfWeek.Saturday:
return (Saturday);
}
return true;
}
thats fine.
but now, there is a requirement to be able to hold a collection of these dates/times. Thats ok, ive created a collection to do so.
Now we need to do the same IsValid() validation but only selecting a singular item, making sure that the item in question from the collection is suited for the present time.
my query seems to work but i am not entirely sure how to handle it for the scenario of Dates (with times) only, so the same logic as above is applied.
please ignore the duplicate code for now.
Code:
public bool IsValid()
{
DateTime now = DateTime.Now;
var logicalDay = EndOfDayHelper.GetLogicalDay(now);
var validityPeriodsQueryable = Enumerable.Cast<ValidityPeriodItem>(ValidityPeriods);
if (validityPeriodsQueryable.Count() == 0)
{
switch (logicalDay.Start.DayOfWeek)
{
case DayOfWeek.Sunday:
return (Sunday);
case DayOfWeek.Monday:
return (Monday);
case DayOfWeek.Tuesday:
return (Tuesday);
case DayOfWeek.Wednesday:
return (Wednesday);
case DayOfWeek.Thursday:
return (Thursday);
case DayOfWeek.Friday:
return (Friday);
case DayOfWeek.Saturday:
return (Saturday);
}
return true;
}
var tmpEligibleItem = (from ValidityPeriodItem currentItem in validityPeriodsQueryable
where now.TimeOfDay > currentItem.Start.TimeOfDay && now.TimeOfDay < currentItem.Finish.TimeOfDay
orderby currentItem.Start ascending // always the most present/current one. NOT in the future.
select currentItem
);
var eligibleItem = tmpEligibleItem.FirstOrDefault();
if (eligibleItem != null) // we have rules to apply
{
if (eligibleItem.AllowUsage.HasValue && eligibleItem.AllowUsage.Value == false)
{
return false;
}
if (now.TimeOfDay < eligibleItem.Start.TimeOfDay)
return false;
if (now.TimeOfDay > eligibleItem.Finish.TimeOfDay)
return false;
if (logicalDay.Start < eligibleItem.StartDate)
return false;
if (logicalDay.Start > eligibleItem.FinishDate)
return false;
switch (logicalDay.Start.DayOfWeek)
{
case DayOfWeek.Sunday:
return (Sunday);
case DayOfWeek.Monday:
return (Monday);
case DayOfWeek.Tuesday:
return (Tuesday);
case DayOfWeek.Wednesday:
return (Wednesday);
case DayOfWeek.Thursday:
return (Thursday);
case DayOfWeek.Friday:
return (Friday);
case DayOfWeek.Saturday:
return (Saturday);
}
return true;
}
} else { /*.... old code as above.... */}
}
I hope it makes sense, but understand the confusion/understanding of my explanation!
basically, the same logic should apply as "before". but because we are now dealing with a collection, I only ever want to get a singular (if applicable) entity out from the collection and perform the logic based on that entity. but the query I am using, not sure if it is the correct query. I know it works for the time of day but im sure I can improve that some how, and also fix any issues which involves dates.
-
Oct 4th, 2011, 11:50 AM
#2
Re: From singular to collection but select single
I think you over thunk it... the IsValid should be a method of your singular object... the collection has no bearing on the issue. Then all you need to do is get the item from your collection, and test its IsValid method and see what it returns. The IsValid method shouldn't care if it part of a collection or not, it simply deals with the data within the instance of that singular object.
-tg
EDIT - and no, you should NOT have a IsValid method on the collection itself... to do so would semantically indicate that it is to check to see if the collection is valid... which means.... what? See? So leave the IsValid in the individual class where it belongs.
-
Oct 4th, 2011, 11:59 AM
#3
Thread Starter
PowerPoster
Re: From singular to collection but select single
agreed but its just how things are currently :-/
lets see...
-
Oct 4th, 2011, 09:18 PM
#4
Re: From singular to collection but select single
So, are you saying that you want all the items that are valid from the collection or just one item from the collection that is valid? If you want multiple then use Where and if you want one then use FirstOrDefault, e.g.
csharp Code:
var validItems = myCollection.Where(i => IsValid(i))
If you want one item then you'll presumably want to slip an OrderBy in there too.
-
Oct 5th, 2011, 02:46 AM
#5
Thread Starter
PowerPoster
Re: From singular to collection but select single
thanks.
its not quite the syntax/code im looking for but rather the logic.
as it happens, they dont want it the way i had thought. typical... and really annoying me as there is absolutely no spec but some how have to be psychic on the requirements.
such a complicated system.... urgh.
originally they had it where it was doing what its currently doing - but for a singular "period" - a period is just the StartDate/FinishDate/StartAt/EndAt properties and does some validation as shown earlier.
now they want to cater for multiple periods. one would think that its simple - where we only select the most relevant period, from the collection, which is where a period is suitable for the current time, then perform the same validation as before.
obviously not....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|