I am creating a chat application that will also have the ability for file transfer. I am trying to design it with the MVVM pattern utilizing Prism.
My model for this application, or at least this one module(the application only has one module at this time, and it might be the only module), is a ChatManager class that is responsible for hosting servers, joining servers and holding all the state information of the chat sessions. It might be a client that is connecting to another server, or it might be both a client and a server as any individual who hosts a chat will also be a client to his own server.
In my application I have different views and viewmodels for each of the different areas of the program, the message area, the area that has the user list, and the area where users type their messages. The views know nothing about the viewmodels, and the viewmodels know nothing about the views. The viewmodels are very simplistic and contain nearly no logic.
All the logic resides in my ChatController class which basically controls the flow of the application. It is responsible for creating all of the views, all of the models, assigning the proper viewmodels as the datacontexts of the views, and injecting each of the views into the shell at the appropriate time. It also has the responsibility of assigning the actual data to the viewmodels, from the model.
The problem I’m having is how to cleanly keep the viewmodel ObservableCollections up to date with the Lists contained within the model. For example, in the model I have a list of connected users. Whenever that list changes, such as when a user connects or disconnects, I need to update the ObservableCollection of users in the viewmodel that handles the user list.
The way I’m thinking I need to accomplish this is using events. So I’ve created events that are fired in the model whenever a user connects, whenever a user disconnects. Surely I must have events as well for when the server is sending out a message to all of the users and all the other things the model actually does.
I guess my question is, is this a good way of doing it? Should I instead change the model’s user collection to be an ObservableCollection and avoid the events altogether, simply assigning the viewmodel’s collection to the model collection?
The more I try to structure this application, the more confusing it becomes for me. Right now my controller is quite large in the number of methods it has and I haven’t even begun to add all the features of the chat program that I want to.
I think the whole question’s redundant because of the last but one paragraph 🙂
ObservableCollection is part of System.Collections.ObjectModel. It’s nothing to do with any particular UI technology so I don’t see any need to keep it confined to the view model rather than having it up in the core and replacing your Lists.