Sign in to follow this  
Followers 0
Psychoman

Model View Controller - Example [solved]

4 posts in this topic

#1 ·  Posted (edited)

Context

It's been a while since I have discovered AutoIt and I believe it is absolutely awesome.

After developing some little scripts I have come across the need for developing a bigger application.

I quickly understood I would have to apply the Model View Controller design pattern.

I have quite some experience with Object Oriented programming languages however I lack knowledge of functional scripting languages.

As the forums didn't give me the answers I searched for I developed a little application which tries to apply the MVC design pattern.

My request

Could following example of the application of the MVC in AutoIt be valdated?

Every remark, comment, input would be greatly appreciated:

- the application of the MVC pattern in AutoIt

- the structure of the code

- the coding conventions I used

- general usage of AutoIt

- ...

Functionality of the program

The application stores a configuration of a database in it's state:

- database server

- database name

- database username

- database password

When the program is launched, an INI file is read with contains the configuration.

The data of the INI file is stored in the state of the program.

The user can then consult and adapt the configuration.

Once the application is stored the program writes back it's state to the INI file.

Graphical User Interface

The main window

The use can consult the stored configuration of modify the configuration.

Posted Image

The consultation window

All the user can do is close the window.

Posted Image

The modification window

Here the user can modify the configuration

Once modified, the user can confirm the modification or cancel it.

Posted Image

Most important au3 files

/Application.au3_________________________The application is launched with this file

/model/Configuration.au3_________________Manipulation of the state of the application

/model/ConfigurationFile.au3_____________Writing the state to an INI file

/view/Main.au3___________________________The main window

/view/ConsultConfiguration.au3___________The window to consult the configuration

/view/ModifyConfiguration.au3____________The window to modify the configuration

/controller/Main.au3_____________________The controller of the main window

/controller/ConsultConfiguration.au3_____The controller of the window to consult the configuration

/controller/ModifyConfiguration.au3______The controller of the window to modify the configuration

Comments on the structure of the code

/model/Configuration.au3

The state of the application is stored in following variables

Global $stateDatabaseServer
Global $stateDatabaseName
Global $stateDatabaseUsername
Global $stateDatabasePassword

The state should only be accessed via following operations

_GetDatabaseServer()
_SetDatabaseServer($parDatabaseServer)
_GetDatabaseName()
_SetDatabaseName($parDatabaseName)
_GetDatabaseUsername()
_SetDatabaseUsername($parDatabaseUsername)
_GetDatabasePassword()
_SetDatabasePassword($parDatabasePassword)

As the model contains all data, it is the only one who can perform validations on it

_IsConfigurationValid($parDatabaseServer, $parDatabaseName, $parDatabaseUsername, $parDatabasePassword)

Views only contain presentation logic.

The initialization of a view can be parametrized as it can contain dynamic data.

Func _View_ConsultConfiguration_Create($parDatabaseServer, $parDatabaseName, $parDatabaseUsername, $parDatabasePassword)

Although the views catch the events of the GUI, it's the associated controller who handles the functionality behind the event.

Catching the events of the main window

Func _View_WindowWelcome_HandleEvents()
    While $guiMainLoop
        Local $varMessage = GUIGetMsg()
        
        Select
            Case $varMessage = $GUI_EVENT_CLOSE
                _Controller_Main_WindowClosed()
            Case $varMessage = $guiMainButtonConsult
                _Controller_Main_ButtonConsultPressed()
            Case $varMessage = $guiMainButtonModify
                _Controller_Main_ButtonModifyPressed()
        EndSelect
    WEnd
EndFunc

Handling the events by the controller.

The controller can access the public functions of the model layer.

Func _Controller_Main_WindowClosed()
    _View_Main_Destroy()
    _WriteConfiguration()
EndFunc


Func _Controller_Main_ButtonConsultPressed()
    _View_Main_Destroy()
    _Controller_ConsultConfiguration_Initialize()
EndFunc


Func _Controller_Main_ButtonModifyPressed()
    _View_Main_Destroy()
    _Controller_ModifyConfiguration_Initialize()
EndFunc

The sourcecode

The sources of this program are included as an attachment of this post.

I will give my best try to answer all your questions and take into account your remarks.

Thanks to all of you who will help me out with this.

I really appreciate your help.

example.zip

Edited by Psychoman
1 person likes this

Share this post


Link to post
Share on other sites



it seems well done to me !!

this is very clear and convenient !

Share this post


Link to post
Share on other sites

1)

I see you using the Local keyword instead of the Global keyword at Root level to define some variables.

Just making sure you know. These variables will just be as Global as if you would have used the Global keyword on them.

(Seem you used Local to indicate there local purpose to the WVC section in which they belong. But ... never forget that they are in reality Global's. To the whole code, as includes don't have there own scope.)


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Thanks for the constructive replies.

I have adapted the sourcecode to reflect you remark MvGulik.

None of the global variables should be declared as local.

example.zip

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0