Does calling GetInstance on a Singleton lifetime mapping return the singleton or create a new instance?

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%?

Answers


So I tried this and found that each time I called GetInstance with ExplicitArguments, a new unique object was created. But each time I called GetInstance without any arguments, the singleton reference was returned.


Need Your Help

What makes Spring a great framework (features)?

spring spring-features

I'm new to web development and Java EE and I understand that Spring is a Java framework for those purposes but when I watch all those fancy videos I still don't get the advantages. More specificall...

Create WCF Service to be hosted differently

wcf iis-7

I have a WCF service which programmatically creates its endpoints rather than using a config file - I'm looking into this as our field engineers are prone to break XML easily and we may use different

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.