如何使用依赖注入而不是服务定位器 [英] How to use Dependency Injection and not Service Locator

查看:92
本文介绍了如何使用依赖注入而不是服务定位器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听到有人说你不应该使用服务定位器进行依赖注入。那么在不依赖于服务定位器的情况下,您如何注入依赖项呢?我想尝试IoC容器,但是不想进入反模式。



如果你只是设置一切,所以有一个地方,所有的类总是有一个依赖链条到最深的班级? (如果我/这是有道理的)



我不是所有的代码都依赖于选择的IoC容器,是吗? p>

那么你在哪里使用你的容器(用于重新安装)?而且,如何让代码尽可能地解决所有问题呢?是否通过使用界面通过每一层直到前面层来设计一切正确的方法的一部分?



或者我只是缺少一个点?



让我提醒你,我只是不想陷入反模式,需要一些技巧/一个头脑。

解决方案


如果你只是设置一切,所以
有一个地方,所有的类
总是有一个依赖链
最深的班级? (如果我/这使
感觉到)


是的,这被称为 strong>的应用程序,并且它将在哪里配置IoC容器并解决您的根类型。


这是不正确的所有你的代码
依赖于选择的IoC
容器,是吗?


正确,它最好不要通过引用您的类型的IoC容器,因为这将使它们不再可重用,并将类型与IoC容器的概念相结合。


那么你在哪里使用你的
容器(用于rexolving)?那么
如何解决所有问题,就像你的代码一样,
深?是否通过使用界面通过每个
层直到前面层设计一切正确的方式
的一部分?b $ b

您将使用您的组合根目录下的容器,以及代码中需要通过容器实例化类型(即从工厂类型)的任何位置(通常用于依赖关系链支持)。



许多IoC容器可以为您生成这些工厂类型,因此您只需要传递,例如 IMyFactory 作为依赖关系,或在某些IoC容器的情况下,一个 Func< IMyService> 。这意味着您不需要创建对您的IoC容器具有依赖关系的工厂类型。



在使用接口方面,依赖关系反转原则规定您应该取决于抽象,而不是结构化,所以如果你想采用依赖注入,你将需要考虑这个概念的代码。


I am hearing people say you should not use Service Locator for your Dependency Injection. So how exactly do you inject the dependencies without relying on a service locator? I want to try out IoC containers, but don't want to land into an anti-pattern.

Should you just set everything up so there is one place where all classes always have a dependency chain to the deepest classes? (if I/that makes sense at all)

I isn't right to have all your code littered with dependencies on the IoC container of choice, is it?

So where do you "use" your the container (for rexolving)? And how do you get it to resolve everything, as deep as your code goes? Is it a part of designing everything the right way by using interfaces through every layer up till the front layer?

Or am I just missing a point?

Let me remind you that I just don't want to fall into an anti-pattern and need some tips / a heads up on it.

解决方案

Should you just set everything up so there is one place where all classes always have a dependency chain to the deepest classes? (if I/that makes sense at all)

Yes, this is called the composition root of your application, and it's where you would configure your IoC container and resolve your root type.

It isn't right to have all your code littered with dependencies on the IoC container of choice, is it?

Correct, it is better to not pass references to your IoC container around your types, as this will make them less reusable, and couple the types to the concept of IoC containers in general.

So where do you "use" your the container (for rexolving)? And how do you get it to resolve everything, as deep as your code goes? Is it a part of designing everything the right way by using interfaces through every layer up till the front layer?

You would use your container at your composition root, and anywhere in your code that needs to instantiate types (i.e. from factory types) via the container (usually for dependency chain support).

Many IoC containers can generate these factory types for you, so you only need to pass, e.g. IMyFactory as a dependency, or in some IoC container's case, a Func<IMyService>. That means that you don't need to create factory types that have a dependency on your IoC container.

In terms of using interfaces, the Dependency Inversion Principle states that you should depend on abstractions, not on concretions, so you will need to factor your code with this concept in mind if you wish to adopt dependency injection.

这篇关于如何使用依赖注入而不是服务定位器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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