Created
May 22, 2015 16:44
-
-
Save ToJans/750afe22dda0c98a457e to your computer and use it in GitHub Desktop.
Example for a question in the DDD-CQRS newsgroup: https://groups.google.com/d/msg/dddcqrs/hyyt8Jvq6J0/yAQkmlgo6wcJ
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace RicardoDeSilva | |
{ | |
namespace Contracts | |
{ | |
public enum ScheduleGeneratorType { Daily, Hourly /*, Add others */ }; | |
public class DeliveryDetails | |
{ | |
public string Id { get; protected set; } | |
public string Description { get; protected set; } | |
public DateTime? DeliverStartingFrom { get; protected set; } | |
public ScheduleGeneratorType ScheduleGenerator { get; protected set; } | |
} | |
} | |
namespace Modules | |
{ | |
using Contracts; | |
public interface IGenerateSchedules | |
{ | |
IEnumerable<DateTime> StartingFrom(DateTime startDate); | |
} | |
public static class ScheduleModule | |
{ | |
public static IGenerateSchedules GetGenerator(ScheduleGeneratorType @type) | |
{ | |
switch (@type) | |
{ | |
case (ScheduleGeneratorType.Daily): return new DailyScheduleGenerator(); | |
case (ScheduleGeneratorType.Hourly): return new HourlyScheduleGenerator(); | |
default: return UnknownScheduleGenerator; | |
} | |
} | |
class HourlyScheduleGenerator : IGenerateSchedules | |
{ | |
IEnumerable<DateTime> IGenerateSchedules.StartingFrom(DateTime startDate) | |
{ | |
var currentDate = startDate; | |
while (true) | |
{ | |
// this will probably be way more complicated | |
if (currentDate.TimeOfDay.TotalHours >= 8.5 && | |
currentDate.TimeOfDay.TotalHours < 17) | |
{ | |
yield return currentDate; | |
} | |
currentDate += TimeSpan.FromHours(1); | |
} | |
} | |
} | |
class DailyScheduleGenerator : IGenerateSchedules | |
{ | |
IEnumerable<DateTime> IGenerateSchedules.StartingFrom(DateTime startDate) | |
{ | |
var currentDate = startDate; | |
while (true) | |
{ | |
// this will probably be way more complicated | |
if (currentDate.DayOfWeek != DayOfWeek.Saturday && | |
currentDate.DayOfWeek != DayOfWeek.Sunday) | |
{ | |
yield return currentDate; | |
} | |
currentDate += TimeSpan.FromDays(1); | |
} | |
} | |
} | |
class NopScheduleGeneratorClass : IGenerateSchedules | |
{ | |
IEnumerable<DateTime> IGenerateSchedules.StartingFrom(DateTime startDate) | |
{ | |
throw new NotImplementedException(); | |
} | |
} | |
public static IGenerateSchedules UnknownScheduleGenerator = new NopScheduleGeneratorClass(); | |
} | |
} | |
namespace Domain | |
{ | |
using Contracts; | |
using Modules; | |
public class Delivery | |
{ | |
void ProvisionDelivery(DeliveryDetails deliveryDetails) | |
{ | |
var generateSchedule = ScheduleModule.GetGenerator(deliveryDetails.ScheduleGenerator); | |
Guard.Against(generateSchedule == ScheduleModule.UnknownScheduleGenerator, "Unknown schedule generator"); | |
// a later starting date might be picked for whatever reason (f.e. not in stock etc) | |
var earliestDate = deliveryDetails.DeliverStartingFrom.HasValue ? deliveryDetails.DeliverStartingFrom.Value : DateTime.Now; | |
// here other dates might be picked (f.e. national holiday) | |
var scheduledDate = generateSchedule.StartingFrom(earliestDate).First(); | |
// everything is ok, persist | |
this.DeliveryWasProvisioned(deliveryDetails.Id, deliveryDetails.Description, scheduledDate); | |
} | |
private void DeliveryWasProvisioned(string Id, string Description, DateTime scheduledDate) | |
{ | |
// TODO | |
throw new NotImplementedException(); | |
} | |
} | |
} | |
public static class Guard | |
{ | |
public static void Against(bool condition, string message) | |
{ | |
if (condition) | |
{ | |
throw new InvalidOperationException(message); | |
} | |
} | |
public static void That(bool condition, string message) | |
{ | |
Guard.Against(!condition, message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment