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:
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.
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.
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.
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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With