z4kn4fein / stashbox

A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
https://z4kn4fein.github.io/stashbox
MIT License
141 stars 10 forks source link

Does Scope.AttachToParent only affect Dispose behaviour? #97

Closed schuettecarsten closed 3 years ago

schuettecarsten commented 3 years ago

Lets say there are several cache services that should be tracked in parent scope "A".

I use Hangfire to run several worker jobs. Hangfire is connected to Stashbox and creates a new "job" scope (B1, B2, B3...) for each job. That's fine. The job scopes are child scopes of my scope "A" and have AttachToParent set to "true".

If the job service classes use other services, these services are created and tracked in the job scope. That's all fine.

But the job services should now be able to use some cache services. So, if a service requests a cache service, Stashbox should look into scope A for that service - and if it creates a new cache instance, it should be automatically tracked in scope A. How to solve this?

z4kn4fein commented 3 years ago

The AttachToParent only adds the given scope to its parent's disposable list, which means all the attached "B" scopes will be disposed when "A" is being disposed. So it's only affecting the tracking of the child scope.

What you described is basically the main concept behind the named scopes. I prepared some tests to show its functionality. While I wrote those tests I found a bug in the named scope resolution so it'll work only with the latest pre-release package, please use that for testing.

Please tell me if this isn't what you are looking for or if you have any questions.

z4kn4fein commented 3 years ago

I'm closing this due to inactivity, please re-open or file a new one if you have more questions or suggestions.