Massey
Massey

Reputation: 1125

Correct usage of lifetime scope of Autofac in web api 2

I am new to autofac. I am using it on my new web api 2 project. The following is my autofac config code called by the Global.asax's Application_Start() method. I am not sure whether my usage of InstancePerRequest() is correct or not. More importantly, is it even need to be used at all? Or, instead I should use other options such as InstancePerLifeTimeScopre() or InstancePerDependency()? Whether I use any of these lifetime scope options or not, during debug, they produce same results.

public class IocConfig
{
    //Autofac configuration
    public static void Configure()
    {
        ContainerBuilder builder = new ContainerBuilder();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<DeliveryCode>().As<IDeliveryCode>() 
         .InstancePerRequest();
        builder.RegisterType<DeliveryContext>().As<IDeliveryContext>()
         .InstancePerRequest();
        builder.RegisterType<DeliveryStrategy>().As<IDeliveryStrategy>() 
         .InstancePerRequest();
        IContainer container = builder.Build();
        AutofacWebApiDependencyResolver resolver = new 
            AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
    }
}

Upvotes: 0

Views: 1601

Answers (2)

Al Kepp
Al Kepp

Reputation: 5980

InstancePerDependency - you get a new object for each dependency. Objects are never shared among clients. I'd say use this only if needed, as it is slower and uses more memory than others. But it is very safe, because each dependency is a private object.

InstancePerRequest - you get a new object for one web api call. This is useful when you want to create a new object for each api call, but then to share it among all other objects during this particular api call.

InstancePerLifeTimeScope - this one is similar to previous, but more generic. You normally don't need this, just ignore. :-)

SingleInstance - this creates just a single instance globally, shared among different web api calls and everything. Useful for global data providers, stateless classes offering a "service" to others etc.

Note that the "per dependency" and "single instance" are the opposite of each other. And "per request" is something in between, tailored for web api. :-)

Also note that if you tried them and they seem to behave identically, it can be because your test is wrong. But also it can be truth - stateless services can be registered as any type and will work correctly. It is just less efficient if you use per-dependency in cases where other registration types are suitable. Similarly it is less efficient to use per-request in case where singleton is suitable. But this is all just optimization. On the other hand, if your class is stateful and you mistakenly register it as singleton when you actually need separate instances, your program will behave wrongly and that's bigger problem than insufficient optimization.

Upvotes: 1

devNull
devNull

Reputation: 4219

Determining the lifetime of your dependencies really depends on the needs of your application. For example, if you have a dependency that is used to retrieve constant values, it may be best suited as a singleton so that those values are only retrieved once throughout the lifetime of your application.

If you haven't already, I'd recommend checking out the Autofac documentation on "Controlling Scope and Lifetime". It provides a good overview of the differences between the types of instance scopes (i.e. InstancePerLifeTimeScope() or InstancePerDependency()) and how they behave.

Upvotes: 0

Related Questions