Re: new instance required when using InstancePerLifetimeScope not under a lifetime scope

494 views
Skip to first unread message

Travis Illig

unread,
Jan 30, 2013, 6:21:06 PM1/30/13
to aut...@googlegroups.com, wallac...@gmail.com
It returns the same instance because you're resolving the instance from the container. A container is an implicit lifetime scope.

If you want different instances IN different lifetime scopes, you have to start a new lifetime scope.

var builder = new ContainerBuilder();
builder.RegisterType<MyType>().InstancePerLifetimeScope();
var container = builder.Build();
using(var scope = container.BeginLifetimeScope())
{
  // Resolve from a SCOPE not the CONTAINER
  var obj = scope.Resolve<MyType>();
}

If you want a different object every time you ask for one, you can specify InstancePerDependency() or just leave off the lifetime specifier entirely.

builder.RegisterType<MyType>();
// same as
builder.RegisterType<MyType>().InstancePerDependency();

Then when you resolve, you get a new one every time, regardless of the scope.

-T

On Wednesday, January 30, 2013 2:50:40 PM UTC-8, wallac...@gmail.com wrote:

I'm migrating some things to InstancePerLifetimeScope, this is working great for things that I can migrate.

However consider this example:

















Resolve returns the same object for service1 and service2. 

Is there an option to return different objects when NOT under a lifetime scope?

Cheers and regards

Wallace

Travis Illig

unread,
Jan 30, 2013, 6:50:08 PM1/30/13
to aut...@googlegroups.com, wallac...@gmail.com
In a one-liner? No, that's not supported out-of-the-box. You've sort of asked for conflicting lifetime definitions for the same component. Knowing that the container is a lifetime scope, the requirement effectively reads:
  • In one lifetime scope I want the same instance returned but
  • In another lifetime scope I want new instances every time.
What you can potentially do is override the service registration at the time you create your child lifetime scope, like this:

var builder = new ContainerBuilder();
builder.RegisterType<MyDependency>();
var container = builder.Build();
// Out here, every time you ask for a new MyDependency
// you'll get a new instance.
using(var scope =
  container.BeginLifetimeScope(
    b => b.RegisterType<MyDependency>().InstancePerLifetimeScope()))
{
  // In here, you'll get the same one every time
  // and it won't be one that you've gotten outside
  // the scope.
}

So, technically it's possible, it's just not free.

-T

On Wednesday, January 30, 2013 3:26:29 PM UTC-8, wallac...@gmail.com wrote:
Travis, 
>It returns the same instance because you're resolving the instance from the container. A container is an implicit lifetime scope.

I'm familiar with how this works when using BeginLifetimeScope.

My question relates to when resolving the dependency when NOT under a lifetime scope. You've answered this by saying 'a container is an implicit lifetime scope' but I'm asking for a way around this:
That is, is it possible to have:

1) when T resolved under  BeginLifetimeScope autofac will return the SAME instance
2) when T resolved NOT under BeginLifetimeScope autofac will return a NEW instance.

?

Travis Illig

unread,
Jan 30, 2013, 8:20:59 PM1/30/13
to aut...@googlegroups.com, wallac...@gmail.com
What I mean is that there's no "out of the box automatic support" for it. You have to write additional code like this to make it happen.

On Wednesday, January 30, 2013 4:31:12 PM UTC-8, wallac...@gmail.com wrote:
thats really cool, thanks!
and just to clarify when you say 'its just not free' <-- you are referring to the overhead of the ContainerBuilder on this line:
>using (var scope = container.BeginLifetimeScope(b => b.RegisterType<DateTimeService>().InstancePerLifetimeScope()))

?


Reply all
Reply to author
Forward
0 new messages