rmoskal
06-11-2005, 04:42 PM
Greetings:
I'm enjoying my eperiments with spring, and have a question about how to incoporate an existing factory idiom I use in in some current apps.
I have factories that load many flavors of an object (for example to represent a plugin for a particular media type). I load them up from a configuration file and put the objects on a hashtable.
Each media type has additional info that is associated with it, besides the plugin. This is currently loaded into a bunch of helper objects accessible through another factory. All of it encoded in an XML file:
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="LibraryItemHelper">
<HasChildren>false</HasChildren>
<VisibleInUI>true</VisibleInUI>
<Id>ppt</Id>
<Description>Powerpoint Presentation</Description>
<PluginClass>MostMedia.Cwrapper.Plugins.PowerPointPlugin,CorePl ugins.dll</PluginClass>
</anyType>
.
.
.
</ArrayOfAnyType>
Curently, I'm loading both factories into the IApplicationContext in order to inject their dependencies and to take advantage of the quasi singleton pattern you offer.
However, in doing so the objects created by my factories aren't managed by Spring and so can't take advantage of features like the loosely bound event publishing mechanism. I could of course eliminate my factory and load each flavor of plugin and its helper object into the IApplicationContext and retrieve from there. But it seems that this is less elegant than my first approach since I'll have to have an entry for every single plugin and helper object in the spring configuration
<object name="PowerPointPlugin" type="MostMedia.Cwrapper.Plugins.PowerPointPlugin,CorePl ugins.dll">
<constructor-arg index="0">
<ref object="libraryFacade"/>
</constructor-arg>
<constructor-arg index="1">
<ref object="powerPointFacade"/>
</constructor-arg>
</object>
<object name="PowerPointPlugin" type="MostMedia.Cwrapper.Core.LibraryItemHelper,Cwrapper Core.dll">
<property name="HasChildren">false</property>
<property name="VisibleInUI">true</property>
<property name="Id">ppt</property>
<property name="Description">Powerpoint Presentation</property>
</object>
Say I have 8 media types, it seems much less elegant to have 16 entries that follow the above pattern, then to have my factory instantiate the objects using reflection and inject all the necessary dependencies:
if (!myContents.ContainsKey(item.ItemType))
{
string [] pluginInfo =Utilities.splitString(",",myHelper.PluginClass);
if (pluginInfo.Length != 2) throw new ConfigurationException(item.ItemType +
" PluginClass property requires a class and assembly name seperated by a comma");
ObjectHandle hdl = System.Activator.CreateInstanceFrom(pluginInfo[1]. Trim(),pluginInfo[0].Trim());
ICwrapperPlugin aPlugin = (ICwrapperPlugin)hdl.Unwrap();
aPlugin.Ppt = myPptFacade;
myContents.Add(item.ItemType,aPlugin);
}
return (ICwrapperPlugin) myContents[item.ItemType];
This IApplicationContext approach seems to have a number of other disadvantages. The info about a media type is now in two places in the spring config file instead of one place in mine. I'll need to provide a facade that can tanslate my media type id to the correct spring id (since I can't assign the same id to both the media object helper and the media object pluging e.g, "ppt"). Finally seems to me that I lose information on how my app hangs together, about dependencies that really matter.
I'm guessing that if my objects aren't managed by the spring framework, I won't be able to take advantage of things link AOP. Do I need to implement my own IFactoryObject. If I do will this play nicely with the objects in the IApplicationContext . For example can they use the messaging feature and AOP features?
Thanks,
I'm enjoying my eperiments with spring, and have a question about how to incoporate an existing factory idiom I use in in some current apps.
I have factories that load many flavors of an object (for example to represent a plugin for a particular media type). I load them up from a configuration file and put the objects on a hashtable.
Each media type has additional info that is associated with it, besides the plugin. This is currently loaded into a bunch of helper objects accessible through another factory. All of it encoded in an XML file:
<ArrayOfAnyType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<anyType xsi:type="LibraryItemHelper">
<HasChildren>false</HasChildren>
<VisibleInUI>true</VisibleInUI>
<Id>ppt</Id>
<Description>Powerpoint Presentation</Description>
<PluginClass>MostMedia.Cwrapper.Plugins.PowerPointPlugin,CorePl ugins.dll</PluginClass>
</anyType>
.
.
.
</ArrayOfAnyType>
Curently, I'm loading both factories into the IApplicationContext in order to inject their dependencies and to take advantage of the quasi singleton pattern you offer.
However, in doing so the objects created by my factories aren't managed by Spring and so can't take advantage of features like the loosely bound event publishing mechanism. I could of course eliminate my factory and load each flavor of plugin and its helper object into the IApplicationContext and retrieve from there. But it seems that this is less elegant than my first approach since I'll have to have an entry for every single plugin and helper object in the spring configuration
<object name="PowerPointPlugin" type="MostMedia.Cwrapper.Plugins.PowerPointPlugin,CorePl ugins.dll">
<constructor-arg index="0">
<ref object="libraryFacade"/>
</constructor-arg>
<constructor-arg index="1">
<ref object="powerPointFacade"/>
</constructor-arg>
</object>
<object name="PowerPointPlugin" type="MostMedia.Cwrapper.Core.LibraryItemHelper,Cwrapper Core.dll">
<property name="HasChildren">false</property>
<property name="VisibleInUI">true</property>
<property name="Id">ppt</property>
<property name="Description">Powerpoint Presentation</property>
</object>
Say I have 8 media types, it seems much less elegant to have 16 entries that follow the above pattern, then to have my factory instantiate the objects using reflection and inject all the necessary dependencies:
if (!myContents.ContainsKey(item.ItemType))
{
string [] pluginInfo =Utilities.splitString(",",myHelper.PluginClass);
if (pluginInfo.Length != 2) throw new ConfigurationException(item.ItemType +
" PluginClass property requires a class and assembly name seperated by a comma");
ObjectHandle hdl = System.Activator.CreateInstanceFrom(pluginInfo[1]. Trim(),pluginInfo[0].Trim());
ICwrapperPlugin aPlugin = (ICwrapperPlugin)hdl.Unwrap();
aPlugin.Ppt = myPptFacade;
myContents.Add(item.ItemType,aPlugin);
}
return (ICwrapperPlugin) myContents[item.ItemType];
This IApplicationContext approach seems to have a number of other disadvantages. The info about a media type is now in two places in the spring config file instead of one place in mine. I'll need to provide a facade that can tanslate my media type id to the correct spring id (since I can't assign the same id to both the media object helper and the media object pluging e.g, "ppt"). Finally seems to me that I lose information on how my app hangs together, about dependencies that really matter.
I'm guessing that if my objects aren't managed by the spring framework, I won't be able to take advantage of things link AOP. Do I need to implement my own IFactoryObject. If I do will this play nicely with the objects in the IApplicationContext . For example can they use the messaging feature and AOP features?
Thanks,