0

Following on this question's comments, I feel obliged to ask this one.

It's an application I'm currently working on, and to avoid further issues and to keep on working with Catel (which, whatever troubles I may have with it, is really pleasant to use), I will need explanations on a few topics.


I have an API class (let's call it API) which contains several methods to connect to some content manager and perform actions on it. It's a single class that I'd like to split into several Model classes (for the example, two will do):

  • Connection : pretty self-explanatory, would contain two Commands calling API.Connect and API.Disconnect
  • DataSource : indicates where/on what in the content manager the application will be work, with one Command calling API.LoadDataSource

As far as the models go, it's ok, nothing too hard. My first issue comes with the associated ViewModels; I need to use the same instance of API in every single of these. So for now, I use only one ViewModel, and the UserControls associated with my Models just inherit it from my MainWindow (these are regular UserControls, not Catel's, else it yells at me for not having ViewModels for them).

Since Catel isn't too fond of nested viewmodels, here's my question:

How do I pass/use a parameter between various ViewModels? (or how do I separate my main ViewModel without using nested viewmodels?)

3
  • Tons of different ways. Like using IoC containers to manage instances and deliver dependencies. Or instantiate your api as a resource in app.xaml and then bind it to your view models. More ways to do it than not to do it.
    – user1228
    Jan 29, 2015 at 14:08
  • @Will that's my issue: I don't know how. I searched, I saw a lot of things, I don't know which ones are related to the others, I'm plainly lost.
    – Kilazur
    Jan 29, 2015 at 14:14
  • Tons of different ways. Pick one. It's easy.
    – user1228
    Jan 29, 2015 at 14:26

1 Answer 1

1

When I need to persist state in an application, I use services for this. View models are a representation of a view, and will only live while the view is active / visible. State goes beyond that, it needs to be managed throughout the whole app in different view models.

Therefore I always choose to have a service that holds the state. This service is registered in the ServiceLocator (IoC container) and will be injected into the view models via dependency injection.

This way you can always retrieve the information when you load the view models. The best way to do this is in the Initialize method (you should subscribe your events here as well). You should clean up your view models (unsubscribing events) in the Close method of the view model.

3
  • Looks good; I can implement my service any way I want, right? I thought I would have to implement some Catel's interface, but looking at IMessageService and MessageService sources, it doesn't seem to be the case. Yet, I don't see which events you're talking about; should I have special events, or are you just referring to WPF events? And, in a MVVM point of view, can services have knowledge of models?
    – Kilazur
    Jan 29, 2015 at 14:50
  • The "events" you are talking about are methods on the view models (just override them). Services have nothing to do with Catel. Catel does implement a lot of stuff as services though so you can easily replace a default implementation by your own. Jan 29, 2015 at 18:16
  • Ok then, thanks a bunch. I'll be sure to annoy you again soon :p
    – Kilazur
    Jan 29, 2015 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.