不清楚如何实现依赖注入 [英] Unclear on how to implement dependency injection

查看:146
本文介绍了不清楚如何实现依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个LoginView及其数据上下文LoginViewModel,需要注入一个可以根据用户名/密码验证用户的服务。



现在让我们说应用程序的状态是有人已经登录,但现在他们正在注销,我需要重新显示下一个用户的登录屏幕。所以在这一点上,我需要一个我的LoginViewModel的一个实例,但是我不知道该怎么做。



我应该将一个LoginViewModel注入到我的ShellViewModel中,到它并重新使用它?这似乎很奇怪,因为我不想在记忆中保留这个内容,而我没有使用它(在这种情况下不是很大的事情,而是可以适用于其他情况)。



如果需要创建LoginViewModel,我应该将身份验证服务注入到ShellViewModel中吗?这似乎是奇怪的,因为我的ShellViewModel不需要对这个服务做任何事情,如果这是答案,那么我会把所有的东西注入我所看到的所有其他ViewModel的ShellViewModel中。



我知道我不应该在我的应用程序的根目录之外的任何地方引用我的DI容器,或者我将实现一个服务定位器模式。



诚然,我现在感觉很笨,我相信当我听到这个答案的时候我会打脸,所以这是什么?

解决方案

在完成任务之后,只需查看过去几个小时的这个主题的各种博客,问题和答案,我开始得出结论像Asp.net MVC这样的框架使得实现纯粹主义者(也就是像马克·塞曼这样的人物)方法相当可行。



这样的框架往往会使其成为可能,因为框架本身利用了(gasp !!!)服务定位器模式。不幸的是,在像WPF这样的平台上使用MVVM设计并不是很简单,因为在内部服务定位器的帮助下,您的视图并不总是从根本上提供的。



然而,我有一个ShellViewModel负责显示我需要的大部分视图,所以我认为最实际的答案是我认为我的ShellViewModel为组合根的一部分,并将我的依赖关系扩展到DI容器中。



除了我的ShellViewModel之外,我相信500的答案是正确的。


Let's say I have a LoginView and its data context, LoginViewModel, needs to be injected with a service that can authenticate a user based on their username/password.

Now let's say that the state of the application is that someone has already logged in, but now they are logging out and I need to redisplay the login screen for the next user. So at this point I need an instance of my LoginViewModel but I'm not sure how to go about getting it.

Should I be injecting a LoginViewModel into my ShellViewModel and holding onto it and reusing it? That seems odd because why would I want to keep that in memory while I'm not using it (granted, not a big deal in this case but could be for other cases).

Should I be injecting the authentication service into ShellViewModel to hold onto for whenever it needs to create a LoginViewModel? That seems odd because my ShellViewModel doesn't need to do anything with this service and if this were the answer then I'd be injecting all kinds of stuff into my ShellViewModel for all the other ViewModels it displays.

And I know I'm not supposed to be referencing my DI container anywhere other than the root of my application or I'll be implementing a Service Locator pattern.

Admittedly, I'm feeling pretty dumb right now and I'm sure I'm going to slap myself in the face when I hear the answer... so what is it?

解决方案

After having done nothing but look up various blogs, questions, and answers on this subject for the past several hours I'm beginning to come to a conclusion that a framework such as Asp.net MVC makes implementing a purist's (aka someone like Mark Seemann) approach quite doable.

Such frameworks tend to make this possible because the framework itself utilizes a (gasp!!!) Service Locator pattern. Unfortunately, using an MVVM design on a platform like WPF doesn't make it quite as easy due to the nature of how your views aren't always served up from the root with the help of a built in Service Locator.

I do, however, have a ShellViewModel that is responsible for showing most the views I need and so I think the most practical answer here is for me to consider my ShellViewModel as part of the composition root and extend my dependency on the DI container into it.

Beyond the point of my ShellViewModel though, I believe 500's answer is correct.

这篇关于不清楚如何实现依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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