Playing with Fluent Interfaces - The Fluent Validator
About a month ago I've visited redmond for a Prism (WPF Composite Client) design review, during the session Glenn Block had talked about the idea of building Prism using fluent interfaces . In the past I did play with the idea of building fluent interfaces api through it was for my own purposes.
Lately I've been requested to design a Validator for a company whom I'm helping and I've decided that a Validator is really a cool example where fluent interfaces may become useful.
Following is an example of declaring a new validation rule using my fluent validator:
Say for example that you have two entities where you want to govern the interaction between them.
Entity entity1 = new Entity(); Entity entity2 = new Entity();
Why not just declare it like this:
validator.Define.If(entity1).CanBe(ValidationAction.Connected).To(entity2).Return(true);
We can also check for validity of rules using:
validator.Validate.Can(entity2).Be(ValidationAction.Droped).On(entity1)
Indeed as you can see, there is no need for xml comment for these rules you can just fluently read what the code does.
Note: read more about fluent interfaces with the help of Martin Fowlers