Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

standard practice to expose Specification pattern to client code?

I have a N-layered application, where I use Specification pattern. Now, I want to provide some ways to construct specifications to client code. It must be several pre-defined options, like these:

  • Equal
  • GreaterThan
  • Contains

These objects (let me call them Filters) mustn't contain any logic (methods), only data - filter type and parameters. And there must be natural way to transform them into specification at server. Here's example of how it should look from the client side:

var serviceClient = new DataModuleService();
var equalFilter = new ContainsFilter<Book>("Title","Lord of the Rings");
var lordOfTheRingBooks = serviceClient.GetBooks(equalFilter);

There also must be filter types for all standard operations (like Equal, Greater, In, Between, StartsWith for string, etc) and ways to combine them with Boolean operators (and, or, not).

Is there some patterns/standard practices to implement such a thing?


UPD: the task is frozen for now, and I've started to think that there is problem in task's definition itself.

like image 862
vorou Avatar asked Jul 13 '12 18:07

vorou


People also ask

What is the specification design pattern?

In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design.

What is specification in ddd?

Specifications are Business Logic, so they belong to the Domain layer too. In all these examples, an ORM Framework and SQL Server is used inside the Repository. Persistance Models may not leak into Domain Layer.


2 Answers

Just a high level answer - I believe you can try out LINQ expressions which support all logical and conditional operator you are mentioned. Looks through the System.Linq.Expressions Namespace to see available types.

Useful links:

  • LINQ Expression Trees and the Specification Pattern
  • Understanding Specification Pattern – Part 1
  • Understanding Specification Pattern with LINQ – Part2
like image 98
sll Avatar answered Oct 06 '22 01:10

sll


What you are trying to do sounds to me as what Hibernate is doing with its criteria queries. You can combine them however you want to build the filter you are interested in.

This is not C#, but Java and C# are similar languages, so maybe you might borrow some ideas from there.

Additionally, you could go for Hibernate's port on .NET (NHibernate) for the same criteria queries (although I don't exactly know if the API is the same as Java's).

like image 25
Bogdan Avatar answered Oct 06 '22 01:10

Bogdan