c# - Check if component is resolved in outermost LifetimeScope -
i reworking existing codebase make better use of autofac container. situation i'm having lot of things used resolve components straight container in classic servicelocator
anti-pattern. i'm in process of introducing proper unit-of-work schemes using lifetimescope
.
the issue i'm facing component must resolved child lifetimescope
implementing idisposable
, must disposed. if they're resolved in root scope never happen.
is there way prevent components being resolved in root scope? crashing runtime ok this, since i'm going through these cases 1 one , introducing scopes needed. way can think of create little dummy component gets resolved once root lifetime scope , resolving in .instanceperlifetimescope()
, storing statically somewhere. when later component resolved i'll 1 of dummy components , see if it's same instance 1 lifes in root scope. it's bit clunky though, , there better way?
you try using 'per matching lifetime scope' registration:
containerbuilder.registertype<foo>() .as<ifoo>() .instancepermatchinglifetimescope("scope");
this way ifoo
can resolved when @ least 1 ancestor lifetime scope tagged lifetime scope , tag equals "scope"
. root lifetime scope not tagged, when try resolve ifoo
it, autofac throw exception.
see the autofac wiki more information.
Comments
Post a Comment