Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Autofac ContainerBuilder.Build an expensive operation?

I'm starting to use Autofac and I can't seem to find an answer to this question.

Also, when should I call ContainerBuilder.Build() ?

After I call the ContainerBuilder.Build() is it possible to register another type or instance?

like image 561
rguerreiro Avatar asked Dec 20 '10 15:12

rguerreiro


People also ask

Why do we need Autofac?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.

Is Autofac A IoC?

Autofac is an IoC container for Microsoft . NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity.


2 Answers

I can't tell you whether or not the Build method is expensive or not, but if you follow the Register Resolve Release pattern it doesn't matter because you should only have a single container instance per application.

You need to invoke the Build method once to get a container instance, so no matter how expensive (or not) it is, this is a cost you must pay. However, when you only use a single instance of the container, you only pay that cost once.

like image 135
Mark Seemann Avatar answered Sep 22 '22 09:09

Mark Seemann


ContainerBuilder.Build() should generally be called during application startup before you actually start invoking business behavior.

If you need to register additional components into an existing container you can. To do this in Autofac v2.2 (or later), you can create another ContainerBuilder instancer and use the ContainerBuilder.Build(IContainer) overload method.

like image 32
Jonathan Oliver Avatar answered Sep 25 '22 09:09

Jonathan Oliver