forked from ardalis/pluralsight-ddd-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding DateTimeRangeOffset to SharedKernel
- Loading branch information
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
SharedKernel/src/PluralsightDdd.SharedKernel/DateTimeOffsetRange.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Ardalis.GuardClauses; | ||
|
||
namespace PluralsightDdd.SharedKernel | ||
{ | ||
public class DateTimeOffsetRange : ValueObject | ||
{ | ||
public DateTimeOffset Start { get; private set; } | ||
public DateTimeOffset End { get; private set; } | ||
|
||
public DateTimeOffsetRange(DateTimeOffset start, DateTimeOffset end) | ||
{ | ||
// Ardalis.GuardClauses supports extensions with custom guards per project | ||
Guard.Against.OutOfRange(start, nameof(start), start, end); | ||
Start = start; | ||
End = end; | ||
} | ||
|
||
public DateTimeOffsetRange(DateTimeOffset start, TimeSpan duration) : this(start, start.Add(duration)) | ||
{ | ||
} | ||
|
||
public int DurationInMinutes() | ||
{ | ||
return (int)Math.Round((End - Start).TotalMinutes, 0); | ||
} | ||
|
||
public DateTimeOffsetRange NewDuration(TimeSpan newDuration) | ||
{ | ||
return new DateTimeOffsetRange(this.Start, newDuration); | ||
} | ||
|
||
public DateTimeOffsetRange NewEnd(DateTime newEnd) | ||
{ | ||
return new DateTimeOffsetRange(this.Start, newEnd); | ||
} | ||
|
||
public DateTimeOffsetRange NewStart(DateTime newStart) | ||
{ | ||
return new DateTimeOffsetRange(newStart, this.End); | ||
} | ||
|
||
public static DateTimeOffsetRange CreateOneDayRange(DateTime day) | ||
{ | ||
return new DateTimeOffsetRange(day, day.AddDays(1)); | ||
} | ||
|
||
public static DateTimeOffsetRange CreateOneWeekRange(DateTime startDay) | ||
{ | ||
return new DateTimeOffsetRange(startDay, startDay.AddDays(7)); | ||
} | ||
|
||
public bool Overlaps(DateTimeOffsetRange dateTimeRange) | ||
{ | ||
return this.Start < dateTimeRange.End && | ||
this.End > dateTimeRange.Start; | ||
} | ||
|
||
protected override IEnumerable<object> GetEqualityComponents() | ||
{ | ||
yield return Start; | ||
yield return End; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
SharedKernel/src/PluralsightDdd.SharedKernel/Guards/CustomGuards.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Ardalis.GuardClauses | ||
{ | ||
public static class CustomGuards | ||
{ | ||
public static DateTimeOffset OutOfRange(this IGuardClause guardClause, DateTimeOffset input, string parameterName, DateTimeOffset rangeFrom, DateTimeOffset rangeTo) | ||
{ | ||
return OutOfRange<DateTimeOffset>(guardClause, input, parameterName, rangeFrom, rangeTo); | ||
} | ||
|
||
// https://github.com/ardalis/GuardClauses/blob/master/src/GuardClauses/GuardClauseExtensions.cs#L255 | ||
private static T OutOfRange<T>(this IGuardClause guardClause, T input, string parameterName, T rangeFrom, T rangeTo) | ||
{ | ||
Comparer<T> comparer = Comparer<T>.Default; | ||
|
||
if (comparer.Compare(rangeFrom, rangeTo) > 0) | ||
{ | ||
throw new ArgumentException($"{nameof(rangeFrom)} should be less or equal than {nameof(rangeTo)}"); | ||
} | ||
|
||
if (comparer.Compare(input, rangeFrom) < 0 || comparer.Compare(input, rangeTo) > 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(parameterName, $"Input {parameterName} was out of range"); | ||
} | ||
|
||
return input; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters