is this naughty / wrong? passing vectors of data class instances to view...

tom.rhodes's Avatar

tom.rhodes

08 Feb, 2012 02:02 PM

Hi,

i'm just starting to wobble around on these handsome robot legs ;)

i have a wall model, which contains a list of the class BrickModel, which has all the data required to display different bricks in the view. at the moment i have a command that updates the wall model using it's API and this affects the bricks. in the command i dispatch an event to let the app know that the bricks have changed. in my wall view mediator i listen for this event. the event passes a vector of BrickModels to the mediator which calls the view's API to draw the bricks using the vector of the BrickModels.

all of this works nicely, but it feels a little dirty to pass the BrickModels around like this. i have to import the BrickModel class in the wall model, the event, the mediator and the view. i coudl do it with anonymous objects, which meanas that the views don't have to know what tyoe of object they are dealing with. but that just seems really nasty.

am i missing something? or am i worrying about nothing?

  1. Support Staff 1 Posted by Ondina D.F. on 09 Feb, 2012 12:20 PM

    Ondina D.F.'s Avatar

    Hi Tom,

    What you’ve described is something similar to using a VO. If the WallModel would extend Actor, it could dispatch an event itself, with a BrickVO or a collection of BrickVOs as a payload, so the command would only have to call a wallModel.updateBricks() method or something.

    A VO (value object) is a strongly typed data structures storing values:

    public class BrickVO
    {

    public var id:int;
    public var material:String;
    public var weight:int;
    

    }

    (Of course, you can use accessors methods instead of the above)

    It is a common practice to use VOs for marshalling data around between the various tiers of an app. VOs’ instances represent data from the Domain Model and can be used by Views to display data in different ways (datagrid, lists, input fields, graphics) and allow the user to interact with the data. Modified data can be sent back to the Model via VOs. In this scenario VOs are the data carriers, moving data from the Model to the View and back.
    There are some advantages of using VOs: VOs can make the design clearer and cleaner, they can be reused (say, you want to build your app using another framework), compile-time type checking ...

    i have to import the BrickModel class in the wall model, the event, the mediator and the view.

    I hope you didn’t mean importing the Mediator and the View into the WallModel, because that would be wrong.

    i coudl do it with anonymous objects, which meanas that the views don't have to know what tyoe of object they are dealing with

    I’m not sure what you mean by “anonymous objects” in as3.
    Do you mean something similar to structs in other languages?
    Or, do you mean Objects that can have properties assigned to them dynamically?
    var brick:Object = { id: 0, material: “ceramic”, weight: 50};
    brick.color=”scarlet”;
    or in a loop?
    or created inline:
    someFunction({id: 0, material: “ceramic”, weight: 50});
    or a dynamic class?

    Anyway, you can use an Array, ArrayCollection, Vector or Object (with the data needed by the View) instead of a VO or a collection of VOs, if you don’t like VOs:)
    Does this help you?

  2. 2 Posted by tom.rhodes on 09 Feb, 2012 12:41 PM

    tom.rhodes's Avatar

    great, it's just a terminology thing then i think. i'd called a VO a model and reading the book and the best practices started to get a bit worried about passing models around. when i should be callin gthem VO's.

    "I hope you didn’t mean importing the Mediator and the View into the WallModel, because that would be wrong."

    nah, i just meant that all those elements have to know about the VO's type and as such import the VO's class. so the VO starts to be a strong bond between all these loosely coupled elements. i suppose i can use an interface to abstract it a little further if i felt it necessary.

    anyway thanks, i was worrying about nothing it seems :)

  3. tom.rhodes closed this discussion on 09 Feb, 2012 12:42 PM.

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