Remember the cicle of life!

I thought – hey, let’s use the WindsorContainer deal with all my dependencies, that exist in my presenter-class. So I don’t have to worry about all the kinds of repositories and other stuff I might need. So I added my presenter:

_nestedContainer.AddComponent(
"SelectionPresenter",
typeof (SelectionPresenter));

But when I accessed my presenter from a ASP.Net webpart, registered some eventhandler on the presenter, which pointed back to the WebPart:

public class MyWebPart : WebPart
{
protected override void CreateChildControls()
{
var presenter = Container.Instance.Resolve<SelectionPresenter>();
presenter.OnItemSelected += HandleOnItemSelected;
}
[...]
}

Then I raised the event (in a postback!!) which sould be handled within the WebPart – boom! NullReferenceException while accessing the WebPart from the presenter!

After an enourmous amount of time I finally figured it out … the default behavior of the WindsorContainer is to provide singleton instances of objects. So in my case the presenter is being created the first time the WebPart is executed, and when the postback occurs the same instance of the presenter is being returned – this is wrong!

OK, so changing the component-registration did take care of this problem:

_nestedContainer.AddComponentWithLifestyle(
"SelectionPresenter",
typeof (SelectionPresenter),
LifestyleType.Transient);

Leave a Comment.