Is the standard ViewModelLocator of MvvmLight antipater? And how to mitigate this?

When starting a new mvvm-wpf application, I usually turn on mvvm-light right at the start. This works fine until my application grows.

Some where along the line ViewModelLocator becomes huge (many ViewModels for all kinds of ChildViewodels). And further down the rabbit hole, I need several different instances of the same model. (for example, for a list of items you would like to interact with on the same screen). This is where the struggle begins, how to deal with this pleasant, consistently keep the code under control?

So, if I want to get rid of ViewModelLocator (is it antipattern ? Feels like a ServiceLocator), should I move to ViewModel-first and create (many) abstract factories for all ViewModels?

+5
source share
1 answer

ViewModelLocator is a fancy name for the navigation bus used for inverse control (IoC). Although this seems to be a newer technology, the navigation bus does use the service bus differently. This is not an anti-pattern if you have a static (shared in VB) container. An anti-pattern is included if you pass the container in your ViewModels.

In MVVM, remember that this is a universal design template, and you can expand it in many ways. The best solution for large projects is component design (a design in which each function of your application is in its own namespace or project).

The design may look like this:

  • Customer
    • Models
    • ViewModels
    • Services
  • Orders
    • Models
    • ViewModels
    • Services

etc ... It really depends on the taste of the developer. As long as your design is consistent.

Further reading: To better understand what ViewModelLocator is looking for on the Navigation Bus . To better understand EventAggregator, find the Message Bus

0
source

Source: https://habr.com/ru/post/1264120/


All Articles