Differences and similarities between: ViewModelLocator, ServiceLocator, Dependency Injection

好久不见. 提交于 2020-02-25 02:13:01

问题


I'm confused about the patterns: ViewModelLocator, ServiceLocator, Dependency Injection.

The latest conclusion are as follows:

ViewModelLocator. The place to connect View and ViewModel.

public ViewModelLocator()
{
    SimpleIoc.Default.Register<MainViewModel>();
    SimpleIoc.Default.Register<SettingViewModel>();
}

public MainViewModel MainViewModel => SimpleIoc.Default.GetInstance<MainViewModel>();
public SettingViewModel SettingViewModel => SimpleIoc.Default.GetInstance<SettingViewModel>();

// View
private MainViewModel ViewModel => ViewModelLocator.Current.MainViewModel;

Dependency Injection. A set of principles for weak connections. Often through the constructor.

private readonly INavigationService _navigation;

public ShellViewModel(INavigationService navigation)
{
    _navigation = navigation;
}

ServiceLocator. What is it? The same as ViewModelLocator, but considered by many to be an anti-pattern? It turns out ViewModelLocator is also bad. But how then to connected View and ViewModel? ServiceLocator only needs to store Services? As you understand, all the confusion is from ServiceLocator.

Could you explain the differences and similarities between these elements? To finally uniquely identify and use them correctly. Thank you for any help.


回答1:


You recently asked a related question, and from it I infer that ViewModelLocator etc. originate from the sample code base you linked to there. From what I gather from the code examples shown so far, it doesn't look like that code base is the best example of using Dependency Injection.

Thus, any question that involves explaining the 'patterns' in that code base should be met by the response mu.

The question implies a false premise. The premise is that there's something to be learned from that code base. That may not be the case.

Service Locator isn't a pattern; it's an anti-pattern. (Most people seem to agree with me, and so far no-one has been able to produce a compelling counter-argument.)

Dependency Injection isn't a pattern; it's a set of design principles and (several) patterns. That's what we've attempted to describe in our book.

how then to connected View and ViewModel?

The best explanation of the MVVM pattern is still, I believe, Josh Smith's original article. If you want to see a complete code example that combines MVVM and Dependency Injection, it's available with the first edition of Dependency Injection in .NET (which is also included as a free e-book with the new edition).

ServiceLocator only needs to store Services?

No, there should be no Service Locator at all. That's an example of what I mean. It seems that that code base isn't a good way to learn about Dependency Injection. It generates more confusion than insight.



来源:https://stackoverflow.com/questions/60333996/differences-and-similarities-between-viewmodellocator-servicelocator-dependen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!