Quote of the Day

Tuesday, April 17, 2007

Exploring VFP on Rails

My friend Brian Marquis recently showed some samples of his early concept design for VFP on Rails at the Chicago Foxpro User Group. Although the samples themselves were something you could do pretty easily with pure ASP or other web scripting tools, the underlying framework that he is creating is very intriguing.

As the name implies, he is attempting to follow the same tenets and design patterns of the very popular Ruby on Rails framework. The main RoR description states "Rails is a full-stack framework for developing database-backed web applications according to the Model-View-Control pattern".

I am not a web developer (not even close!) and I have not used RoR, but I am a fan of well designed frameworks that make my job easier, which in turns makes my clients happier, etc. That's why most of my VFP applications have been developed with the Visual FoxExpress framework, which happens to be a very well designed framework with full nTier architecture and implements several design patterns in a very elegant way.

So, just as an experiment, I decided to put VFP on Rails to the test and see how quickly I could create a search demo (based on Brian's simple customer search demo) that does not go directly at a VFP table, but rather requests data through an existing VFE business object through COM. I mean, why shouldn't I be able to have access to the business objects I've already created from the web? It turned out to be incredibly easy.

First, I created a new folder under Views to keep my VML files separate from the other samples. (VML is the extension Brian has assigned to the "view" or presentation layer scripts, not to be confused with VFP views.) Then, I compiled one of my clients VFE apps as a DLL and registered it. (This app happens to have it's data in SQL Server)

I then copied Brian's VML files and edited them to be specific to my search:

Search.VML - Changed "say" in this line of code to "agents"

Ajax.Request("<<fullurl(oDispatchContext,'/agents/list')>>", {


List.VML - just changed the cursor (alias) name and only display active records.

<table>
<%
SELECT CurAgents
SCAN
If Not CurAgents->Inactive
%>
<tr><td>
<<CurAgents->Ag_Name>>
</td></tr>
<%
Endif
ENDSCAN
%>
</table>
<%
IF oDispatchContext.hasErrors()
FOR EACH error in oDispatchContext.errors%><<error>><br />
<%
ENDFOR
ENDIF
%>


Agents.Prg - This is in the Controllers folder.


DEFINE CLASS agents AS Custom
greeting = ""
HIDDEN oAgent
oAgent = .NULL.
HIDDEN oApp
oApp = .NULL.
lDestroyInProgress = .F.

PROCEDURE Destroy
This.lDestroyInProgress = .T.
IF VARTYPE(This.oAgent) = "O"
This.oAgent.Release()
this.oAgent = .NULL.
ENDIF
IF VARTYPE(This.oApp) = "O"
This.oApp.Cleanup()
This.oApp = .NULL.
ENDIF
ENDPROC

HIDDEN PROCEDURE oAgent_Access
IF NOT This.lDestroyInProgress AND VARTYPE(This.oAgent) <> "O"
This.oApp = CREATEOBJECT('ptrak.ptrakapplicationobject')
This.oAgent = This.oApp.oBizObjs.Add("AgentsViewBizobj")
ENDIF
RETURN This.oAgent
ENDPROC

PROCEDURE hello
LPARAMETERS user
this.greeting = "Hello " + user
ENDPROC

PROCEDURE search
ENDPROC

PROCEDURE list

LOCAL loAgent, ;
lcXML, ;
lcCurrentDirectory

loAgent = This.oAgent

IF NOT EMPTY(this.criteria)
* Set operator to contains (defaults to "Begins With")
loAgent.oCursor.Parameters.Item(2).cOperator = "Contains"
loAgent.SetParameter("VP_AG_NAME",this.criteria)
ENDIF
loAgent.Requery()
lcXML = loAgent.GetXML()
XMLTOCURSOR(lcXML,"curAgents")
loAgent = .NULL.
ENDPROC
ENDDEFINE


I then just navigated to http://localhost/WebRoot/Default.aspx/agents/search and it works!

Naturally, it's a bit more code to go at a COM server vs. native VFP data, but the point is, it was do-able using VFP on Rails. I'm still a LONG way from being able to write a complete usable web application with full CRUD features, but at least I know I don't have to worry about the business and data tier since I can use my favorite tool for this: VFP. When it's time to get serious I can either learn some presentation layer tools or farm that part out to experienced web developers while I focus on the business logic.

Brian is focusing on trying to keep the implementation and configuration as simple as possible. Currently, it's as simple as creating a new web share in IIS and registering a single COM server. No other configuration is required. Controller classes can be deployed without stopping the web server and I think there will be a new "autocompile" feature added soon so that you don't have to remember to compile PRG changes during development.

I am aware of at least 4 other VFP based web tools. I'm not sure how this compares with those as I've not had the opportunity to try them. Of course, this is in the very early stages and Brian is hoping to generate some interest from other developers before submitting to VFPX on CodePlex.

No comments: