nitin
nitin

Reputation: 75

How to use model in mvc architecture based application in flex

In a MVC architecture the model is used to store data used in the application. I create a class and use some static properties to store data so that it can be used throughout the application. For example if there is a datagrid, I use a static arraycollection in model class. Update it whenever a service is called and by using databinding update the dataprovider of the datagrid. I have read that singleton class can also be used to accomplish this task. Is the way I use for model class is better or there exists some other ways too?

Upvotes: 0

Views: 310

Answers (2)

RIAstar
RIAstar

Reputation: 11912

As the discussion Tianshen referred to points out, there is hardly any difference between a bunch of static variables and a Singleton. Both can be seen as a form of global variables, with all the accompanying downsides. I'm not going to argue about how Singletons are evil here; plenty has been written about the subject already that you can easily find on the web.

However, I would like to present an alternative: Inversion of Control (IoC), also referred to as Dependency Injection (DI). I'll explain this pattern in short, but you can also find plenty of information for yourself. Take the ArrayCollection from your example; if you'd want to avoid the static variables or the Singleton pattern, you would have to create one instance and pass that instance around from object to object throughout your application and perhaps it would even have to be passed through an object that doesn't really need it, which wouldn't be very clean either.
In comes the IoC container (for a Flex app it would take the form of library you add to your project): with such a library, you can create/configure that ArrayCollection in one place and let the IoC "inject" that single instance in whatever class that needs it.

A concrete example: we might have a configuration file like this

<fx:Object>
    <s:ArrayCollection id="myLetters">
        <fx:String>A</String>
        <fx:String>B</String>
    </s:ArrayCollection>
</fx:Object>

and a class like this

public class MyClass {

    [Inject(id="myLetters")]
    public var letters:IList;

}

The IoC container would then inject the myLetters ArrayCollection instance whenever a MyClass would be instantiated. There are a lot of other injection techniques, but this example is just to give you an idea.

At the time of this writing I belive Parsley to be the most widely used IoC container for Flex.

Upvotes: 1

Tianzhen Lin
Tianzhen Lin

Reputation: 2614

You may read the this discussion that compares Singleton and Static. Though it is in C#, the similar Object-Oriented philosophies apply.

In short, Singleton gives you better flexibility. If you use Databinding for your Flex application, using Singleton would allow you to inherit EventDispatcher, with which you can dispatch custom change events. Custom change events would give your application better performance.

Upvotes: 0

Related Questions