English 中文(简体)
采用名目表示
原标题:Questioning Dependency Injection through name-lookup

我认为,我对围绕“强盗服务”概念的争辩感到 struggl。

对开端人来说,国际奥委会是巨大的,对接口的方案拟订是前进的道路。 但是,我看不出这里使用的黄页模式,即是无汇编可调的的一部分。

申请代码将使用(Spring)集装箱从中提取物体。 既然如此,由于该法典只需要知道所需的接口(投向)、春季集装箱接口和所需物体的名称,因此,许多 coup合物被删除。

public void foo(){
   ((MyInterface) locator.get("the/object/I/need")).callMe();
}

当然,如果定位器能够配备所有种类的物体的刺>。” 目标衍生物。

但是,我感到迷惑的是,用姓名检索物体的灵活性实际上已经达到<><>>hides。 a. 以“安全、看管-安全”方式的依赖:如果我的汇编者用来检查所申请的反对成员的存在,则其类型现在都推迟到操作阶段。

我认为,最简单、从功能上讲,模式是巨大的,应用范围很广的<条码>。 物体:

public class YellowPages  {
    public MyInterface the_object_i_need;
    public YourInterface the_object_you_need;
    ....
}

// context population (no xml... is that bad?)
YellowPages locator = new YellowPages();
locator.the_object_i_need=new MyImpl("xyx",true),
locator.the_object_you_need=new YourImpl(1,2,3)


public void foo(){
   locator.the_object_i_need.callMe(); // type-safe, lookup-safe
}

Would there be a way/pattern/framework to ask the compiler to resolve the requested object, and check whether it s type is ok? Are there DI frameworks that also do that?

很多人

最佳回答

你们所描述的是反家长、便衣和简单。 依赖性注射(如有可能通过建筑商注射)是转用控制的最佳方式。 关于国际奥委会的较老的文章谈到,服务地点是实施这一模式的可行办法,但很难在最近著作中找到任何鼓吹这一点的人。

你用这一发言击中了那位 n:

But I m a bit puzzled by the fact that the flexibility of retrieving an object by name actually hides the dependency in a type-unsafe, lookup-unsafe manner:

多数现代的DI框架可以围绕“无安全”部分进行,但它们确实掩盖了依赖性,因为它暗示而不是明确。

Good article on this topic:

Service Locator is an Anti-Pattern

问题回答

春天是我所利用的唯一的DI框架,因此,我可以向他人说话,但即使春天使你能够以自己的名义要求提出反对,你也不要打上<>。 页: 1

Spring/DI背后的总体原则是,您的物体必须到 king,用于某一特定物体——它只应拥有国际奥委会集装箱向其提交的任何物体。 这种区别是微妙的,但会发生。 前一种做法与你所遵循的守则相似:

public void foo(){
   ((MyInterface) locator.get("the/object/I/need")).callMe();
}

相比之下,后一种做法并不取决于任何服务地点,也不取决于它想要的物体的“名称”:

private ObjectINeed objectINeed;

public void setObjectINeed(ObjectINeed objectINeed) {
    this.objectINeed = objectINeed;
}

public void foo(){
   objectINeed.callMe();
}

It s the IOC container that calls the setObjectINeed() method. DI is definitely tackles the same problem as the service locator pattern, but it goes that extra step towards getting rid of the dependency on your service locator class.





相关问题
Choosing the right subclass to instantiate programmatically

Ok, the context is some serialization / deserialization code that will parse a byte stream into an object representation that s easier to work with (and vice-versa). Here s a simplified example ...

Design pattern for managing queues and stacks?

Is there a design pattern for managing a queue or a stack? For example, we are looking to manage a list of tasks. These tasks will be added to a group queue, users will then be able to pull off the ...

Organizing classes using the repository design pattern

I have started upgrading one of our internal software applications, written in ASP.NET Web Forms, and moving to ASP.NET MVC. I am trying to leverage the Repository design pattern for my classes, ...

Misuse of Observer Pattern?

I have a Car object which contains a latitude field and a longitude field. I use the observer pattern so that any time either of these fields change in my application, my car object is notified. I ...

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I m in the process of putting together a simple login module and I m unsure where to put the "business logic." If I put the logic with the data in the ...

热门标签