Developing WinDev applications can be fun and challenging, but where there’s ease of use comes great responsibility.
You need to structure your code is such a way that it will be easily maintainable for yourself and other developers involved in the project.
So MVC comes to the rescue when you want to have a clean and tidy source project. But I here you say, “WinDev doesn’t have an MVC framework”. Well I am going to show you how easy it is to structure your project in such a way that will make it MVC friendly.
What are we trying to achieve here?
To often we just add H commands to our form code just because it is easy to do. But the problem is this soon makes the application messy and you will find more cases of duplicating a process that was in another form.
I am going to show you a simple example of how I structure my projects in an MVC style.
Firstly I created 2 custom folders in Classes, one for Models which will interact with the database, and the other is Controllers which is the conduit between the window form code and the model.
Models
In the screen shot above you can see I have created a UserModel. This class contains all the methods I need to access the database related to a user.
Within the model is a Structure object which represents the User table fields in the database.
So here is an example of the GetUser method in the model which returns an object of type STUser.
Now the windows code does not access the model, it’s the controller that uses the model to return the required data and other information needed by the window.
In the controller the code here calls the model and returns the data as a STUser object. Here you would code other functionality you needed to manipulate the data to show in the View (form/window).
If you are using WinDev 14 then you can use the STUser structure in a very simple way to update the form.
Firstly you would create an object used to hold the data returned from the controller.
In the example above I have created a global object which will now allow me to link the controls on the form to it, as you can see in the screen shot below.
Now I have linked the controls to the object I can get the user data in just a couple lines of code.
If you don’t have WinDev 14 then its a simple task of assigning the object field data to the controls instead, as shown in this screen shot.
So now you can see how using the MVC pattern in WinDev / WebDev can make your code clean and easy to maintain.
Recent Comments