Search code examples
c#.netrepositoryspecification-pattern

Repository and Specification pattern


I'm currently setting up a new project, and I have run into a few things, where I need a little input.

This is what i'm considering:

  • I would like a generic repository

  • I don't want to return IQueryable from my repository.

  • I would like to encapsulate my queries in specifications.

  • I have implemented the specification pattern

  • It needs to be easily testable

Now this is where I get a little stuck and my question is which way would be the most elegant way of calling the find method with one or more specifications:

(Fluent): bannerRepository.Find().IsAvailableForFrontend().IsSmallMediaBanner()

or express queries as lambdas with my specifications

(Lambda): bannerRepository.Find.Where(banner => banner.IsFrontendCampaignBanner && banner.IsSmallMediaBanner)

or maybe some completely other way? Most important thing is, that the guy implementing the MVC front, should have a good intuitive experience of the repository.

What I am hoping to achieve is to keep som flexibility with regard to being able to combine specifications, and give the experience of "filtering" with the specfications, but without leaking an IQueryable to the controller, but more like an ISpecifiable, that only allows to modify the query with specifications and not with Linq. But am i just back at leaking query logic to the controller this way?


Solution

  • I have seen some Fluent API's that uses Properties for specifications, so they don't add the parenthesis noise to the clients.

    bannerRepository.Find.IsAvailableForFrontend.IsSmallMediaBanner.Exec()
    

    Being Exec() a method for executing the specifications against the repo.

    but even if you don't use the properties, I would go for the fluent API, since it has the minimum noise.