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.
May 1st, 2013
In this blog post I will demonstrate the practical use of chain of responsibility design pattern in ColdFusion.
The original Gang of Four definition of Chain of Request (CoR) is:
“Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. There is a potentially variable number of “handler” objects and a stream of requests that must be handled. Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings.”
“In object-oriented design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.”
The idea behind Chain of responsibility pattern is to promote loose coupling by separating out the request from its processing. In this pattern the request can pass through multiple handlers and each handler can
a) Decide if it will handle the request and finish the request processing
b) Decide if it can handle the request, process it and pass the request to another handler in the chain for additional processing
c) Decide it cannot handle the request and pass it on to another handler in the chain for processing
The requester is not aware about the handler chaining and handlers can be added or removed without impacting the requester/client.
In the implementation of this pattern BaseHandler class is created with implementation for setting next handler in the chain and then individual handler can inherit from BaseHandler and provide concrete implement the ProcessRequest
A credit approval application has to send notifications to customer after application has been approved and there are various notification methods available email, sms, voice mail, snail mail. The business rule is that all customer shall receive email and SMS notification but customer with credit score >= 600 will additionally receive voice mail notification and credit score >= 700 will additionally receive snail mail.
April 11th, 2013
One of the functionality I like in asp.net platform is the master page concept, it really helpful to keep the layout clean and gives really good control to developer in terms of placement of content/data on the rendered page. Having worked on aps.net side of it, I figured it’s fairly easy to implement the similar concept in ColdFusion, let me first explain what master page is.
“Master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.” Reference : http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx (can read more about master page..)
How does master page functionality work
a) Create a master page
You need a master page (i.e. the template) with global layout of your web application, there can be multiple templates if you want different look and feel for different section or can have just one global master page for the entire web app, part of the template you will have to also put in placeholder that can be updated by the developer that will be creating functionality for individual pages.
b) Create content page
Content page is the actual page that gets shown to the user; it’s the combination of template + the content relevant to that page. In this page the developer fills in the master page placeholder with page relevant content and there for have full control over what gets rendered to the end user.
Why is master page required in ColdFusion application?
Before I even answer this question, lets walk through the scenario of how page template are created in most of the ColdFusion application, normally the header and footer or other section gets broken into separate file like Header.cfm, footer.cfm etc etc, and the individual content page like index.cfm will include header.cfm on top and then add some html code for the page and finally include footer.cfm something like this.
Some content, static, via db or other means.
So what’s the big deal?
December 19th, 2008
In this blog post I will demonstrate the practical use of strategy design pattern in ColdFusion and discuss other possibilities.
So what is Strategy Design Pattern?
Wikipedia : “In computer programming, the strategy pattern (also known as the policy pattern) is a particular software design pattern, whereby algorithms can be selected at runtime.”
Gang of four : “Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”
So from the various descriptions it’s clear that this pattern can be used in the situation where there is possibility of having difference/multiple calculation and the application has to choose the right one on the fly.
I am going to use a scenario to build the case on why and how this pattern should be used.
Let’s say there is a web application which has multiple developers working on it, or have multiple teams working on independent section of the application, in such sort of application its fundamental to have core set of components that the developer use and there is also a possibility that they may be used across multiple applications. In such sort of scenario its inherent that core component be stable and least amount of changes should be done on the core components (i.e. cfc’s ) to avoid large scale impacts to other modules/developers.
So let’s say one of the core components that we have is the Customer.cfc it has some properties and also methods which get used by the appropriate section of the web applications
June 28th, 2008
The observer design pattern is used in application development to observe the state of an object. The best way to think about it is the publish / subscribe model. In this pattern you have one object that is being observed (the subject), and a group of objects watching the subject called observers or listeners. This pattern is an excellent example of loose coupling, because our classes can interact with very little knowledge of each other.
(read about the pattern @ http://en.wikipedia.org/wiki/Observer_pattern )
This post will demonstrate the usage of this pattern in ColdFusion
In the example I am creating a user list and the list if being watched by a logging observers that prints the message out to the browser anytime the new user gets added
April 14th, 2008
Have you ever come across the idea of building an application or functionality that notifies the user of some event, I can think of various examples?
- A Real estate side notifying the user that property matching their criteria just got listed
- A job side notifying user that a new job matches there keywords
- Application server error alert send to administrator
- Notifying that stock prices went down or up
I can think of hundreds of different situation where notification functionality can be use, today’s most of the notification either happens though email or by the fact that user will come to certain section of site to get the updated information, not trying to downplay the fact that there are tools like nagios etc that can pull data from SNMP traps and send notification but that’s require you to implement SNMP protocol in your application which is not a trivial task.
With the every growing popularity of Instant messaging where millions of users are now actively using IM as means of communication both in office and personal environment bring a very promising platform for application development that can provide the notification mechanism that once thought was tough to implement.
Though instant messaging application like MSN, Yahoo Messenger, AOL Instant Messenger, Google Talk etc have implemented their own protocol and don’t cross communicate but there is light at the end of the tunnel. Google Talk uses XMPP protocol for messaging and looks like yahoo and AOL may follow the suite in future.
XMPP (Extensible Messaging and Presence Protocol) aka Jabber, is a light weight XML based messaging protocol for which various libraries exists and can be used for application development. If all the IM application start supporting XMPP the application development for IM platform will become super simple, but that not the case yet and I think it’s going to be a while before something like this will happen. (more…)
March 28th, 2008
Day was going fine, had my morning coffee attended project related meeting by the time I got back to check the mail I saw a chain of message going back and forth between the coldfusion developers and the backed web services developer regarding the AxisFault subject.
The issue started with the changes that backend/services team implemented in development environment, where they were throwing axis fault in error situation so that the calling application can trap that information and perform appropriate action, It was a little different then how it used to be down before. The main issue for the coldfusion developer was that they were not getting the AxisFault object back, instead in the exception object the fault data was back as a string which they parse and search for the error code and description.
After having the discussion with both the group it was clear that services team wanted to keep the functionality but for frontend/coldfusion team it was additional burden for parsing string and looking for information.
February 7th, 2008
Something that all of us wants to avoid is server slow down and crash in the event of application load and that’s what motivates us to look at code optimization, refactoring, frameworks, database optimization etc.
There are various steps that can be taken for application optimization and to have uninterruptible application availability, but for the moment I am just going to focus on database side. This wont be the first time you are hearing about database/query optimization for having a healthy application, and assuming you have the best optimization done there are still times when unthinkable happens i.e. network connection to database is slow, database transaction log is full, query is running without using a index etc. The point being unforeseen issue can happen with database and it can bring your site to its knees, and your customers have to suffer.
Many of the large scale application uses stored procedure instead of inline SQL queries in CF code and therefore CFStoreproc tag is the ideal choice for running Stored procedure. But one of the biggest disadvantages of using CFStoredproce is the missing timeout attribute, which can lead to slow sites and complete crapping out of server. For whatever reason if the stored procedure executing time goes up then normal expectancy its going to slow down the page processing and eventually result in request queuing for high traffic site and the unavailable server crash.
January 2nd, 2008
The prototype means making a clone. This implies cloning of an object to avoid creation. If the cost of creating a new object is large and creation is resource intensive, we clone the object. This article explains how this pattern can be used in coldfusion
I set out tonight to determine if the Prototype design pattern would be useful in object factories in ColdFusion 8. The Prototype pattern, in a nutshell, is to create a new instance of an object by copying an existing object instantiation. The object to copy is an object instantiated once and then used as a blueprint for future objects of the same type. The thought is that copying an object holds a lesser cost than instantiating a new object.
Read the full Article
December 26th, 2007
Brief introduction of DAO and Gateway pattern for coldfusion audience
Data Access Object and Gateways are the names given to ColdFusion components that access information from a database or other external data source. Within the ColdFusion community DAOs and Gateways have pretty much taken on the meaning as follows
Read the full Article
December 22nd, 2007