6 Replies Latest reply on Jan 14, 2010 1:47 PM by peteandrus

    Question about implementing MVC

    peteandrus Level 1

      i'm trying to get better and structuring my Flex apps better. i'm trying to implement some semblance of MVC in my code.

       

      my question is: where does the Model live? How is MVC typically implemented in Flex?

       

      It seems to me that the View ought to contain the Model in it with a separate controller class that handles events, but that doesn't seem right either.

       

      If the Model lives in the Controller, then it will make it difficult for multiple Controllers to access the Model, which is supposed to be allowed in MVC.

       

      So, where the heck does the Model live? thanks!

        • 1. Re: Question about implementing MVC
          pauland Level 4

          If you consider the singleton pattern, then multiple controllers can reference the same model.

           

          I tend to think of the model view and controller parts co-existing rather than being part one of another.

          • 2. Re: Question about implementing MVC
            Gregory Lafrance Level 6

            Here is my Flex Coobook post on creating a simple MVC architecture. The model is implemented as a singleton.

             

            http://cookbooks.adobe.com/post_Building_a_wizard_using_a_simplified_MVC_architect-11246.h tml

             

            If this post answers your question or helps, please mark it as such.


            Greg Lafrance - Flex 2 and 3 ACE certified

            www.ChikaraDev.com

            Flex / AIR Development, Training, and Support Services

            • 3. Re: Question about implementing MVC
              peteandrus Level 1

              pauland,

              I feel that way too about MVC  (I think), but these objects have to live somewhere. So, in Greg's example, if i've read it correctly, the Model and Controller live in the View. That's what i thought was the way to do it, but now i've seen it i think it makes more sense. We'll see.

               

              Greg,

              thanks for that example. it's got a ton of stuff for me to eat up. I'd never used or seen use of the SystemManager. Now i'm interested in figuring out how it works in more detail. Plus, the MVC example is excellent. However, i have some questions:

              1-you use the .getInstance() method to make changes to the WizardModel inside the WizardController class. This seems like it isn't very extensible. What if i wanted to use this same functionality again in some other place in my code? Wouldn't the restriction of WizardModel to only having one instance preclude me from doing so? It seems like i'd need to write a new WizardModel class named something else to do so, which seems like it defeats the purpose of reusability from MVC. maybe i'm not understanding it correctly.

              2-wouldn't it be more effective to house the Model in the Controller as a public member of WizardController, then let the View bind to it that way? Plese explain why not if you don't agree. I'm just trying to learn here.

               

              thanks!

              • 4. Re: Question about implementing MVC
                pauland Level 4

                peteandrus wrote:

                 

                pauland,

                I feel that way too about MVC  (I think), but these objects have to live somewhere. So, in Greg's example, if i've read it correctly, the Model and Controller live in the View. That's what i thought was the way to do it, but now i've seen it i think it makes more sense. We'll see.

                 

                 

                Well the objects do all live some where - in the context of the application. Even if you consider the Model as being part of the view (a concept I hate), the view lives somewhere - ie in the application.

                I tend to think that there is one model but potentially multiple controllers and views - hence my reluctance to consider the Model as being part of a view.

                 

                Ultimately, as long as there is seperation and an understood framework, it's all good.

                Paul

                • 5. Re: Question about implementing MVC
                  Gregory Lafrance Level 6

                  I'm not extremely knowledgable on MVC, which is why I created that Cookbook post as a way to learn more about the basics.

                   

                  I heard the model should be a singleton, may others with greater experience can chime in as to why this is a best practice.

                   

                  I thought the Model, View, and Controller should be kept as separate as possible, but perhaps others could chime in about putting an instance of the Model in the Controller.

                  • 6. Re: Question about implementing MVC
                    peteandrus Level 1

                    i appreciate the help. i might be misunderstanding as well. i'm just trying to get a good handle on this. i am on the cusp of starting a project at work and want to organize my code well from the start. i appreciate your example. most MVC examples are so trivial that they, IMO, lose all potential for teaching. yours is much more in-depth. thanks again