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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:37:10+00:00 2026-05-24T12:37:10+00:00

Is passing specification object is overkill in Repository I am asking this because, if

  • 0

Is passing specification object is overkill in Repository

I am asking this because, if we pass specification object in method like FindCustomersCreatedToday,

class CustomerRepository
{
     public List<Customer> FindCustomersCreatedToday(ISpecification ISCreatedToday)
     {
         List<Customer> customers= Load all customers from db;

         List<Customer> newList=new List<>();

         foreach(var customer in customers)
         {  
              if(ISCreatedToday.SatisfiedBy(customer)
              {
                  newList.Add(customer);
              }
         }
     }

}

I above implementation in most of the sites i saw that, they fetch all entities from database and loop over them and pass each of them in to specification, but i don’t like the idea of loading all entities at once and then create a new filtered list.

Suppose if i have 10000 customers and only 10 passes this criteria.

Is that not over kill to pass specification ?

  • 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-24T12:37:11+00:00Added an answer on May 24, 2026 at 12:37 pm

    Yes it is definitely an overkill if you expect a lot of customers. You can use information in the instance of your specification to generate appropriate SQL/HQL or ICreteria (assuming you use NHibernate).

    public IList<Customer> FindCustomers(CreationDateRangeSpecification spec) {
        ICriteria c = _nhibernateSession.CreateCriteria(typeof(Customer));
        c.Add(Restrictions.Between("_creationDate", spec.Start, spec.End));
        return c.List<Customer>();
    }
    

    This code is bit less expressive than the one you posted but it still good at capturing some domain information in Specification.

    One thing to keep in mind when you work with Specification is that it is a domain concept. It belongs to domain layer and should be free of data access technologies. Technicalities of getting the data are important they just not important in domain layer, they belong to data access layer. Things like Expression<Func<Customer, bool>> are to ‘infrastructural’ for domain code in my opinion. In addition, Linq-based specification tend to require domain objects to expose their data as properties which sometimes breaks their encapsulation. So the whole thing may turn into “Linq over Anemic Model“.

    I highly recommend you to read DDD book. It has a chapter dedicated to Specification pattern and all the tradeoffs that you are dealing with.

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

Sidebar

Related Questions

If I am passing an object to a method, why should I use the
I'm passing a reference of a form to a class. Within this class I
I'm used to passing around string like this in my C++ applications: void foo(const
Passing an object by reference is an easier, faster and safer way to pass
Passing data (simple string) from Controller to View I have in my controller: public
I have this method signature: List<ITMData> Parse(string[] lines) ITMData has 35 properties. How would
Passing an undimensioned array to the VB6's Ubound function will cause an error, so
passing post data using cURL requires that the name of the input. However, I
Passing in an Expression to a Linq query behaves differently depending on syntax used,
warning: passing argument 1 of 'bsearch' makes pointer from integer without a cast 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.