|By Sam Farmer||
|April 13, 2005 12:00 AM EDT||
In any membership organization with many in-house applications, the ability to share members' data among applications is vital. Previously, applications would duplicate functionality or make members leave one application to update their new address. This didn't work well for members, nor was it easy to maintain by developers.
Using a new framework, CFMVC, allowed us to make improvements. Our new membership system will allow our paper submission application to let members change their institution with the same front and back end code the membership application uses.
In developing a new framework we needed it to handle the integration of over twenty different databases and applications, work with multiple developers, have low processing and be straightforward. We achieved it by utilizing the benefits of ColdFusion and the Model-View-Controller (MVC) architecture.
ColdFusionThough it's arguable whether or not ColdFusion is an object-oriented development environment, it does support many object-oriented features and has many constructs that allow developers to reuse code and functionality. ColdFusion Components (CFCs) are ideal for all data interactions and business logic while normal ColdFusion pages, User Defined Functions (UDFs) and Custom Tags are ideal for handling display portions.
All applications, regardless of size, contain the following pieces:
- There will be some way of displaying information to a user - most likely, for ColdFusion developers, via a Web browser.
- Another piece will process any changes the user makes generally by writing to a database.
- A third piece will direct the user to a new item to be displayed.
Model-View-ControllerThe Model-View-Controller (MVC) architecture was designed over 25 years ago by Trygve Reenskaug. Originally designed for Smalltalk programmers, it has been used in many languages from Visual Basic to Java, and is especially common among Object Oriented programmers. Companies embracing it include Apple and IBM.
MVC separates an application into the three pieces or tiers mentioned earlier. What exactly does MVC consist of?
- The model layer handles all core data interaction whether with a database, XML files or any other data store along with other business interactions.
- The view layer displays data to the user.
- Any action the user takes goes through the controller layer, which calls the needed parts of the model layer before calling the next view layer to display.
By combining ColdFusion's best practices and the proven architecture of MVC, a framework emerges that modulizes code and allows for it to be used in multiple applications. (Benoit Hediard also has a similar idea for merging together MVC and CF, see www.benorama.com.)
Tying Together CF and MVCWhere does ColdFusion code live and where does it use components, pages and custom tags?
Most applications use at least one database. The model tier retrieves data from the database, stores this data, and provides functionality for interacting with the database. If your application uses text files or some other data storage mechanism, then your model tier will interact with that entity. My MVC implementation involved creating an entity CFC for each table in the database (for example, PeopleEntity.cfc) that handles select (get), update and insert statements (set) and delete if required. A utility CFC is used when returning multiple records, joining other tables and processing functions that require more than one entity, like adding a new member - which requires inserts into a members and an addresses table. All code in the model layer must be written so it can be reused in other applications and should not use session, application or request variables; rather, values should be passed in via attributes. Other CFCs, to be shared, are also added in the model layer for e-mail, SOAP calls, LDAP, and so on.
Each CFC should be a object and contain only functions (methods) that relate to it. Functions should either perform one action only or be a wrapper function and call other functions.
The view tier is where all display files reside. Custom tags are used extensively for presentation code and can be called from any application if needed. Like the model layer, they must not include session, application or request variables. For some presentation purposes UDFs may also be needed.
We did two things that are not part of MVC but were needed for ColdFusion implementation. First, all forms use the post method and custom tags that contain a form accept two attributes: formAction and formMethod, which are placed in the form to pass data to the correct controller. This allows for calls from anywhere and the capability to post to anywhere. Second, all applications have at least one applicationSkin file (see membershipSkin.cfm) in the view layer to allow for an application appearance to be set. Multiple skins can then be used if, say, an admin version is intended to look different to a normal version.
The controller tier is on the web root and has no pretense of being reused. Indeed they should only be used by the application. There are two types of controller files: view-controller and model-controller. View-controller files call code in the view tier and sometimes add customization themselves. Model-controller, as the name suggests, handle all interaction with the model layer but it's important to remember that code here is not intended for reuse and extra features for this application can be added for example an e-mail or calling a web service.
As part of the ColdFusion implementation, view-controller files are standard cfm files that call custom tags. Model-controller files are CFCs, are accessed remotely but as are below an Application.cfm (or Application.cfc) file fit into whatever security system an application deploys. Once a model-controller file has finished processing, it uses cflocation to call the next view-controller file to ensure that data is processed only once if a user reloads.
If you have trouble separating your files into their appropriate areas of the MVC framework, the code in that file is probably doing too much. Try breaking it down into smaller parts.
Folder StructureWhere does all this code sit? As controller files are used only by the application they all belong in the application web root, i.e. webroot/membership/. View-controller files, one for each area of the application, sit in this folder (see Figure 1). This allows for freedom in naming and short URL's. Features that apply to that area only, such as navigation, can also be added easily. Model-controller files are all placed in a controller folder i.e. webroot/membership/controller/.
The rest of our code, in the model and view tiers, is intended to be shared. Below the webroot we set up a folder called cfshare and within that a CFC, tag, and UDF folder (see Figure 2). A merging of ColdFusion, MVC and our desire to share code amongst applications led to such a folder set up. All model files are CFCs leading to a natural home for them, so beneath the CFC folder an additional folder is created for each application to store Entity, Utility and other components.
A similar set-up occurs for view files, all of which are custom tags. Likewise within the tag folder is an additional folder for each application. For the times when UDFs are used they follow the exact same structure under the UDF folder.
To make accessing easier in our code, a ColdFusion mapping is set up to the cfshare folder. Once achieved to access the peopleEntity in the model tier simply call the component:
<cfinvoke component="cfshare.cfc.membership.peopleEntity" method="init"></cfinvoke>
And to access the applicationSkin (using CFMX 6.1):
<cfimport prefix="/cfshare/tag/membership/ " taglib="view"> <view:membershipSkin> </view:membershipSkin>
Or for 6 and backwards:
CodeLet's look at two code examples, which can be downloaded from www.sys-con.com/coldfusion/sourcec.cfm; first the display page for a member to join and second the processing of the collected data.
Code Example One: index.cfm
The first example uses a view-controller called index.cfm.
We start by importing a taglib and giving the prefix of view to enable the calling of custom tags (such a method shows future developers exactly where the custom tag is). Then we param the URL display to a default value of "join". Next we open up the membershipSkin call inside of which is a cfswitch statement which will call the desired display. The default value of join is picked and the "people" custom tag is called with some variables passed in. The cfswitch statement and membershipSkin custom tags are then closed.
Let's step back and look at the people custom tag. First it performs a check to see if it has already been called and if so exits, to prevent duplicate output. Then it calls the peopleEntity cfc and outputs a simple form to allow for the collecting or editing of data. The three attributes it accepts are important: nextAction and nextMethod work in tandem - nextAction contains the path to the processing controller ("controller/membershipController.cfc" in this case) and nextMethod is the name of the function that will be called in the controller file. Passing in a value of -1 for peopleID allows for blank values to be created, but if a value of a real member had been passed in then those values would have been displayed.
Code Example Two: process-controller
How does this get processed? This is an example of a process-controller with the membershipController.cfc being called.
The nextMethod attribute in the people custom tag was set to a hidden field called method which when passed to a cfc remotely will trigger that a function of the provided name. For this example it is join. The access attribute in join function is set to remote to allow for it to be called over the web. The function creates an object to the peopleEntity in the model layer and then calls the set function with the passed in values. The peopleEntity then either runs an update if a valid peopleID is passed or an insert. Just like that, we have a new member! The join function then uses cflocation to relocate the user to the next page.
SummaryThe CFMVC framework discussed above is based on a proven, successful architecture in MVC and has a low processing overhead for ColdFusion. The effect of splitting an application into tiers - model, view and controller - allows us to write applications that can share both data and functionality easily. But the benefits will also extend to completely standalone applications.
|David Hardwick 05/12/05 12:00:44 PM EDT|
Have you investigated the other MVC frameworks before coming up with your own? Mach-II and Tartan for example? I'm using Mach-II on a project right now, and it is very solid. I've worked with Java-based MVC frameworks prior to this project, and I am very impressed with the thought that went into this architecture.
While it is admirable that you put together your own MVC framework, I would rather the CF community do a better job than say the PHP community in putting our efforts behind one or two horses instead of fragmenting our time across multiple 're-inventing the wheel' efforts.
|Sridhar Loka 05/12/05 11:53:07 AM EDT|
I have implemented MVC architecture for ColdFusion application using different approach.
|Jim Davis 05/12/05 07:20:35 AM EDT|
I like this approach and its clear division between layers. Will definitely keep it in mind when picking a methodology for a new project.
|Brian Kotek 05/11/05 07:04:17 PM EDT|
While it is always great to spread the word about best practices and patterns like MVC, I have to point out that using MVC with ColdFusion has been around for years. There are several widely-used frameworks with established communities that support MVC development. Fusebox 4, Mach-II, and Model-Glue are among the most popular. These have the added benefit of a built-in system to handle content elements and layouts without having to resort to custom tags. I would urge anyone interested in this topic to investigate these popular frameworks.
SYS-CON Events announced today that Windstream, a leading provider of advanced network and cloud communications, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Windstream (Nasdaq: WIN), a FORTUNE 500 and S&P 500 company, is a leading provider of advanced network communications, including cloud computing and managed services, to businesses nationwide. The company also offers broadband, phone and digital TV services to consumers primarily in rural areas.
Apr. 18, 2015 10:45 PM EDT Reads: 4,743
SYS-CON Events announced today that Dyn, the worldwide leader in Internet Performance, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Dyn is a cloud-based Internet Performance company. Dyn helps companies monitor, control, and optimize online infrastructure for an exceptional end-user experience. Through a world-class network and unrivaled, objective intelligence into Internet conditions, Dyn ensures traffic gets delivered faster, safer, and more reliably than ever.
Apr. 18, 2015 09:45 PM EDT Reads: 2,256
SYS-CON Events announced today that Open Data Centers (ODC), a carrier-neutral colocation provider, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. Open Data Centers is a carrier-neutral data center operator in New Jersey and New York City offering alternative connectivity options for carriers, service providers and enterprise customers.
Apr. 18, 2015 09:30 PM EDT Reads: 2,450
SYS-CON Events announced today that On the Avenue Marketing Group, a sales and marketing firm that utilizes events to market and sell products to consumers, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. On the Avenue Marketing Group (OTA) is a sales and marketing firm that utilizes events to market and sell products to consumers. On behalf of our clients, we attend thousands of fairs, festivals, expos, concerts, conferences, and sporting events annually, helping them reach millions of individuals ...
Apr. 18, 2015 09:15 PM EDT Reads: 3,117
SYS-CON Events announced today that ActiveState, the leading independent Cloud Foundry and Docker-based PaaS provider, has been named “Silver Sponsor” of SYS-CON's DevOps Summit New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. ActiveState believes that enterprises gain a competitive advantage when they are able to quickly create, deploy and efficiently manage software solutions that immediately create business value, but they face many challenges that prevent them from doing so. The Company is uniquely positioned to help address these challenges thro...
Apr. 18, 2015 09:00 PM EDT Reads: 2,688
SYS-CON Events announced today that Vitria Technology, Inc. will exhibit at SYS-CON’s @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Vitria will showcase the company’s new IoT Analytics Platform through live demonstrations at booth #330. Vitria’s IoT Analytics Platform, fully integrated and powered by an operational intelligence engine, enables customers to rapidly build and operationalize advanced analytics to deliver timely business outcomes for use cases across the industrial, enterprise, and consumer segments.
Apr. 18, 2015 08:45 PM EDT Reads: 2,412
SYS-CON Events announced today that Alert Logic, the leading provider of Security-as-a-Service solutions for the cloud, has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo® and DevOps Summit 2015 New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY, and the 17th International Cloud Expo® and DevOps Summit 2015 Silicon Valley, which will take place November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA.
Apr. 18, 2015 08:30 PM EDT Reads: 1,732
SYS-CON Events announced today that kintone has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY, and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. kintone promotes cloud-based workgroup productivity, transparency and profitability with a seamless collaboration space, build your own business application (BYOA) platform, and workflow automation system.
Apr. 18, 2015 08:00 PM EDT Reads: 1,485
SYS-CON Events announced today that Akana, formerly SOA Software, has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. Akana’s comprehensive suite of API Management, API Security, Integrated SOA Governance, and Cloud Integration solutions helps businesses accelerate digital transformation by securely extending their reach across multiple channels – mobile, cloud and Internet of Things. Akana enables enterprises to share data as APIs, connect and integrate applications, drive part...
Apr. 18, 2015 07:45 PM EDT Reads: 1,781
SYS-CON Events announced today that CommVault has been named “Bronze Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY, and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. A singular vision – a belief in a better way to address current and future data management needs – guides CommVault in the development of Singular Information Management® solutions for high-performance data protection, universal availability and sim...
Apr. 18, 2015 07:30 PM EDT Reads: 1,721
SYS-CON Events announced today that SafeLogic has been named “Bag Sponsor” of SYS-CON's 16th International Cloud Expo® New York, which will take place June 9-11, 2015, at the Javits Center in New York City, NY. SafeLogic provides security products for applications in mobile and server/appliance environments. SafeLogic’s flagship product CryptoComply is a FIPS 140-2 validated cryptographic engine designed to secure data on servers, workstations, appliances, mobile devices, and in the Cloud.
Apr. 18, 2015 07:15 PM EDT Reads: 1,833
SYS-CON Events announced today that StorPool Storage will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. StorPool is distributed storage software that allows service providers, enterprises and other cloud builders to run data storage on standard x86 servers, instead of using expensive and inefficient storage arrays (SAN).
Apr. 18, 2015 06:00 PM EDT Reads: 1,959
SYS-CON Events announced today that Site24x7, the cloud infrastructure monitoring service, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Site24x7 is a cloud infrastructure monitoring service that helps monitor the uptime and performance of websites, online applications, servers, mobile websites and custom APIs. The monitoring is done from 50+ locations across the world and from various wireless carriers, thus providing a global perspective of the end-user experience. Site24x7 supports monitoring H...
Apr. 18, 2015 05:45 PM EDT Reads: 1,808
SYS-CON Events announced today that Intelligent Systems Services will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Established in 1994, Intelligent Systems Services Inc. is located near Washington, DC, with representatives and partners nationwide. ISS’s well-established track record is based on the continuous pursuit of excellence in designing, implementing and supporting nationwide clients’ mission-critical systems. ISS has completed many successful projects in Healthcare, Commercial, Manufacturing, ...
Apr. 18, 2015 05:15 PM EDT Reads: 2,363
SYS-CON Events announced today that B2Cloud, a provider of enterprise resource planning software, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. B2cloud develops the software you need. They have the ideal tools to help you work with your clients. B2Cloud’s main solutions include AGIS – ERP, CLOHC, AGIS – Invoice, and IZUM
Apr. 18, 2015 05:15 PM EDT Reads: 3,111
SYS-CON Events announced today that Vicom Computer Services, Inc., a provider of technology and service solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. They are located at booth #427. Vicom Computer Services, Inc. is a progressive leader in the technology industry for over 30 years. Headquartered in the NY Metropolitan area. Vicom provides products and services based on today’s requirements around Unified Networks, Cloud Computing strategies, Virtualization around Software defined Data Ce...
Apr. 18, 2015 02:00 PM EDT Reads: 1,379
SYS-CON Events announced today that Optimal Design, an Internet of Things solution provider, will exhibit at SYS-CON's Internet of @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Optimal Design is an award winning product development firm offering industrial design and engineering services to the consumer, medical, and defense markets.
Apr. 18, 2015 01:45 PM EDT Reads: 1,592
SYS-CON Events announced today that Tufin, the market-leading provider of Security Policy Orchestration Solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. As the market leader of Security Policy Orchestration, Tufin automates and accelerates network configuration changes while maintaining security and compliance. Tufin's award-winning Orchestration Suite™ gives IT organizations the power and agility to enforce security policy across complex, multi-vendor enterprise networks. With more than 1...
Apr. 18, 2015 01:45 PM EDT Reads: 3,274
SYS-CON Events announced today that MangoApps will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY., and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. MangoApps provides private all-in-one social intranets allowing workers to securely collaborate from anywhere in the world and from any device. Social, mobile, and easy to use. MangoApps has been named a "Market Leader" by Ovum Research and a "Cool Vendor" by Gartner...
Apr. 18, 2015 01:15 PM EDT Reads: 3,544
SYS-CON Events announced today that Cloudian, Inc., the leading provider of hybrid cloud storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Cloudian, Inc., is a Foster City, California - based software company specializing in cloud storage software. The main product is Cloudian, an Amazon S3-compliant cloud object storage platform, the bedrock of cloud computing systems, that enables cloud service providers and enterprises to build reliable, affordable and scalable cloud storage solu...
Apr. 18, 2015 01:00 PM EDT Reads: 2,357