I have the following mapping in ObjectFactory.Initialize method:
expression.For<IFoo>().Singleton()
.Use<SomeFoo>()
.Ctor<string>("url").Is(<fetch from config>)
.Ctor<string>("username").Is(<fetch from config>)
This ensures that my classes that have declared IFoo as a dependency always get the same instance.
However there is a scenario in my application where I need to simulate how the SomeFoo object behaves for a specific user. I create the dependency using GetInstance like so:
var args = new ExplicitArguments();
args.SetArg("url", <dynamic url>);
args.SetArg("user", <logged in user>);
ObjectFactory.GetInstance<IFoo>(args);
I would hope that this call with specific arguments would construct a new object rather than return a reference to the singleton.
What happens in this case and why? What would it take for me to maintain the singleton for 90% of the cases but have a new instance created in the rare 10%?
So I tried this and found that each time I called
GetInstancewithExplicitArguments, a new unique object was created. But each time I calledGetInstancewithout any arguments, the singleton reference was returned.