views:

80

answers:

1

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.

+3  A: 

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.

on recursive build up ViewState is lost

"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.

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.

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.

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.

Mauricio Scheffer
I have tried injection which calls Unity.BuildUp in Controls Init Eventhandler..The recursive loop for the subcontrols causes the View State to be lost. If the Injection is explicit by injecting a particular control then works fine...
chugh97
@chugh97: well, that's too hackish for my taste.
Mauricio Scheffer
difference/relationship between IoC and DI: http://www.betaversion.org/~stefano/linotype/news/38/
Mauricio Scheffer