From: Mikeon on
Hello!

I find myself using specification pattern a lot lately, but in a
foreseaable future I will run in to following problem:

I'm using repositories as a way to abstract data access logic from my
model/domain code. There are simple methods defined on a repository
such as: GetAll, Add etc. There is also one additional method:
GetBySpecification which looks like this

MyObject[] GetMyObjectsBySpecification(ISpecification<T> spec);

>From the model point of view, specification pattern is as clean and
nice as can be.
The question is: what to do with the specification when it comes to
data access layer code i.e. the data mapper.
How do you translate specification into a data access layer specific
code i.e. into SQL statemenents?

--
Michal

From: Jens Winter on
Mikeon wrote:
> MyObject[] GetMyObjectsBySpecification(ISpecification<T> spec);
>
>>From the model point of view, specification pattern is as clean and
> nice as can be.
> The question is: what to do with the specification when it comes to
> data access layer code i.e. the data mapper.
> How do you translate specification into a data access layer specific
> code i.e. into SQL statemenents?

Hello Michal,

You should check out Eric Evans' book "Domain Driven Design":
http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215

Evans describes a way how to do selection with the specification pattern.
Unfortunately, I don't have the book here at the moment. So I can't give
you more information about the suggestions from the book. If I remember
correctly, Evans uses IoC to decouple the specifications and the SQL
generation.

Ciao,
Jens

--
http://www.jenswinter.com
From: Mikeon on
On Feb 3, 12:53 pm, Jens Winter <jens.win...(a)mdcc-fun.de> wrote:

> You should check out Eric Evans' book "Domain Driven Design":http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Softwa...
>
> Evans describes a way how to do selection with the specification pattern.
> Unfortunately, I don't have the book here at the moment. So I can't give
> you more information about the suggestions from the book. If I remember
> correctly, Evans uses IoC to decouple the specifications and the SQL
> generation.

I've read the book a while back, but I don't recall anything about
translating Specification to i.e. SQL.
Could you post a simple example of how to use IoC with specification
to generate SQL?

--
Michal

From: Frans Bouma on
Mikeon wrote:

> On Feb 3, 12:53 pm, Jens Winter <jens.win...(a)mdcc-fun.de> wrote:
>
> > You should check out Eric Evans' book "Domain Driven
> > Design":http://www.amazon.com/Domain-Driven-Design-Tackling-Complexi
> > ty-Softwa...
> >
> > Evans describes a way how to do selection with the specification
> > pattern. Unfortunately, I don't have the book here at the moment.
> > So I can't give you more information about the suggestions from the
> > book. If I remember correctly, Evans uses IoC to decouple the
> > specifications and the SQL generation.
>
> I've read the book a while back, but I don't recall anything about
> translating Specification to i.e. SQL.
> Could you post a simple example of how to use IoC with specification
> to generate SQL?

See it like this: you have in your repository an object which
generates SQL based on specifications you pass into the repository. How
that's done isn't important to the repository. WHICH object is used by
the repository is also not defined inside the repository, but set
outside the repository, for example by passing in the sql generator
object on the constructor of the repository.

That's basicly what IoC means: you specify somewhere else which
objects a given object depends on, instead of specifying that inside
the depending class.

To do that without hassle, people often use IoC containers, to
implement IoC, but you don't necessarily need an IoC container to
implement IoC.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
From: Mikeon on
> See it like this: you have in your repository an object which
> generates SQL based on specifications you pass into the repository. How
> that's done isn't important to the repository. WHICH object is used by
> the repository is also not defined inside the repository, but set
> outside the repository, for example by passing in the sql generator
> object on the constructor of the repository.


Ok, I know what IoC means. What I don't know is how to convert
Specification to Query Objects automatically.

--
Michal