Evgeniy Berezovsky
Evgeniy Berezovsky

Reputation: 19248

Openrasta: Swap instance in dependency resolver

Suppose I register some instance in OpenRasta's dependency resolver using

resolver.AddDependencyInstance(IInterface, instance, DependencyLifetime.Singleton)

Now if I want to swap that instance later, say to reread fresh data from the db, is another call to resolver.AddDependencyInstance the right thing to do?

Checking the InternalDependencyResolver implementation, it seems to be fine. However I'm asking because the behavior is not defined (in openrasta's sources, where I checked), and the method prefix "Add" is suggestive of different behavior.

Upvotes: 1

Views: 168

Answers (1)

KlsLondon
KlsLondon

Reputation: 1280

I wouldn't use Singleton if you have to swap the instance at some point. Use DependencyLifetime.Transient and have a constructor injection in the class where you need the new instance

Upvotes: 1

Related Questions