Spring.net and cyclic references in context

Recently I have implemented a simple Passive View as a POC for one of the projects my team is going to be working on, part of this implementation I wanted to use IoC and AoP (at least put the infrastructure for it).

To make it simple I will describe a simple scenario (class definition)

public interface IPresenter
{
IView View { set; }
}
public interface IView
{
IPresenter Presenter{ set; }
}
public class ConcretePresenter : IPresenter
{
private IView m_View;
public ConcretePresenter(IView view)
{
m_View = view;
}
public IView View
{
set { m_View = value; }
}
}
public class ConcreteView : Page, IView
{
private IPresenter m_Presenter;
public ConcreteView()
{
}
public IPresenter Presenter
{
set { m_Presenter = value; }
}
}


The thing I wanted to achieve using spring is to set the ConcretePresenter for ConcreteView.



<object id="Presenter"
type="ConcretePresenter, Demo" singleton="false">
<
property name="View" ref="View" />
</
object>
<
object id="View"
type="ConcreteView, Demo" singleton="false">
<
property name="Presenter" ref="Presenter" />
</
object>


But as said I got StackOverFlow exception on runtime because of the cyclic references (actually the object dependencies),



I find this very weird as I would expect that creation of an object and assignments of property would be two different actions (in object life cycle perspective).



I searched Google and found a note on the forum that talk about that specific issue of cyclic references (link to article), the solution is to use IObjectPostProcessor class and create an object that will run after the object was initialized and link the objects.



You can download the code at this link.



Hope this will save you some time.

Spring.net and cyclic references in context Spring.net and cyclic references in context Reviewed by Ran Davidovitz on 2:25 PM Rating: 5

2 comments:

Ryan Montgomery said...

It's a shame they don't have better support for MVC, MVP, or the Passive View pattern (which I prefer) built in. I noticed that they have some notion of this when using databinding with the InitModel, LoadModel, and SaveModel methods. There is even a property of the Spring.Web.Page class called controller, but I coulnd't find anything out about it. How have you fared with implementing the passive view with Spring?

Ran Davidovitz said...

Actually implementing the passive view is not directly constrained to SPRING, i only use spring for DI and AoP.

Regarding passive view creating we have some complex issues that require a controller to support multiple presenters but other than that it looks promising

The issue of cyclic references troubles me very much and i am currently working on the source code of spring to allow caching of object during creating context

Powered by Blogger.