PDA

View Full Version : Object Instantiation based on property values


mr_prabhu
11-22-2006, 06:07 AM
Hi,

I am presently working on a web service implemented using Spring.NET.
I am new to Spring.NET. I have a scenario where we need to instantiate an object based on a property value provided by client applications and set it to another property.

I have an asmx file with one exposed method called execute
It accepts a serializable business object called DataInput as an input parameter.
DataInput looks similar to this
[Serializable]
public class DataInput
{
private int intID;
private objType objtype; // objType is an enumeration ObjectA =1, ObjectB =2

public int ID
{
get{return intID;}
set{intID=value;}
}

public objType ObjectType
{
get{return objtype;}
set{objtype= value;}
}
}

I have 2 classes ObjectA and ObjectB, both derived from IMyobject
Based on the value provided in the ObjectType property I need to instantiate the appropriate class ObjectA or ObjectB and assign it to a property which excepts a value of IMyObject type.

Please let me know if you have come across such a scenario and how we can go about configuring this.


Thanks,
Rajesh

Mark Pollack
11-26-2006, 03:50 AM
Hi Rajesh,

This seems to be a common scenario when using something like the command pattern. Take a look at this other forum posting (http://forum.springframework.net/showthread.php?t=456) to see if the solution presented there works for you. In particular read the referenced blog entry (http://www.pyrasun.com/mike/mt/archives/2004/11/07/12.58.49/index.html), it goes into pretty good detail regarding the problem and a solution in Spring.Java (though it doesn't take into account creating a 'prototype' - aka non-singleton object with parameters like the spring.net forum posting solution.

If I understood your case correctly, it looks like you would need to do something like this...

public ProcessingObject GetProcessingObject(ObjectType objectType)
if (objectType == ObjectA)
{

return factory.GetObject("processingObject", new ObjectA() );
}

you could nest it like this if you didn't want to new ObjectA for some reason (if it has complex dependencies).

object arg = factory.GetObject("objectA")
return factory.GetObject("processingObject", arg);

Note there is also the ConfigureObject method...that may be of use to you if you already instantiated the object yourself and would like to have spring wire up the rest of the dependencies.

In short, you need to have some dependency on the IoC container with the approach I'm talking about. In the blog entry there was a special factory object created for this scenario that implemented an abstract finder method on a factory class...that would help remove the dependency.

Mark

mr_prabhu
11-27-2006, 05:49 AM
Hi Mark,
I have configured the 2 objects ObjectA and ObjectB in the objects.xml.
I think I will use the AbstractObjectFactory.GetObject method to access the object instances. Was looking more at a purely XML configuration solution.

Thanks
Rajesh

Mark Pollack
11-27-2006, 04:41 PM
Hi Rajesh,

You may also be interested to look at this AOP based solution (http://forum.springframework.net/showthread.php?t=694)contributed by Bruno. There was a fair amount of discussion among the team on this point - generalized solutions for this dynamic behavior are too brittle, that is you can't really seem encapsulate all the possible dynamic behavior into a configuration based approach. That doesn't mean you can't adopt a specifc config approach for your case - in particular with the use of the expressions parser.

We plan to add a section to the documentation that covers this usage pattern and will be putting the GetObject(string name, object[] args) into the top level interfaces. Also we will add a version of GetObject that takes ctor name/value pairs.

Mark

Bruno Baia
11-27-2006, 10:39 PM
Hi,

As Aleks proposed, you can also make your object implements Spring.Context.IApplicationContextAware but it makes your code tied to Spring...


Bruno