Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MVC and Visual Foxpro

Status
Not open for further replies.

skahn612

Programmer
May 24, 2017
2
US
Hello all.
Is there a tutorial on how to implement MVC pattern in VFP?
Can it be done?

Thanks in advance.
 
1. Maybe, I don't know one.
2. Yes, what hinders you?
Model = Database and some data access classes.
View = Winforms
Controller = Business logic

That said the MVC architecture pattern does not really apply well to Winform desktop applications. VFP has a very simple way of data binding and there is no reason to make anything more complex than necessary, but generalize this and you have a 3 tier architecture and of course, VFP can do that. Database layer, business logic, and presentation layer. You will put more logic into a VFP form than into a business logic, but of course, it is rather good practice to not put the code into form control elements and instead let them call into form methods, or create a controller class you instantiate within forms or as goApp and call its methods.

Do you have any concrete MVC implementation in mind? ASP.NET MVC, for example?

Bye, Olaf.
 
thank you for your reply, Olaf.

A coder that I respect keeps insisting I can implement MVC pattern on a VFP app. and insist I try.... I don't know how to tell him it would be difficult and maybe even impossible.
 
Someone told me that I could change my bicycle into a jet plane. Just like your "coder", they are both wrong.
 
He's not wrong.

But it depends on what you expect. MVC is mainly used on websites. It is too specific to be the pattern to use in VFP, but using a 3 tier model of data access, business logic and presentation layer is totally normal and possible in VFP, too. Like any N-tier model it would help by making it easy to switch layers. With a business logic/application layer making use of data access objects you can switch from dbf to views to remote viewss or cursoradapter and thereby to remote databases. And it the frontend should be HTML the business logic layer could also create view (presentational view) objects which "emit" HTML and send that as request response to browsers.

You have several frameworks applying these N-tier principles. One I used is Oakleaf Mere Mortals.

Bye, Olaf.
 

MVC is great, and you can use it on VFP.

Below I post a demo on how you can implement the MVC ( just the view-controller ) part using VFP.

Regarding the 'model' the controller should never access directly the data; you should use
an object to get/set the data - this way you can change your data backend just changing the object base class.

Using MVC you'll have loosely coupled, interchangeable components.

This is what dependency injection's about.


Code:
***********************************
* mvc sample
***********************************
oView = createobject('myform')

with oView

	.addobject('controller1','myController1')
	.addobject('controller2','myController2')

	with .controller1
		.be('sendEmail','button1','click')
	endwith

	with .controller2
		.be('print'      ,'button1','click')
		.be('changeColor','button2','click')
	endwith

	.show()

endwith

read events



***********************************************
define class controller as custom
	osource = ''

	function be(action,src,eventname)

	bindevent(this.parent.&src,m.eventname,this,m.action)


	procedure osource_access
	aevents(asrc,0)
	return asrc(1)


enddefine
***********************************************


***********************************************
define class myController1 as controller

	procedure sendemail
	messagebox(" I'll send an email! / Form Caption: "+this.parent.caption )

enddefine
***********************************************


***********************************************
define class myController2 as controller

	procedure print
	messagebox("I will print.. My Caption is : "+this.osource.caption)

	procedure changecolor
	thisform.backcolor = rgb(26, 118, 209)

enddefine
***********************************************



*********************************************
define class myform as form

	add object button1 as commandbutton ;
		with caption= "I don't know what I do ",;
		left = 10, height = 50 ,	visible = .t., width = 150

	add object button2 as commandbutton ;
		with caption= " me neither!",;
		left = 10, top = 100,height = 50, width = 150, visible = .t.

	caption = 'MVC DEMO '

	procedure destroy
	clear events

enddefine
*********************************************


Marco Plaza
@vfp2nofox
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top