Are Model classes useless ?

Migs's Avatar

Migs

24 May, 2012 12:19 PM

Hello,

I've been following some examples about how to use Models in a MVCS project and as I was progressing in my project I realised that I couldn't see any benefit of using my Models the way I use them. So, first of all, I'm wondering if I'm using them right and secondly, if that's the case, then can somebody explain what's the point? Because I don't get it :)

Here is what I do:

A service receives remote data and transfers it to the model:

protected function getAllSBServersSuccess(e:ResultEvent):void{

        var DBServers:ArrayCollection = new ArrayCollection(e.result as Array);

        dbServerModel.setAllDBServers(DBServers);
}

Then in the model, I do a copy of the data and dispatch an event to transfert the result to my Mediators:

public function setAllDBServers(DBServerList:ArrayCollection):void{
        this.DBServerList.removeAll();

        for each(var dbServer:DBServer in DBServerList){
            this.DBServerList.addItem(dbServer);
        }

        dispatch(new DBServerEvent(DBServerEvent.GET_ALL_DBSERVERS_LOADED, this.DBServerList));
}

Then in my Mediators, I use the result object sent along with event.

So my question is, why not dispatching the event directly in the service instead of using a Model class, doing a copy and then dispatching... Because as it is here I really don't see the point except if you like to add classes and have an even more confusing code :)

I've been using the following diagram to help me and I think the part I don't quite understand is the step 5a:
Image Name

Thank you for your help

  1. 1 Posted by matt on 24 May, 2012 12:32 PM

    matt's Avatar

    your model should store the state of your application, services are responsible for fetching/sending data which may also be stored in a model. You can combine the two but keeping them separate should in theory make for less confusing code (your model dealing purely with manipulating data without having to worry about how and where it comes from), and also allow you to swap out different services without having to touch your model. A service may well also be used by multiple models so again, separating out the responsibilities gives you far more flexibility.

  2. 2 Posted by Migs on 24 May, 2012 01:03 PM

    Migs's Avatar

    Thank you matt for your answer,

    However my issue is not really that I'm trying to put everything in the service, it's more that I feel like I don't need the model at all whether it's merged in the service or seperated.

    I'm using ampfphp, this means that the data that I receive from my service is already an instanciated VO. This VO represents in a way my model and I can transfer it to my view without the need of a Model class in between. At least that's how it feels in this scenario...

  3. 3 Posted by Stray on 24 May, 2012 01:52 PM

    Stray's Avatar

    Hi Migs,

    I instinctively agree with Matt - and from a 'testing' and 'knowing the state of the world' point of view, models are useful - not least because for most of us they feel like a better fit with our brain's understanding of the app.

    However - there are many styles of programming that are essentially 'stateless' - in which you would take the approach you're floating, where the values get passed along to the interested parties without being stored in a model along the way.

    The two approaches each have merits.

    In fact, we frequently skip the model when dispatching results from services - any message about progress, errors and so on is likely to be sent right from the service out to the view or logger without being stored in a model.

    On the other hand, data that might want to be restored, have undo or history, be retained for checking whether there are actually changes to save, be used for logic or flow, and so on, clearly needs a model.

    I think the question you're really asking is "in the context of my app, is this a model, or is it just a message?". Your model still exists - it's just outside of the app, sitting on a server somewhere. You've abstracted the M layer of your MVCS entirely into the backend. That's ok - it's just as valid as when we abstract any other layer.

    The AWESOME thing about Robotlegs is that you can develop *without* the model, and then, if you find that you *do* need a model, you can just add a command that also picks up the values and stores them in a model. Changing your model / no model decision with Robotlegs should be trivial.

    So - high fives to MVCS for allowing a layer to be included / removed within one technical domain as required.

    hth - great Q by the way.

    Stray

  4. 4 Posted by matt on 24 May, 2012 01:53 PM

    matt's Avatar

    in that case, if it feels right - i would say it probably is right : )

  5. 5 Posted by Migs on 24 May, 2012 02:26 PM

    Migs's Avatar

    Thank you Stray. That's exactly the kind of answer I needed :)

    I love the fact that I can use models only when I need them. I just hope that I will be able to tell my self "hey here I need a model it feels right!". But you gave a few examples giving me a rough idea so I should be alright.

    Thx again, you rock ;)

  6. Ondina D.F. closed this discussion on 21 Jun, 2012 08:19 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac