I am building an application using DDD, however I am trying to figure out where you should create specification classes or use them.
My application makes heavy use of booking windows, so I have a specification that ensures that the booking window to be added to the unit does not overlap with the other window that is currently in the aggregate. As shown below.
And then I have a method in combination that allows me to add a new window using the specification. Aggregates that have already been saved, windows are passed to the specification designer.
public virtual void AddWindow(DayOfWeek dayOfWeek, int startTime, int endTime) { var nonCollidingWindowSpecification = new BookingTemplateWindowDoesNotCollideSpecification(this.Windows); var bookingWindow = new BookingScheduleTemplateWindow(this){ DayOfWeek = dayOfWeek, WindowPeriod = new Range<int>(startTime, endTime) }; if (nonCollidingWindowSpecification.IsSatisfiedBy(bookingWindow)) { this.Windows.Add(bookingWindow); } }
What I'm struggling with is that part of me thinks that I should introduce this specification into the class, and not create it directly (as a general rule for my application, and not just in this case) as a type, a specification may be required change depending on the state of the object. But it feels dirty introducing the specification from the MVC level, as if I had a different application interface such as the REST API, later the logic that the duplicate specification would be used about.
How do you make sure that the specification used remains flexible, ensuring that the logic about which specification will be used is not duplicated in another application interface.
Is this the case when you want to insert a factory into an object and return the specification there, not allowing the domain logic to go to a higher level? Or is there a better / cleaner / easier way to do this?
source share