Sharing instances and nested LifetimeScopes

67 views
Skip to first unread message

Sebastian Bach

unread,
Oct 15, 2011, 12:02:10 PM10/15/11
to Autofac
Hi Nicholas,

I'm using Autofac for a while and have a recurring constellation and
problem. In many applications (f.e. WCF) you have three levels of
(nested) scopes:
1. Application scope
2. Session scope
3. Unit of work scope (Request scope)

It's easy to share instances among the whole application scope by
defining a registration SingleInstance(). By creating an own
LifteTimeScope for every session (like in autofac WcfIntegration) it
is possible to share instances among a session by defining a
registration InstancePerLifetimeScope(). The problem arrises at the
unit of work scope. Every unit of work starts an own LifeTimeScope.
Every registration that is declared as InstancePerLifetimeScope will
be resolved as a new instance in each unit of work. Because of that
it's impossible to use inside the scope of a unit of work shared
instances from the session scope. They are all override'ed (what is
the correct past tense by the way?) Ok, to be fair: You can declare
registrations as InstancePerMatchingLifetimeScope(). But this doesn't
really solves the problem. First, I don't have a reference to the
current lifetime scope, because I'm using factories returning owned
instances (Func<Owned<IUnitOfWork>>). And in the WcfIntegration I
don't have access to my current lifetime scope so I can't tag it too.
On the other hand there could be more than one tag used on different
levels of lifetime scopes and you can't assign more than one tag to a
nested lifetime scope.

It may help to think of lifetime control and sharing instances in a
semantic of inheritance (in a tree structure):
SingleInstance means, that an instance is inherited to all nested
lifetime scopes from the root container. InstancePerLifetimeScope is
only applicable to leafs when it comes to sharing instances. What I
need is an inheritance of instances from an inner node of the lifetime
tree to all nested lifetime scopes. I need
InstanceInheritedPerLifetimeScope() or InstancePerLifetimeScope(bool
preserveParentInstance) or
InstancePerLifetimeScope().PreserveParentInstance(). :-)

Cheers
Sebastian

Sebastian Bach

unread,
Oct 21, 2011, 12:47:32 PM10/21/11
to Autofac
Hi,

I took a deeper look at the InstancePerLifetimeScope and obviously is
this what I was looking for. The description of this feature in the
wiki is to short and misleading. Dependencies (marked as
InstancePerLifetimeScope) in a tagged lifetime scope are shared with
their nested lifetime scopes. And the nested lifetime scopes don't
need to be tagged.
But in the WcfIntegration I've still the problem:
How can I tag the lifetime scope created inside the
AutofacInstanceContext? Maybe the method
AddDependencyInjectionBehavior could have a second parameter to
provide the tag?

Cheers,
Sebastian

Sebastian Bach

unread,
Oct 22, 2011, 5:05:32 AM10/22/11
to Autofac
I mean of course InstancePerMatchingLifetimeScope is what i was
looking for. Sorry for this typo. :)
Reply all
Reply to author
Forward
0 new messages