wpf - What is a ViewModelLocator and what are its pros/cons compared to DataTemplates? -


Do I have a View Modellococtor, how it works, and what kind of professional / cons to use Is this data compared to templates?

I have tried to find information on Google but there are not many different implementations and no Stratelot list as it is and it is professional / opposition to use it.

intro

In MVVM generally their views models by solving ideas To find out (DI) from the container it automatically occurs when the container is said to provide (see solution) An example of a class container injects a ViewModel parameter in ViewModel view By accepting a manufacturer who watches;

Benefits of DI

The main benefit here is that the container can be configured on with run time instructions how we request it This allows it to resolve the maximum number of test qualifications (scenes and view models) to direct it when we use, when our applications actually run, but the unit tests for the application On run Direct it in different ways. In the latter case the application will also not be a UI (it is not running, it is just a test) so that the container will be resolved at the "normal" type of place, when the application runs.

Problems stemming DI

We have seen that the DI approach allows easy testability for application by adding an intangible layer on the creation of application components. There is a problem with this approach: It does not perform well with visual designers such as Microsoft Expression Blend.

The problem is that both the common application runs and unit test runs, what is the container with instructions for any solution to set up ; In addition to this, to empower someone to ask for a determination, so that they can injected the voidodels.

However, there is no code in our design time running . The designer tries to use the reflection to make an example of our ideas, which means that:

  • If the visual creator requires a view model instance then the designer will see the scene at all Will not be able to instantiate - this will be a error in some controlled way
  • If the view is a parasailable constructor, the view will be made immediately, but its code will be Datacon will be null then we will get a "blank" view in the designer - which is not very useful

    enter ViewModelLocator

    ViewModelLocator is an additional abstraction used like this:

  • By any means detects that we are in design mode
  • see
  • View
  • Automatically shows a visual modellocator as its share and its datacentext to the locator's view model
  • If not in design mode, the locator gives a view modal that resolves it with a di container, as explained above If in the design mode, the locator sees a definite "dummy" modell using its logic (remember: there is no container in design time!); This viewodel is usually prepared with dummy data

    Of course it means that the scene must be a parlayable constructor to start with (otherwise the designer is not able to instantiate it. ).

    SUMMARY

    MODELLOCKER is a phrase that helps you keep the benefits of DI in your MVV application, while allowing your code to play well with Visual Designer is. This is sometimes called the "mixing capacity" (referring to expression blend) of your application.

    After the above digestion, see a practical example.

    Finally, using the Data Template option to use a ViewModelLocator, but the option to use explicit view / view model pairs for parts of your UI. Often you can find that there is no need to define view for view model because you can use a data template instead.

Comments