Sebastian Bach
unread,Oct 15, 2011, 12:02:10 PM10/15/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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