May 1st, 2013
In C# there are various libraries available to do fluent validation and the concept of Extension methods also helps in writing validation/other business logic using fluent interfaces. In this blog post I am going to demo how fluent validation can be implemented in coldfusion.
“In software engineering, a Fluent interface (as first coined by Eric Evans and Martin Fowler) is a way of implementing an object oriented API in a way that aims to provide for more readable code. A Fluent interface is normally implemented by using method chaining to relay the instruction context of a subsequent call (but a Fluent interface entails more than just method chaining). Generally, the context is defined through the return value of a called method self referential, where the new context is equivalent to the last context terminated through the return of a void context.”
Fluent interface is great way to structure your code such that it easy to read, understand and maintain your code. By looking at the code that uses fluent interface implementation you can easily understand the chain of action and what each of those actions are doing.
In this blog post I am demonstrating the use of Fluent interface to do model validation (on same lines as fluentvalidation in c#)
The idea being after some data has been acquired from customer or through other method I want to verify the model is error free prior to updating any internal platforms.
The kind of rules that I want to run against my model in fluent manners are following
Rule(employee.firstName).NotEmpty().ErrorMessage(”First name is required..”);
As you may see the code is self-explanatory, the first rule is to make sure first name is not empty, second and third to make sure numeric format, fourth to make sure name is bound by certain length constrains. I also want to make sure that many of these validations can be chained together
Rule(employee.lastname).IsEmpty().When(employee.age GT 25).ErrorMessage(”Last name not required for grown up”);
The implementation consist of ColdFusion component with fluent methods. To make methods fluent
- The implementation consists of method chaining where each method at the end of their execution returns the context/instance on which they were executing that way the next method on the chain get the scope/context for its execution.
- Since the validation rule can fail at any hop on the execution chain, I have created variable “validationState” that keeps track of when the validation failure happens and use that variable to skip other validations in the same chain.
- This CFC allows you to write multiple rules against the model, therefore rulesResult variable is used for storing the result of each validation rule (chain).
- In this demo there are handful methods available for validation but more can be added.
- Finally also added the concept where somebody can do custom validation using Closure instead of changing the core validation cfc every time.
The source code has been created and tested with CF 10
Entry Filed under: code snippet