Search code examples
openrasta

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.


Solution

  • 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