Flex4 - Coding architecture, view/model separation
In the project I'm working on, we've just started to use Flex 4. We don't use any particular framework and management wants a full view/separation architecture. The basic principle is to associate each time an ActionScript class as a model to the .mxml view.
I'm having a hard time understanding how to link the two : who references who, and to what extent can I eliminate the script part in my view.
I'd r开发者_如何学编程eally appreciate any insights on this one.
Thanks
You also have the ability to use the "Presentation Model" pattern, which is very similar to MVVM in the Silverlight world. The difference is that in Flex, it is VERY easy where Silverlight requires a ton more plumbing.
The architecture goes like this:
- The View is just a view. All declarative... very little imperative code
- The Presentation Model (PM) contains UI behavior and glue to the model
- The Model contains the data, services and client-side business rules
The View references the PM and uses data binding to properties in the PM. The PM Wraps the Model and adds UI-level validation, UI-level behavior and Formats data.
"Separated Presentation Patterns" like PM, MVC and MVP are really important for the following "ilities":
- Unit Testability
- Designability
- Maintainability
- Migratability (Browser to Air to device... etc)
Usually, models are supposed to contain nearly no logic at all. Views contain the logic to display the data the models provide. As such a view accesses the models, and not the other way round. This also makes it possible to define multiple different views for the same set of data.
In Flex, data binding and content providers are both a good way to integrate data into the MXML structure. Especially data binding allows you to just use the content without thinking about it much; and especially without defining all the references yourself (so you don't need to write those in code).
To eliminate the rest of the logic from the view, you usually use controllers in the MVC architecture. Controllers are some "middleman" between views and models and are supposed to process the data. So all logic your application uses is supposed to be there. That way you have a strict separation of models (which only contain the pure data), views (which only display data) and controllers (which brings them all together and adds the application logic in between). See the Wikipedia article for further information on that topic.
And as a final note, there are some frameworks that make MVC easier to manage. Most known for ActionScript is probably RobotLegs.
精彩评论