Documentation icon indicating copy to clipboard operation
Documentation copied to clipboard

Add FAQ based on immutable container challenges

Open tillig opened this issue 6 years ago • 6 comments

With Autofac 5 we removed ContainerBuilder.Update and containers are now immutable.

We should add an FAQ based on this discussion issue to help people understand alternatives to updating the container proper.

Also, we should make sure there aren't anymore doc references to ContainerBuilder.Update.

tillig avatar Jan 27 '20 20:01 tillig

This may tie into #99 if the Update() removal is the root issue there?

natrexjacobs avatar Mar 03 '20 02:03 natrexjacobs

This issue is more specifically to write a new article to explain how to work with immutable containers. I'll leave the other issue open so this one can be separately tracked.

tillig avatar Mar 03 '20 02:03 tillig

How can I add a registration to the global scope after build?

StefanoVuerich avatar May 15 '20 07:05 StefanoVuerich

@StefanoVuerich; typically questions like that should be asked on Stack Overflow, rather than added as comments on existing issues.

That being said, the short answer is that, after Autofac 5.0, you cannot modify the global scope after it has been built. Child lifetime scopes can continue to have custom registrations within that scope. See https://autofaccn.readthedocs.io/en/latest/best-practices/#consider-a-container-as-immutable for some more detail.

alistairjevans avatar May 15 '20 08:05 alistairjevans

@alistairjevans After googling a bit found that PreserveExistingDefaults works. It it really the best solution to force people add a kind of "place holder object", that can be later on overrided? Wouldn't be simpler to let the container have the possibility to add a new registration?

StefanoVuerich avatar May 15 '20 08:05 StefanoVuerich

I'd recommend reading the content at the beginning of this thread for an explanation as to why it isn't simpler to allow a container update.

Placeholder objects aren't your only option; you can use delegate registrations, or organise your child scopes so you can change the dependencies per-scope.

alistairjevans avatar May 15 '20 08:05 alistairjevans