ipjohnson / Grace

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

How to reconfigure a service registration #143

Closed xp-development closed 6 years ago

xp-development commented 6 years ago

Hi. It is possible to replace a binding in a second registration block?

The following test is green, but ClearExports don't remove the export. I try to rebind (like Ninject) a service registration.

[Fact]
public void ReplaceExport_InSecondRegistrationBlock()
{
  var container = new DependencyInjectionContainer();

  container.Configure(c =>
  {
    c.Export<MultipleService1>().As<IMultipleService>();
  });

  container.Configure(c =>
  {
    c.ClearExports(export => export.ExportAs.Contains(typeof(IMultipleService)));
    c.Export<MultipleService2>().As<IMultipleService>();
  });

  var multipleService = container.Locate<IMultipleService>();
  Assert.Equal(multipleService.GetType(), typeof(MultipleService2));
}
ipjohnson commented 6 years ago

Hi @jan-schubert clear exports only clears within that configure block. By default registering a new export will become the default export. So if you remove the ClearExport your test will pass.

That said by default Grace builds delegates at runtime to achieve it's performance. So once you've resolved a type once the delegate is built and the dependencies are set. If you need to change dependencies after the first time a type is resolved you need to mark the dependency as IsDynamic (DynamicTests.cs for some examples)

xp-development commented 6 years ago

Great!