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 6871905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:51:39+00:00 2026-05-27T03:51:39+00:00

So, I’ve looked at some posts about the Specification Pattern here, and haven’t found

  • 0

So, I’ve looked at some posts about the Specification Pattern here, and haven’t found an answer to this one yet.

My question is, in an n-layered architecture, where exactly should me Specifications get “newed” up?

  1. I could put them in my Service Layer (aka, Application layer it’s sometimes called… basically, something an .aspx code-behind would talk to), but I feel like by doing that, I’m letting business rules leak out of the Domain. If the Domain objects are accessed some other way (besides the Service Layer), the Domain objects cannot enforce their own business rules.

  2. I could inject the Specification into my Model class via constructor injection. But again, this feels “wrong”. I feel like the only thing that should be injected into Model classes are “services”, like Caching, Logging, dirty-flag tracking, etc… And if you can avoid it, to use Aspects instead of littering the constructors of the Model classes with tons of service interfaces.

  3. I could inject the Specification via method injection (sometimes referred to as “Double Dispatch”???), and explicitly have that method encapsulate the injected Specification to enforce its business rule.

  4. Create a “Domain Services” class, which would take a Specification(s) via constructor injection, and then let the Service Layer use the Domain Service to coordinate the Domain object. This seems OK to me, as the rule enforced by the Specification is still in the “Domain”, and the Domain Service class can be named very much like the Domain object it’s coordinating. The thing here is I feel like I’m writing a LOT of classes and code, just to “properly” implement the Specification pattern.

Add to this, that the Specification in question requires a Repository in order to determine whether it’s “satisfied” or not.

This could potentially cause performance problems, esp. if I use constructor injection b/c consuming code could call a property that perhaps wraps the Specification, and that, in turn is calling the database.

So any ideas/thoughts/links to articles?

Where is the best place to new up and use Specifications?

  • 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-05-27T03:51:40+00:00Added an answer on May 27, 2026 at 3:51 am

    Short answer:

    You use Specifications mainly in your Service Layer, so there.

    Long answer:
    First of all, there’s two questions here:

    Where should your specs live, and where should they be new’d up?

    Just like your repository interfaces, your specs should live in the domain layer, as they are, after all, domain specific. There’s a question on SO that discusses this on repository interfaces.

    Where should they be new’d up though? Well, I use LinqSpecs on my repositories and mostly ever have three methods on my repository:

    public interface ILinqSpecsRepository<T>
    {
        IEnumerable<T> FindAll(Specification<T> specification);
        IEnumerable<T> FindAll<TRelated>(Specification<T> specification, Expression<Func<T, TRelated>> fetchExpression);
        T FindOne(Specification<T> specification);
    }
    

    The rest of my queries are constructed in my service layer. That keeps the repositories from getting bloated with methods like GetUserByEmail, GetUserById, GetUserByStatus etc.
    In my service, I new-up my specs and pass them to the FindAll or FindOne methods of my repository. For example:

    public User GetUserByEmail(string email)
    {
        var withEmail = new UserByEmail(email); // the specification
        return userRepository.FindOne(withEmail);
    }
    

    and here is the Specification:

    public class UserByEmail : Specification<User>
    {
        private readonly string email;
    
        public UserByEmail(string email)
        {
            this.email = email;
        }
    
        #region Overrides of Specification<User>
    
        public override Expression<Func<User, bool>> IsSatisfiedBy()
        {
            return x => x.Email == email;
        }
    
        #endregion
    }
    

    So to answer your question, specs are new’d up in the service layer (in my book).

    I feel like the only thing that should be injected into Model classes
    are “services”

    IMO you should not be injecting anything into domain entities.

    Add to this, that the Specification in question requires a Repository
    in order to determine whether it’s “satisfied” or not.

    That’s a code smell. I would review your code there. A Specification should definitely not require a repository.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.