I’m implementing a finite-state machine, with each class representing a state. Each state knows which other states it can transition to, and this naturally leads to circular relationships. (See the State Design Pattern).
For this simplified example I’m creating two components, where the first component has a reference to the second component, and the second component has a reference to the first.
The problem is that the Windsor framework is correctly setting the references for the first created component, but not setting the references for the second:
Here are the two components:
// DefaultMouseHandler knows about NewLineMouseHandler
public class DefaultMouseHandler : MouseHandler
{
public DefaultMouseHandler()
{
}
public NewLineMouseHandler NewLineMouseHandler
{
get;
set;
}
internal override MouseHandler LeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
return this.NewLineMouseHandler;
}
}
// NewLineMouseHandler knows about DefaultMouseHandler
public class NewLineMouseHandler : MouseHandler
{
public NewLineMouseHandler()
{
}
public DefaultMouseHandler DefaultMouseHandler
{
get;
set;
}
internal override MouseHandler LeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
{
return this.DefaultMouseHandler;
}
}
I then register the components as so:
_windsorContainer.Register(Classes.FromThisAssembly()
.BasedOn<MouseHandler>()
);
But when I first attempt to create the DefaultMouseHandler:
- The
DefaultMouseHandleris constructed - The
NewLineMouseHandleris constructed - The
NewLineMouseHandleris set on theDefaultMouseHandler
But the DefaultMouseHandler is NOT set on the NewLineMouseHandler.
Can this be considered a defect in Castle Windsor?
What’s the best way to have the two components referencing each other, without either component being aware of the Windsor container?
The solution I’ve come up with is to use the TypedFactoryFacility.
This allows objects to be created as they are needed, so we don’t have the complications of them all being dependent upon one another at initialization time.