依赖注入的方式 [英] Ways of Dependency Injection

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

问题描述

制罐工厂模式(工厂方法,简单工厂方法和抽象工厂)或服务定位器获取的依赖被称为依赖注入模式的方法。



我的理解是与实例依赖这种方式,高水平的类仍然必须了解有关工厂或服务locator.Dependency喷射装置的依赖推,而不是工厂不同类型,做到这一点的最好办法是拉使用类似容器控制反转。NInject,团结,CastleWindsor等请分享您的意见


解决方案

依赖注入和服务定位器是的两个反对办法对接口进行编程的,但依赖注入可以解决大部分的问题和挑战远比服务定位器反模式更好。



抽象工厂可以注入需要它们的服务,所以可以用两种方法进行组合。它们垂直于讨论,不是服务定位器的一个变体。

Can the Factory Patterns (Factory Method,Simple Factory Method and Abstract Factory) or Service Locator Pattern way of fetching the dependency be called Dependency Injection.

My understanding is with this way of instantiating dependency, the high level class still have to know about about the Factory or Service locator.Dependency Injection means the dependency is pushed and not pulled unlike Factory type and the best way to do it is using Inversion of Control containers like NInject,Unity,CastleWindsor etc. Please share your views.

解决方案

Dependency Injection and Service Locator are two opposing ways to program to interfaces, but Dependency Injection solves most problems and challenges far better than the Service Locator anti-pattern.

Abstract Factories can be injected into services that need them, so can be combined with both approaches. They are perpendicular to the discussion, not a variation of Service Locator.

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

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