Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:59:47+00:00 2026-06-15T11:59:47+00:00

I’m building an app using DDD, however I’m struggling to understand where you should

  • 0

I’m building an app using DDD, however I’m struggling to understand where you should be instantiating specification classes or making use of them.

My app makes heavy use of booking windows so I have a specification that ensures that a booking window that is about to be added to an aggregate doesn’t overlap with another window that’s currently in the aggregate. Like the following.

    /// <summary>
/// A specification that determines if the window passed in collides with other windows.
/// </summary>
public class BookingTemplateWindowDoesNotCollideSpecification : ISpecification<BookingScheduleTemplateWindow>
{
    /// <summary>
    /// The other windows to check the passed in window against.
    /// </summary>
    private readonly IEnumerable<BookingScheduleTemplateWindow> otherWindows;

    /// <summary>
    /// Initializes a new instance of the <see cref="BookingTemplateWindowDoesNotCollideSpecification" /> class.
    /// </summary>
    /// <param name="otherWindows">The other windows.</param>
    public BookingTemplateWindowDoesNotCollideSpecification(IEnumerable<BookingScheduleTemplateWindow> otherWindows)
    {
        this.otherWindows = otherWindows;
    }

    /// <summary>
    /// Determines whether the window passed in collides with other windows held inside this class.
    /// </summary>
    /// <param name="obj">The obj.</param>
    /// <returns>
    ///  <c>true</c> if [is satisfied by] [the specified obj]; otherwise, <c>false</c>.
    /// </returns>
    public bool IsSatisfiedBy(BookingScheduleTemplateWindow obj)
    {
        return !this.otherWindows.Any(w => obj.DayOfWeek == w.DayOfWeek && w.WindowPeriod.IsOverlap(obj.WindowPeriod));
    }
}

And then i have a method on the aggregate that allows a new window to be added using the specification. The aggregates already persisted windows are passed into the specification constructor.

        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 a part of me is thinking that I should be injecting this specification into the class rather than instantiating directly (as a general rule across my app and not just in this case) as the type of specfication may need to change depending on the state of the entity. But it feels dirty injecting the specification from the MVC layer as if I have another application interface like a REST API later then logic about which specification to use would be duplicated.

How do you make sure that the Specification being used remains flexible while ensuring that the logic about which specification to use doesn’t get duplicated in another application interface.

Is this a case where you would want to inject a factory into an entity and return the specification from there, thus not allowing the domain logic to spill out into a higher layer? Or is there a better/cleaner/simpler way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:59:48+00:00Added an answer on June 15, 2026 at 11:59 am

    It is perfectly acceptable to inject domain services into entities. It is best to make dependencies on services explicit as parameters in the respective method on the aggregate. For example, the AddWindow method could look like this:

      public virtual void AddWindow(ISpecification<BookingScheduleTemplateWindow> nonCollidingWindowSpecification, DayOfWeek dayOfWeek, int startTime, int endTime)
      {
            var bookingWindow = new BookingScheduleTemplateWindow(this){
                                                                           DayOfWeek = dayOfWeek,
                                                                           WindowPeriod = new Range<int>(startTime, endTime)
                                                                       };
    
            if (nonCollidingWindowSpecification.IsSatisfiedBy(bookingWindow))
            {
                this.Windows.Add(bookingWindow);
            }
        }
    

    In this case, the specification acts as a domain service. Now it is up to surrounding infrastructure to pass the appropriate specification. This is where application services come in. An application service establishes a facade over your domain layer and contains methods for specific use cases. This application service, in turn, would be referenced by a controller. It is also possible to have the controller pass the required dependencies, however the encapsulation provided by an application service may be beneficial.

    Sample application service code:

    public class AddWindow(string aggregateId, DayOfWeek dayOfWeek, int startTime, int endTime)
    {
        var aggregate = this.repository.Get(aggregateId);
        var specification = // instantiate specification
        aggregate.AddWindow(specification, dayOfWeek, startTime, endTime);
        this.repository.Commit();
    }
    

    This is typical application service code: gets the appropriate aggregate, instantiates required dependencies if any, and invokes behavior on the aggregate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am trying to understand how to use SyndicationItem to display feed which is
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.