之间的区别和相似之处:ViewModelLocator,ServiceLocator,依赖项注入 [英] Differences and similarities between: ViewModelLocator, ServiceLocator, Dependency Injection

查看:73
本文介绍了之间的区别和相似之处:ViewModelLocator,ServiceLocator,依赖项注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对模式感到困惑: ViewModelLocator ServiceLocator 依赖注入

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

最新结论如下:

ViewModelLocator 。连接 View 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 。它是什么?与 ViewModelLocator 相同,但是很多人认为它是反模式吗?事实证明 ViewModelLocator 也很糟糕。但是如何连接 View ViewModel ServiceLocator 仅需要存储服务?如您所知,所有的困惑都来自 ServiceLocator

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.

推荐答案

最近问了一个相关问题,然后据此推断出 ViewModelLocator 等源自您链接到该示例代码的示例。从到目前为止显示的代码示例中收集的信息来看,似乎该代码库并不是使用依赖注入的最佳示例。

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.

因此,任何问题涉及解释代码库中的模式,应通过响应 mu 来满足

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.


然后如何连接 View ViewModel

我相信,对MVVM模式的最佳解释仍然是乔什·史密斯的原始文章。如果您想查看结合了MVVM和依赖注入的完整代码示例,可以在 .NET中的第一版依赖注入中找到它。 (新版本也包含在免费电子书中)。

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 仅需要存储服务?

不,根本没有服务定位符。这就是我的意思的一个例子。似乎该代码库不是学习依赖注入的好方法。它比见识引起的混乱更多。

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.

这篇关于之间的区别和相似之处:ViewModelLocator,ServiceLocator,依赖项注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆