locked
Two Controllers sharing one model RRS feed

  • Question

  • Hi, really hoping that a) I've posted this in the right forum, b) that someone can help me out.

    I'm implementing a MVC pattern that contains two controllers that both need to access the same instance of a model. I'm having problems trying to work out how to get the controllers to share one instance of an model. The model can't be a singleton object and each instance of the model contains it's own data, so the data cannot be static across all model instances.

    I'm thinking of using a third controller that contains the model instance and the other two now child controllers. All events raised by the views notify the child controllers that notify the parent contoller, which updates the shared model.

    Would you recommend this approach or is there another simplier way that would be better?

    Thursday, February 23, 2006 11:48 AM

Answers

  • Hi Derek,
     
    correct me if I'm wrong, but what you need is a shared storage of some sort, right ?
     
    It seems like you'll need to create some sort of storage class that can be accessed by both controllers and can return the model according to some kind of an id.
     
    The storage itself can be a built-in array in the storage class, a cache mechanism that the storage class addresses etc.
     
    BTW, you can look at this storage class as a "Model Agent" (just another fancy name with no actual meaning).
     
    Best of luck,
    Ido.
    Thursday, February 23, 2006 6:01 PM

All replies

  • Hi Derek,
     
    correct me if I'm wrong, but what you need is a shared storage of some sort, right ?
     
    It seems like you'll need to create some sort of storage class that can be accessed by both controllers and can return the model according to some kind of an id.
     
    The storage itself can be a built-in array in the storage class, a cache mechanism that the storage class addresses etc.
     
    BTW, you can look at this storage class as a "Model Agent" (just another fancy name with no actual meaning).
     
    Best of luck,
    Ido.
    Thursday, February 23, 2006 6:01 PM
  • Hi Ido, first off thanks for replying, I appreciate it. Most of the IT folk I work with don't even know about objects so its impossible to get feedback on ideas.

    Your right that is exactly what I need, shared storage, and a class to control access to the models stored in it. When I made the original post I thought it would  be a class to control multiple controllers accessing one model, rather than a class to control which model, from a group of models, the controllers would use. 

    Your idea actually improves the overall approach of the application. It makes it so much simplier and covers some issues I hadn't even thought of.

    Thank you very much! That was a great help.

    Derek

    Thursday, February 23, 2006 9:32 PM
  • Hi Derek,

    I would really like to know which idea you finally worked out.

    I am not sure why you came into this struggle, since the modell should never contain any stored data. The modell, as far I am used to this pattern encapsulated the functionalities only. Such as writing a data object into the DB etc.

    I must mention that I`ve used MCV in small projects only. So its really interessting to me how you have solved your problem.

    Tuesday, February 28, 2006 2:57 PM
  • Sorry it took a while to reply there Tillman, been busy coding up the solution.

    I'll give you a little background on the application. Essentially I was developing an xml editor that would help me learn Xml and its related technologies as well as the Xml classes in .NET. The application is a MDI application that lets me edit multiple xml documents. The models in this case were instances of each opened xml file, the controller (creatively named XmlController) would work with these multiple models as well as with multiple views of the models, a text view and tree view.

    Prior to Ido's idea of shared storage I pictured the models being created and stored independantly of each other. I couldn't work out how a single controller could work on multiple models that were displayed in mutiple views. 

    Essentially I created a model class that contained a string for it's data, a string for an ID (the file path) and like you say methods to save itself and to determine if it had been saved, etc. Then I created a ModelAgent class that was responsible for storing the model, creating new models, returning the correct model to the XmlController for the active view, and for deleting, editing and updating a model, all based on an ID.

    Each of the views contained the ID (file path) of the model it was displaying and thats all. When the view raised an event to the controller, the controller asked the view for it's ID which it then passed to the ModelAgent which returned the correct model, the controller then did what it needed to do.

    It works extremely well! I've even expanded it to perform XPath and XSLT transformations with very little modification to the structure. It means now there are many different models that are displayed in lots of different views all controlled by one controller.

    I always pictured the model as having data as well as the methods to save, protect, modify, etc the data that it was modelling but you know design patterns are just a rough guide.

    Thursday, March 2, 2006 12:28 PM