public static PartialDateRange Map(this IHavePartialDateRangeEntity entity) { if (entity == null) { return(null); } // 1st possibility, no start or end date but is current. if (entity.startDate == null && entity.startDateParts == null && entity.endDate == null && entity.endDateParts == null && entity.isCurrent) { return(new PartialDateRange()); } // 2nd possibility, a start date but no end date/is current. if (entity.startDate != null && entity.startDateParts != null && entity.endDate == null && entity.endDateParts == null) { return(new PartialDateRange(GetPartialDate(entity.startDate.Value, entity.startDateParts.Value))); } // 3rd possibility, no start date but does have an end date. if (entity.startDate == null && entity.startDateParts == null && entity.endDate != null && entity.endDateParts != null) { return(new PartialDateRange(null, GetPartialDate(entity.endDate.Value, entity.endDateParts.Value))); } // 4th possibility, start date and end date. if (entity.startDate != null && entity.startDateParts != null && entity.endDate != null && entity.endDateParts != null) { return(new PartialDateRange(GetPartialDate(entity.startDate.Value, entity.startDateParts.Value), GetPartialDate(entity.endDate.Value, entity.endDateParts.Value))); } // No other combination supported. return(null); }
public static void MapTo(this PartialDateRange partialDateRange, IHavePartialDateRangeEntity entity) { if (partialDateRange == null) { entity.startDate = null; entity.startDateParts = null; entity.endDate = null; entity.endDateParts = null; entity.isCurrent = false; return; } // Convert the dates first to deal with errors etc. DateTime? startDate; DateParts?startDateParts; DateTime? endDate; DateParts?endDateParts; partialDateRange.Start.MapTo(out startDate, out startDateParts); partialDateRange.End.MapTo(out endDate, out endDateParts); // 1st possibility, no start or end date. if (startDate == null && endDate == null) { entity.startDate = null; entity.startDateParts = null; entity.endDate = null; entity.endDateParts = null; entity.isCurrent = true; return; } // 2nd possibility, a start date but no end date. if (startDate != null && endDate == null) { entity.startDate = startDate; entity.startDateParts = (byte?)startDateParts; entity.endDate = null; entity.endDateParts = null; entity.isCurrent = true; return; } // 3rd possibility, no start date but does have an end date. if (startDate == null) { entity.startDate = null; entity.startDateParts = null; entity.endDate = endDate; entity.endDateParts = (byte?)endDateParts; entity.isCurrent = false; return; } // 4th possibility, start date and end date. entity.startDate = startDate; entity.startDateParts = (byte?)startDateParts; entity.endDate = endDate; entity.endDateParts = (byte?)endDateParts; entity.isCurrent = false; }