ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Updated Registration doesn't affect located service #310

Open dansiegel opened 9 months ago

dansiegel commented 9 months ago

Description

Typically you would expect that the last registration for a given service would result in that implementation type being the type that will be resolved. This occurs given the following test:

container.Configure(c => c.Export(typeof(IServiceA)).As(typeof(ServiceA1)));
container.Configure(c => c.Export(typeof(IServiceA)).As(typeof(ServiceA2)));
var instance = container.Locate(typeof(IServiceA));
Assert.IsType<ServiceA2>(instance);

However if you first locate the type and then modify the registrations the initial registration is cached and the new registration does not take effect.

container.Configure(c => c.Export(typeof(IServiceA)).As(typeof(ServiceA1)));
var instance = container.Locate(typeof(IServiceA));
Assert.IsType<ServiceA1>(instance);

// Update the container
container.Configure(c => c.Export(typeof(IServiceA)).As(typeof(ServiceA2)));
instance = container.Locate(typeof(IServiceA));
Assert.IsType<ServiceA2>(instance); // Fails because it resolved ServiceA1