依赖注入陷阱 [英] Dependency Injection Gotchas

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

问题描述

有没有人对www的链接(S)的名单DI陷阱的一个好名单?
我一直在试图通过注入DI控制,在一个asp.net web表单应用程序,发现递归积聚了ViewState是丢失。
也将是有益的文章列表,开发人员需要考虑在应用中实施的IoC / DI德大步前要注意给陷阱。

Does anyone have a list of link(s) on the www for a good list of DI gotchas? I have been trying to inject controls using DI, in an asp.net webforms app and found that on recursive build up that ViewState is lost. Also would be helpful a list of articles where the developer needs to be aware to gotchas before taking teh big step in implementing IoC/DI in the app.

推荐答案

DI本身没有陷阱。以上precisely,它会像列举的总体规划的陷阱,像传递一个空引用,然后不检查它。

DI itself has no gotchas. Or more precisely, it would be like enumerating general programming gotchas, like passing a null reference and then not checking for it.

递归积聚的ViewState丢失

on recursive build up ViewState is lost

积累为某些容器所说的那样,通常是一个坏主意,并应尽可能避免。看看在<一个href=\"http://stw.castleproject.org/Windsor.FAQ.ashx#Why_cant_Windsor_resolve_concrete_types_without_registering_them_first_10\"相对=nofollow>反对积累一些很好的理由温莎常见问题解答。这适用于所有的IoC容器。

"Buildup" as some containers put it, is generally a bad idea and should be avoided whenever possible. Take a look at the Windsor FAQ for some good reasons against buildup. This applies to all IoC containers.

在特定的,你的情况似乎发生了什么事是累积的控件的LoadViewState发生后()(或页面的的LoadPageStateFromPersistenceMedium()),所以它呈现的视图状态无效。也许你的IoC容器提供了已经注意到了这一问题基本控制类和你忘了继承它。

In particular, in your case it seems that what's happening is that buildup is happening after the control's LoadViewState() (or the page's LoadPageStateFromPersistenceMedium()) so it renders the viewstate invalid. Maybe your IoC container provides a base control class that already takes care of this and you forgot to inherit it.

恕我直言,在这样做的WebForms DI是如此的hackish(例如,这非常的情况下),我preFER求助于服务位置(仅适用于WebForms的页面和控件!)。操作简单,并且不需要怪异的黑客。

IMHO, doing DI in WebForms is so hackish (this very case for example) that I prefer to resort to service location (only for WebForms pages and controls!). Simple, and no weird hacks required.

现在,的IoC + DI 可以的有一些陷阱,比如生命周期/范围的问题。 伊戈尔Brejc在这篇文章非常彻底涵盖了这一点。你必须记住这一点,尤其是在有很多组分的复杂/大的应用程序。

Now, IoC + DI can have some gotchas, for example lifecycle/scope issues. Igor Brejc covers this very thoroughly in this article. You have to keep this in mind particularly in complex/big applications with lots of components.

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

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