locked
prism 5 and WPF RRS feed

  • Question

  • Hello.
    I'm Using prism 5 and WPF and I want to find a good way to communicate between viewmodels.
    I thought two methods.
    1) use a service injected in all ViewModels and use events like winforms
    2) use the event aggregator prism

    2) leads to a spaghetti type programming in my opinion and I'm looking for a solution.
    Is there anything better?
    thank you.
    Saturday, May 7, 2016 5:24 PM

Answers

  • Personally, I prefer MVVM light to PRISM. It's technically superior in a couple of ways. The event aggregator of PRISM requires an object to be passed around. This is not so great. You can make this a bit less painful by passing your object around via your unity container. Still not ideal though.

    MVVM light messenger is much more elegant in that you don't pass anything around at all. You can just use the pub/sub pattern and subscribe to a message of a particular type wherever you want to act.

    http://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between-classes.aspx

    Then there is the service approach which is pretty much synonymous.

    You could alternatively use binding and share a viewmodel via unity or application.current.resources ( allowing you to bind to a property ).

    Another option is to use a static as a bridging object. That means you have no complications in getting a reference to it. Statics are always in memory and make testing a bit of a nuisance.

    Depends on exactly what you're trying to do here, which is best.

    If you subscribe to events then you might want to consider using weak events so you avoid firm dependencies.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 9:42 AM
    • Marked as answer by Xavier Xie-MSFT Tuesday, May 24, 2016 11:24 AM
    Sunday, May 8, 2016 9:33 AM
  • Using an event aggregator correctly does not lead to spaghetti code...it is a simple and clean way of removing the tight coupling between a publisher and a subscriber. Please refer to my blog post for more information about this:

    Using the event aggregator pattern to communicate between view models: https://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/

    The other way to communicate between view models is to use a shared service. Please refer to my answer in the following thread for more information about this: https://social.msdn.microsoft.com/Forums/en-US/22907a0f-d805-4195-8272-7c284b72d2ee/example-of-using-shared-services-prism?forum=wpf

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 9:42 AM
    • Marked as answer by Xavier Xie-MSFT Tuesday, May 24, 2016 11:24 AM
    Sunday, May 8, 2016 7:38 PM

All replies

  • Personally, I prefer MVVM light to PRISM. It's technically superior in a couple of ways. The event aggregator of PRISM requires an object to be passed around. This is not so great. You can make this a bit less painful by passing your object around via your unity container. Still not ideal though.

    MVVM light messenger is much more elegant in that you don't pass anything around at all. You can just use the pub/sub pattern and subscribe to a message of a particular type wherever you want to act.

    http://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between-classes.aspx

    Then there is the service approach which is pretty much synonymous.

    You could alternatively use binding and share a viewmodel via unity or application.current.resources ( allowing you to bind to a property ).

    Another option is to use a static as a bridging object. That means you have no complications in getting a reference to it. Statics are always in memory and make testing a bit of a nuisance.

    Depends on exactly what you're trying to do here, which is best.

    If you subscribe to events then you might want to consider using weak events so you avoid firm dependencies.


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 9:42 AM
    • Marked as answer by Xavier Xie-MSFT Tuesday, May 24, 2016 11:24 AM
    Sunday, May 8, 2016 9:33 AM
  • Using an event aggregator correctly does not lead to spaghetti code...it is a simple and clean way of removing the tight coupling between a publisher and a subscriber. Please refer to my blog post for more information about this:

    Using the event aggregator pattern to communicate between view models: https://blog.magnusmontin.net/2014/02/28/using-the-event-aggregator-pattern-to-communicate-between-view-models/

    The other way to communicate between view models is to use a shared service. Please refer to my answer in the following thread for more information about this: https://social.msdn.microsoft.com/Forums/en-US/22907a0f-d805-4195-8272-7c284b72d2ee/example-of-using-shared-services-prism?forum=wpf

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by DotNet Wang Monday, May 23, 2016 9:42 AM
    • Marked as answer by Xavier Xie-MSFT Tuesday, May 24, 2016 11:24 AM
    Sunday, May 8, 2016 7:38 PM