Nancy.Bootstrappers.Autofac icon indicating copy to clipboard operation
Nancy.Bootstrappers.Autofac copied to clipboard

Update method on Autofac builder is obsolete

Open dario-l opened this issue 8 years ago • 6 comments

Autofac documentation says:

While Autofac provides an Update() method to update registrations in an existing container, for the most part it’s there for backwards-compatibility with Autofac 2.x. Where at all possible, you should avoid updating a container and instead register everything up front before building the container.

http://docs.autofac.org/en/latest/best-practices/index.html#consider-a-container-as-immutable

And they make Update obsolete:

Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.

This leads to major modifications in this bootstrapper.

dario-l avatar Dec 08 '16 11:12 dario-l

Yes. The current design was based around the assumption that the containers could be modified after the initial registrations. This has also caused trouble with containers like SimpleInjector, which simply doesn't allow you to do this.

We've been prototyping a bootstrapper design which doesn't have these assumptions. It collects all registrations up-front and relies on scoping and/or nested containers/lifetimes to provide request-scoped dependencies. Hopefully we'll be able to get that rolled into Nancy at some point in the future.

khellang avatar Dec 08 '16 13:12 khellang

As I've said before though, that prototype doesn't cover all the use cases we cover. There's a key difference between something being immutable, and its API not allowing you to "change" it. Strings are immutable in .net, but that doesn't ,mean you can't do "mystring = mystring + otherstring", it just returns a new string instance - this is exactly how I'd expect containers to work that require internal immutability for performance.

grumpydev avatar Dec 08 '16 14:12 grumpydev

@grumpydev Are you trying to say that we can't cover these use cases by having an immutable container?

khellang avatar Dec 08 '16 16:12 khellang

@khellang

Hopefully we'll be able to get that rolled into Nancy at some point in the future.

I'll commit to work with you on this asap if you're up for it :P

thecodejunkie avatar Dec 08 '16 16:12 thecodejunkie

I'll commit to work with you on this asap if you're up for it :P

Yes! New year, new bootstrapper :grin:

khellang avatar Dec 08 '16 16:12 khellang

Is this project dead?

Did nancy-bootstrapper-prototype get parked?

Edit: In the meantime here's a hack to allow moderately painless usage of the code from this repo with autfoac 4.9.x

Warning this has had all of 5 minutes testing

#pragma warning disable CS0618 // Type or member is obsolete (I KNOWWWWW!)
#pragma warning disable SA1402 // File may only contain a single type
public static class AutofacExtensions
{
    public static IContainer Update(this IContainer container, Action<ContainerBuilder> builderAction)
    {
        var builder = new ContainerBuilder();
        builderAction(builder);
        builder.Update(container);
        return container;
    }
}
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore SA1402 // File may only contain a single type

`

p-m-j avatar Oct 02 '19 13:10 p-m-j