Howto: Connect dynamically created views (non-singleton forms) to a singleton controller with Spring.NET config file?
I have a piece of code that create Windows Forms dynamically at run-time using the lookup-method tag approach in Spring.NET. Basically what I have is a factory class to create any number of Forms, see config file snippet below.
<object id="frmListView" type="GUI.View.ListView, MMM" singleton="false">
<property name="MdiParent" ref="frmMainForm" />
</object>
<object id="frmListViewController" type="Controller.View.ListView, MMM">
<listener event="Search" method="frmListView_Search">
<ref object="frmListView"/>
</listener>
</object>
<object id="frmListViewFactory" type="GUI.ListViewFactory, MMM">
<lookup-method name="createView" object="frmListView" />
</object>
The problem is that since the frmListView
is created dynamically it seems like the listener
-tag doesn't bind, i.e. the Search-event will not be bound to any event listener. Obviously I can solve this by doing the binding in code but I would like to know if there is any way to do it in the context file?
I find it a bit strange since the whole idea with the lookup-method
factory is to hav开发者_如何学Goe the created frmListView
instance Spring-aware and it is to some extent, i.e. the MdiParent
property is set as expected but apparently the listener reference in the singleton frmListViewController
is not resolved.
Any help on this issue would be greatly appreciated.
Regards, Ola
I don't know how to do this in an xml config, actually I'm not even sure if it is possible at all in xml. But I can explain why your approach doesn't work:
With this configuration, you create a singleton frmListViewController
, that subscribes to the Search
event from a frmListView
. This frmListView
is requested from the container and because it's a prototype (non singleton), a new ListView
instance is created.
Your frmListViewController
will only listen to Search
events from this ListView
instance, because, well, that's the way it's configured. The listener reference is resolved, but it resolves to a ListView
instance you didn't expect.
You'll see that a single ListView
instance is created once the container is initialized, as part of the creation of frmListViewFactory
. You can test this (for instance) by setting a breakpoint in the ListView
constructor and run a program similar to this:
internal class Program
{
private static void Main(string[] args)
{
IApplicationContext ctx = new XmlApplicationContext("objects.xml");
Console.WriteLine("Container initialized ... ");
Console.WriteLine("Enter to exit");
Console.ReadLine();
}
}
You'll observe that a new ListView
is created, without the frmListViewFactory
's CreateView
method being called.
BTW, the purpose of lookup method injection is not to make frmListView
and frmListViewFactory
aware of their container.
精彩评论