3

I am new to Domain Driven Design but trying to apply it in my current c#.net project. One of the requirements is to allow the user to define business rules to an entity (ie Client Name is required) Different groups of users will define their own sets of rules so I'll be storing them in a database.

I have read a few articles explaining the Specification Pattern but is there any way I could use this to apply rules that are stored in a database? In previous non-DDD projects I have had a IList property on the entity and called a GetBrokenRules(Client client) method to load the rules and check if the client is valid. Would I be better off doing the same sort of thing and not using the Specification Pattern?

Alan Marsh
  • 61
  • 4

3 Answers3

2

The specification pattern is about trying to find entities in a repository by a set of search criteria. Rather than defining a long list of (probably) optional parameters you build up a specification instead that you then translate to whatever query language you are using under the hood.

You business rules are about entity validation so I would say the specification pattern doesn't fit well here. Although it's not impossible.

Your suggested approach of loading the rules and calling a GetBrokenRules method seems a sensible choice.

Community
  • 1
  • 1
Paolo
  • 22,188
  • 6
  • 42
  • 49
1

Dynamic validation rules scream factory pattern. I would probably go the route of having a factory generate validation rules. As pointed out, specifications are meant to build up a predicate.

Depending on the implementation, specifications can be composed dynamically to form a predicate to check whether the property/model/context is valid, albeit this would represent a single rule.

Check out FluentValidation if you haven't, it's pretty flexible and may offer an approach you haven't considered.

Shelakel
  • 1,070
  • 9
  • 16
  • thanks, after Paolo's response, Factory Pattern is what I was thinking, will take a look at FluentValidation. – Alan Marsh Dec 17 '12 at 18:01
0

I think the specification pattern is a good way to go with this. Check out this book if you haven't read it yet.

You should look at creating a composite specification that will be built from the database within a domain service.

Aaron Hawkins
  • 2,611
  • 1
  • 20
  • 24