3

I have been looking into the specification pattern that is briefly described in martin fowler's patterns of enterprise architecture under the repository pattern section, as well as several examples on the web. However, almost all of the examples/descriptions are created by utilizing an ORM and methods such as IsSatisfiedBy which are executed by the specification objects(and probably converted into SQL by the ORM).

I can see how you might adapt it to work with SQL, but due to a general lack of SQL examples, I was wondering if people are using this pattern with a SQL data access layer and the repository pattern, and their experience/approach with it if they are, or any alternatives that may be better suited to the task if there are any.

Christian Davén
  • 16,713
  • 12
  • 64
  • 77
gmn
  • 4,199
  • 4
  • 24
  • 46
  • I have that same need. Research specification pattern with Repository on Google and 95% of results are C# related. I believe it's because C# makes it relatively easy for ORMs to convert LINQ to SQL. But what if I'm writing my app in Python, or TypeScript? I think in that case you would have to write some kind of Specification to SQL mapper by parsing the code's syntax tree. Not a trivial thing. I'm not sure though and I couldn't find anything about that while researching the topic. – snowfrogdev Mar 20 '21 at 11:30

2 Answers2

1

With Clean Architecture, you have these restrictions:

  • Specifications are written in the application or domain layer
  • Database entities and SQL are only allowed in the infrastructure layer

Therefore, you need to write your specifications

  • without using SQL, and
  • based on the domain entities.

If you are not using an ORM, the missing piece to making Specification Pattern work is the mapping from specifications and domain entities to SQL and database tables.

Entity Framework does this out of the box, based on either conventions, attributes or the Fluent API.

You can either

  • look into other libraries that provide this feature set, or
  • implement some kind of abstraction yourself, or
  • break Clean Architecture and use SQL in the specifications.

Unfortunately, I haven't explored these options further yet.

Christian Davén
  • 16,713
  • 12
  • 64
  • 77
0

I believe that LINQ effectively implements the need to the specification pattern (I believe that this also ties into you request to work with SQL).

I suspect there are also APIs to spit out SQL without LINQ parse trees.

So, Entity Framework or LINQ to SQL would be worth looking into.

I hope this answers your question.

RBZ
  • 2,034
  • 17
  • 34
  • 2
    Thanks for the answer, I'm really looking for how its been used(if at all) without an ORM (i.e. Entity framework or linq to sql). – gmn Feb 23 '11 at 22:59