Magix is a Web Application Framework which I’ve been working on for a very long time. One of its core features, is that it relies upon a Design Pattern called “Active Events”, which replaces the Polymorphism and Encapsulation mechanisms of OOP with something that creates far more SOLID code. You can find Magix here. To download it, you must have Mono and Mono Develop installed, which you can find on the Mono Website, or in your local Linux Package Manager. In addition you’ll need to have db4o installed, which you can find in your Package Manager on Linux. If you’ve got everything up running with Mono and db4o, you should be able to open the “Magix2.sln” file, and click “F5″, and your installation of Magix should start locally on your development computer. To port Magix to Windows or Visual Studio, is of course a breeze, but outside of the scope of this tutorial. If you compile your app on Linux, you’ve got binary compatibility with .Net and Windows Servers anyway.
Below is the first screen you see when you start Magix …
The top textbox is “input nodes”, the middle one is your Active Event code, while the bottom textbox is your “output area”. Currently the system is set into “Hello World” mode. If you click the “Run” button, you should see something like the below …
Congratulations, you have now invoked your first “Active Event”. When you clicked the “Run” button, the active event “magix.viewport.show-message” was raised, with one parameter called “message”, which contains the message being shown. If you’d like to see where this occurs in C# code, then open up the Website.ascx.cs file in the Magix.vewports project. In there you will find something like this …
[ActiveEvent(Name = "magix.viewport.show-message")]
protected void magix_viewport_show_message (object sender, ActiveEventArgs e)
{
/* ... code ... */
}
Active Events are “hooks” into your code, which you can dynamically invoke through scripting and similar types of mechanisms. When you invoke the “magix.viewport.show-message” event, then your input nodes are being serialized into a C# Node object, which again is being interpreted as input parameters to your active events, for then to execute the code associated with your active events.
If I now wanted to exchange the “show-message” event, then I could easily either create an override, or an entirely new module, tossing out the old one, for my new logic. And as long as I’m handling the same nodes as input parameters, then my new code would simply just work!
This facilitates for a much more solid way of implementing polymorphism and encapsulation, which ends up becoming far more superior code, easily maintainable, flexible and robust in the end. This makes it possible for you to be TRULY Agile, since you now can change any functionality within your application, without needing to think about the consequences of your changes. Magix facilitates for “change” …
Your First Program
If you scroll further down on the page, you will see a list of active events registered within the system. We’ll start with “magix.execute”. Make sure the small textbox in the middle says “magix.execute”, and copy and paste in the code below:
Data
List
Item1
Description=>Hi there Thomas Hansen
Item2
Description=>Howdy John Doe!
Item3
Description=>Mohammed Ali I presume ...?
Item4
Description=>21st Century Schizoid Man!!!
Buffer
for-each=>[Data][List]
set=>[.ip][..][raise][message].Value
value=>[.][Description].Value
raise=>magix.viewport.show-message
message
SPACING COUNTS. Remember, in Magix, in the Active Event Executor, spaces carries semantic value, and are basically like braces in C#. A two space indentation, creates a new “scope” for you. When you click run, after typing in the code, your screen should look something like this …
The code above, basically just iterates over all [Data][List] nodes, which are two, “Item1″ and “Item2″. Then it raises “magix.viewport.show-message” with the “Description” node as the message to show to the end user. “magix.execute” is an active event which serves as a small “programming language”, built upon the lessons learned from Lisp, which you can create custom logic with. All keywords for it are within the “magix.execute” namespace, and you can also extend the language with your own keywords, either in code, or in the front end. So when we type “for-each” in the code above, then this will map to the Active Event called “magix.execute.for-each”, and this event will be raised. Meaning, any keywords you miss in Magix, you can easily create yourself in for instance C# by creating an event that starts with “magix.execute” somehow, which will become your keyword. However, all keywords needs to start with a lowercase alpha character to be recognized as such. This is why “Data” is ignored as code. This way you can combine data and code together, and even transmit them over the network as input to other methods, and in such a way implement Map Reduce functionality.
Let’s create a Function
You can also create your own functions, which will serve as Active Events, with their own code. Copy and paste the code below into the top textbox …
function
event=>bar
code
Value1=>thomas
if=>[Value1].Value==thomas
magix.viewport.show-message
message=>Still there Thomas?
Make sure your view looks like this now …
Then click “Run” …
If you scroll down on the page now, you will see that “foo” is a new active event, which the screenshot below illustrates …
You’ve now created a new active event, which you can invoke. If you empty your code window, and type in “bar” into the small textbox in the middle, and click run, you’ll see something like this …
This way you can create your own functions, which will become serialized into the db4o storage file, and can later be used. You can also override “system Active Events” this way, or active events created in code. And by combining this with the “fork” keyword, and the “remote” keyword, which creates a new thread, and invokes an active event remotely, you can create complex cloud implementations, where the sum of your servers basically behaves as “one application”, but you’ve really partitioned your server park into hundreds of thousands of different servers, with different roles.
To exchange your application main state, you’ll need to override the “magix.viewport.page-load” active event, which is the one being raised upon the initial loading of a page. Currently this is found in “Magix.SampleController”, where you’ll find an active event handler which is handling “magix.viewport.page-load” – this is the active event you need to override to change the initial state of your app. From this active event handler, you can load up other types of modules instead of the Active Event Executor, which is the one we’ve bee looking at for now.
Below you can see the code that loads up the Active Event Executor …
If you create your own application in C#, the first thing you’ll probably want to do, is to throw away the “Magix.SampleController” project, an handle the “magix.viewport.page-load” active event, to load up your own modules and logic.
If you are wondering about what some active event in Magix is doing, you can pass in “inspect” as its only parameter, having no value, which will show you the description of what an active event does, together with some sample code …








Pingback: Your first Active Event Handler « Magix Illuminate
Pingback: Introducing Magix Illuminate, Open Source Ajax Framework for Mono | Ajax Magazine