Nested dependencies with different lifetime scopes

62 views
Skip to first unread message

Jeremy Lew

unread,
Nov 3, 2011, 9:25:19 AM11/3/11
to aut...@googlegroups.com
I have chased down bugs a few times which have resulted from component registered as SingleInstance that has a dependency registered as InstancePerLifetimeScope.  The solution, of course, was to change the outer component's registration to InstancePerLifetimeScope as well.  However I was wondering whether it would be reasonable for the container to refuse to inject a dependency whose lifetime scope if less than the component being injected?  I can't think of a scenario where I'd want this to succeed.

Nicholas Blumhardt

unread,
Nov 3, 2011, 11:08:42 PM11/3/11
to aut...@googlegroups.com
Hi Jeremy, admittedly they're rare, but valid cases do exist.

If you're able to, using InstancePerHttpRequest() or a similar specific sharing model (rather than IPLS(), which is very flexible) will turn up these kinds of bugs. If a singleton references an IPHR() component, Autofac will throw an (almost informative) exception.

Cheers,
Nick

On 3 November 2011 06:25, Jeremy Lew <jerem...@gmail.com> wrote:
I have chased down bugs a few times which have resulted from component registered as SingleInstance that has a dependency registered as InstancePerLifetimeScope.  The solution, of course, was to change the outer component's registration to InstancePerLifetimeScope as well.  However I was wondering whether it would be reasonable for the container to refuse to inject a dependency whose lifetime scope if less than the component being injected?  I can't think of a scenario where I'd want this to succeed.

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/PQ4KhfybCvgJ.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.

Jeremy Lew

unread,
Nov 4, 2011, 9:21:04 AM11/4/11
to aut...@googlegroups.com
Thanks, didn't know about InstancePerHttpRequest(). A related question:  If I do have the situation described above, and I declare the outer component's constructor using the factory pattern like this:

public MySingletonComponent(Func<MyInstancePerLifetimeScopeComponent>  factory)
{
  _factory = factory;
}

...at the point that I actually call the factory, will it obey the lifetime scope semantics for whatever lifetime scope happens to be in play?  (Hoping the answer is yes.)

Jeremy

Jeremy Lew

unread,
Nov 4, 2011, 9:27:05 AM11/4/11
to aut...@googlegroups.com
Actually, thinking about this more, I don't see how Func could possibly behave like that, it must be tied to the lifetime scope that injected the Func, right?

Nicholas Blumhardt

unread,
Nov 4, 2011, 9:35:21 AM11/4/11
to aut...@googlegroups.com
Not exactly sure what behaviour you mean - correct though, a Func is tied to and produces results in the lifetime scope that injected it.

On 4 November 2011 06:27, Jeremy Lew <jerem...@gmail.com> wrote:
Actually, thinking about this more, I don't see how Func could possibly behave like that, it must be tied to the lifetime scope that injected the Func, right?

--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/y3iRujgSa3AJ.

Jeremy Lew

unread,
Nov 4, 2011, 11:52:04 AM11/4/11
to aut...@googlegroups.com
Okay, thanks  I think Owned<> will solve my use-case this time.
Reply all
Reply to author
Forward
0 new messages