patterncsharpMinor
similar code in several methods
Viewed 0 times
codeseveralsimilarmethods
Problem
I have method which checks period and return schedules:
Consider the
The similar code have all methods above. How to rewrite it? May be, there is some pattern for this?
And, the second, now I want that
public IEnumerable GetSchedulesForPeriod(PeriodEvent period, string tab = "")
{
switch (period)
{
case PeriodEvent.Today:
return GetTodaySchedules(tab);
case PeriodEvent.Tomorrow:
return GetTomorrowSchedules(tab);
case PeriodEvent.Week:
return GetWeekSchedules(tab);
case PeriodEvent.FewWeek:
return GetFewWeekSchedules(tab);
case PeriodEvent.Month:
return GetMonthSchedules(tab);
case PeriodEvent.All:
return GetAllFromTodaySchedules(tab);
default:
return GetTodaySchedules(tab);
}
}Consider the
GetTodaySchedules and GetTomorrowSchedules:private IEnumerable GetTodaySchedules(string tab)
{
var today = DateTime.Now.Date;
var result = Database.EventSchedules.Where(s => s.RecurrenceStart.Value.Date = today &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(
c => c.EventCategory.Name == tab)).ToList();
return result.Where(s => Evaluator.CheckDate(s, today)).ToList();
}
private IEnumerable GetTomorrowSchedules(string tab)
{
var today = DateTime.Now.AddDays(1).Date;
var result = Database.EventSchedules.Where(s => s.RecurrenceStart.Value.Date = today &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(
c => c.EventCategory.Name == tab)).ToList();
return result.Where(s => Evaluator.CheckDate(s, today)).ToList();
}The similar code have all methods above. How to rewrite it? May be, there is some pattern for this?
And, the second, now I want that
tab checks only if it filled and introduce new var called placeName (also check if filled):Solution
You can compose query predicates by applying several where clauses in a row.
The predicates must be passed as
See LINQ to Entities: Combining Predicates on Microsoft Docs.
Create a general schedules query like this
And then pass it the appropriate individual date predicate like this
The predicates must be passed as
Expression> for composition.See LINQ to Entities: Combining Predicates on Microsoft Docs.
Create a general schedules query like this
private IEnumerable GetSchedules(
string tab,
Expression> dateCondition)
{
var result = Database.EventSchedules
.Where(s => s.BaseEvent.IsApproved.Value &&
!s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(c => c.EventCategory.Name == tab))
.Where(dateCondition)
.ToList();
return result.Where(s => Evaluator.CheckDate(s, today)).ToList();
}And then pass it the appropriate individual date predicate like this
private IEnumerable GetTodaySchedules(string tab) {
var today = DateTime.Now.Date;
return GetSchedules(
tab,
s => s.RecurrenceStart.Value.Date = today);
}
private IEnumerable GetTomorrowSchedules(string tab) {
var tomorrow = DateTime.Now.AddDays(1).Date;
return GetSchedules(
tab,
s => s.RecurrenceStart.Value.Date = tomorrow);
}Code Snippets
private IEnumerable<EventSchedule> GetSchedules(
string tab,
Expression<Func<EventSchedule, bool>> dateCondition)
{
var result = Database.EventSchedules
.Where(s => s.BaseEvent.IsApproved.Value &&
!s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(c => c.EventCategory.Name == tab))
.Where(dateCondition)
.ToList();
return result.Where(s => Evaluator.CheckDate(s, today)).ToList();
}private IEnumerable<EventSchedule> GetTodaySchedules(string tab) {
var today = DateTime.Now.Date;
return GetSchedules(
tab,
s => s.RecurrenceStart.Value.Date <= today &&
s.RecurrenceEnd.Value.Date >= today);
}
private IEnumerable<EventSchedule> GetTomorrowSchedules(string tab) {
var tomorrow = DateTime.Now.AddDays(1).Date;
return GetSchedules(
tab,
s => s.RecurrenceStart.Value.Date <= tomorrow &&
s.RecurrenceEnd.Value.Date >= tomorrow);
}Context
StackExchange Code Review Q#11201, answer score: 4
Revisions (0)
No revisions yet.