Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

19 Comments

  1. Hello,
    How do you project to a DTO for instance or using IMapper, using specification pattern
    Thanks

    1. How do you make projections into a DTO instead fetching the entire columns using the specification pattern?

  2. The Repository Pattern with Specification Pattern , And CQRS + Event Sourcing. Interesting topic.

    By using event sourcing, all transaction are recorded, possible to rollback.

  3. Hi Mukesh,

    super interesting design pattern and great article!

    Is there a good way to combine this with the CQRS pattern?
    To me it feels more like an alternative right now, because the specification classes kind of represent what query classes are in a CQRS setup. Using both in the same project would be confusing because you could use a query or a specification to get the same result.
    Only difference I can guess right now, is that queries would return DTO’s and the specifications sent to a repository would off course return entities. However both contain business logic and I would find myself mostly writing complex specifications. Queries would then boil down to using these specifications and mapping the resulting entities to DTOs.
    I’d appreciate some advice on how to tackle this.

  4. Hello Mukesh,
    Very good article as always ;).
    Have you already tried or tested OData with .NET Core ?
    I know it is possible to combine it with CQRS.
    Could you do an article about it?
    Thanks a lot anyway, your articles inspire me a lot.
    See you later
    Jordan

  5. Where is SpecificationEvaluator defined that is in the GenericRepository implementation?
    Good article and I like the idea, but what is the difference between having a ton of different entry points with each specification into the controller, with all of the associated code and one entry point with a query object like you defined in your API course using the extensions for the repository.

    As someone who is still learning and trying hard to do the “next right thing” this can be confusing, especially when you mention time and again that a service layer between the controller and repository is a good idea, but your examples most ALWAYS leave that part out.

    I know things, they are “achanging”, but good grief, I can’t seem to keep up!

  6. I think the pattern is really intresting. But how can we add more flexible Evalutor?
    for example we want to have ‘orderby’ and then ‘thenbydescending’ and etc.

    Maybe action delegates in spec?
    What you think?

  7. Hi, Mukesh.
    Thank you for the great article.
    Could you please explain the logic in DeveloperWithAddressSpecification?
    Why do you accept int years in the constructor and compare it with EstimatedIncome?

  8. ” Now, go back to the controller, comment out line 38 and let’s use the DeveloperByIncomeSpecification for now. Again, run the application.”

    A small correction here, I guess you’re referring to line 23 of the code snippet, not the 38th line. The writeup was amazing as usual!

  9. Hi Mukesh
    thanks for your help
    but still little confused
    now if you need to search by income you create class –>DeveloperByIncomeSpecification
    if you need to search by address you create class –>DeveloperWithAddressSpecification
    Q1:
    so if i need to search by email so i need to create new classe –>Deleveloper_with_Email??
    Q2:
    if need to search by multiple conditions for example(email /address) so i need to create new class for those conditions
    Q3:
    if in some cases i need to search with mail address & in other cases search by part of mail like all developer who have mail in specific domain so i will create class for each search condition??

      1. Why complicate your life with entityframework and a lot of classes to manage advanced search condition instead of using dinamic sql string build with a simple DataFilter class to specify the query filters?

        1. Just what I was thinking… like this:
          int skip = 50;
          int take = 10;
          string rawSqlQuery = “Select * from tableX inner join… …order by tableX.lastName”
          data = _dbContext.things.FromSqlRaw(rawSqlQuery).Skip(skip).Take(take);

  10. Thank you for such a nice article on specification pattern. It was very helpful for me. However please elaborate/ update this article so that it covers pagination, group by and most importantly thenby scenario.