views:

219

answers:

1

I have an object with two parameters that needs to be created via Spring.NET using the configuration file for decoupling.

public Object(string param1, string param2) { ... }

The two parameters are dynamically passed in based on user interaction where they pass in a username and password, so these values can't be hard coded to the configuration file. Therefore the following will not work:

<object name="WinFormApplicationWorkflow" type="COM.Us.Workflow.ApplicationWorkflow, "COM.Us.Workflow ">
<!-- this will NOT work -->
        <constructor-arg index="0" value="TESTUSER"></constructor-arg>
        <constructor-arg index="1" value="TESTPW"></constructor-arg>
<!-- / -->        
        <property name="NetworkWorkflow" ref="NetworkWorkflow" />
        <property name="ExceptionLogger" ref="ExceptionLogger" />
      </object>

How can I do this with Spring.NET, so that I can just do:

 ContextRegister.GetContext().GetObject("WinFormApplicationWorkflow");

But still pass in the two necessary parameters to my workflow class.

Thanks!

+1  A: 

You can use the overloaded method GetObject(string, object[]) of the Spring.Objects.Factory.IObjectFactory interface to pass in your dynamic values for object creation.

string userName = "Test";
string password = "Test";
object[] arguments = new object[] { userName, password };

ContextRegister.GetContext().GetObject("WinFormApplicationWorkflow", arguments);
Jehof
Now this is interesting. Docs say this method can return shared or independent instance. But I see no way it can be a singleton, because you may pass different constructor arguments. Does it mean you will always get a new object? Or you singleton will get reconfigured (sounds bad)? Or you will get separate instances per unique arguments?
Alexander Abramov
@Alexander : In my opinion, each GetObject() call will create a new instance of the specified type, calling the constructor that matches the given arguments.
Jehof