Spring.NET 1.3.0 RC1 for .NET 2.0 API Reference

MethodInvokingFactoryObject Class

An IFactoryObject that returns a value that is the result of a static (Shared in Visual Basic) or instance method invocation.

For a list of all members of this type, see MethodInvokingFactoryObject Members .

System.Object
   MethodInvoker
      ArgumentConvertingMethodInvoker
         MethodInvokingFactoryObject

[Visual Basic]
<Serializable> _
Public Class MethodInvokingFactoryObject
    Inherits ArgumentConvertingMethodInvoker
    Implements IFactoryObject, IInitializingObject
[C#]
[Serializable]
public class MethodInvokingFactoryObject : ArgumentConvertingMethodInvoker, IFactoryObject, IInitializingObject

Thread Safety

Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe.

Remarks

Note that this class generally is expected to be used for accessing factory methods, and as such defaults to operating in singleton mode. The first request to GetObject by the owning object factory will cause a method invocation, the return value of which will be cached for all subsequent requests. The IsSingleton property may be set to false, to cause this factory to invoke the target method each time it is asked for an object.

A static (Shared in Visual Basic) target method may be specified by setting the TargetMethod property to a string representing the static (Shared in Visual Basic) method name, with TargetType specifying the Type that the static (Shared in Visual Basic) method is defined on. Alternatively, a target instance method may be specified, by setting the TargetObject property as the target object, and the TargetMethod property as the name of the method to call on that target object. Arguments for the method invocation may be specified by setting the Arguments property.

Another (esoteric) use case for this factory object is when one needs to call a method that doesn't return any value (for example, a static (Shared in Visual Basic) class method to force some sort of initialization to happen)... this use case is not supported by factory-methods, since a return value is needed to become the object.

Note    This class depends on the AfterPropertiesSet method being called after all properties have been set, as per the IInitializingObject contract. If you are using this class outside of a Spring.NET IoC container, you must call one of either AfterPropertiesSet or Prepare yourself to ready the object's internal state, or you will get a nasty NullReferenceException.

Example

The following example uses an instance of this class to call a static (Shared in Visual Basic) factory method...

  <object id="myObject" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetType" value="Whatever.MyClassFactory, MyAssembly" />
    <property name="TargetMethod" value="Instance" />
    <!-- the ordering of arguments is significant -->
    <property name="Arguments">
      <list>
        <value>1st</value>
        <value>2nd</value>
        <value>and 3rd arguments</value>
      </list>
    </property>
  </object>

The following example is similar to the preceding example; the only pertinent difference is the fact that a number of different objects are passed as arguments, demonstrating that not only simple value types are valid as elements of the argument list...

        
  <object id="myObject" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetType" value="Whatever.MyClassFactory, MyAssembly" />
    <property name="TargetMethod" value="Instance" />
    <!-- the ordering of arguments is significant -->
    <property name="Arguments">
      <list>
        <!-- a primitive type (a string) -->
        <value>1st</value>
        <!-- an inner object definition is passed as the second argument -->
        <object type="Whatever.SomeClass, MyAssembly" />
        <!-- a reference to another objects is passed as the third argument -->
        <ref object="someOtherObject" />
        <!-- another list is passed as the fourth argument -->
        <list>
          <value>http://www.springframework.net/</value>
        </list>
      </list>
    </property>
  </object>

Named parameters are also supported... this next example yields the same results as the preceding example (that did not use named arguments).

  <object id="myObject" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetObject">
      <object type="Whatever.MyClassFactory, MyAssembly" />
    </property>
    <property name="TargetMethod" value="Execute" />
    <!-- the ordering of named arguments is not significant -->
    <property name="NamedArguments">
      <dictionary>
        <entry key="argumentName">
          <value>1st</value>
        </entry>
        <entry key="finalArgumentName">
          <value>and 3rd arguments</value>
        </entry>
        <entry key="anotherArgumentName">
          <value>2nd</value>
        </entry>
      </dictionary>
    </property>
  </object>

Similarly, the following example uses an instance of this class to call an instance method...

  <object id="myMethodObject" type="Whatever.MyClassFactory, MyAssembly" />
  <object id="myObject" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetObject">
      <ref local="myMethodObject" />
    </property>
    <property name="TargetMethod" value="Execute" />
  </object>

The above example could also have been written using an anonymous inner object definition... if the object on which the method is to be invoked is not going to be used outside of the factory object definition, then this is the preferred idiom because it limits the scope of the object on which the method is to be invoked to the surrounding factory object.

  <object id="myObject" type="Spring.Objects.Factory.Config.MethodInvokingFactoryObject, Spring.Core">
    <property name="TargetObject">
      <object type="Whatever.MyClassFactory, MyAssembly" />
    </property>
    <property name="TargetMethod" value="Execute" />
  </object>

Requirements

Namespace: Spring.Objects.Factory.Config

Assembly: Spring.Core (in Spring.Core.dll)

See Also

MethodInvokingFactoryObject Members | Spring.Objects.Factory.Config Namespace | MethodInvoker | ArgumentConvertingMethodInvoker