Swinject: [Question] How to create an objectscope similar to `graph`?

Created on 27 Jun 2017  路  9Comments  路  Source: Swinject/Swinject

Hello,

I'm kind of starting with Dependency Injection and I have some questions about scopes and was wondering how one could create an objectscope that behaves similar to .graph.

When I look at how the graph objectscope is created I see that this is done with the PermanentStorage, the same as the container scope. This feels conflicting with what is said in the docs about the Graph objectscope. However, digging further in the Container's code, I found a snippet:

private func decrementResolutionDepth() {
        assert(resolutionDepth > 0, "The depth cannot be negative.")

        resolutionDepth -= 1
        if resolutionDepth == 0 {
            resetObjectScope(.graph)
        }
    }

_If_ I'm right, the above code is what makes the scope behave like stated in the docs right?
I'm wondering how I can recreate such a scope as a custom scope.


Another question though;
I'm using the scopes to 'group' certain registrations which I can then reset when needed. Is this a viable method to use or is creating multiple containers a more wise decision?

Much appreciated already!

question

Most helpful comment

Well, it depends on what you want to achieve by "resetting" the container. Assigning it a new container will also get rid of all the registrations, i.e. you will need to call all the register methods again.

All 9 comments

Hi @geraldeersteling

Your understanding of .graph object scope is correct - currently it is treated as a "special" scope and it's behaviour cannot be recreated via custom object scope. ObjectScopes are meant to separate instance persistency management from Container - however, .graph's behaviour is intrinsically tied to Container and so unfortunatelly we weren't able to avoid this "special" treatment.

As for your second question, one of the goals of providing option to create custom object scopes was exactly this - being able to separate objects in .container scope to groups which can be independently reset. However, I'm not sure if this feature has been used in nontrivial projects.
Personally, for this purpose I'm using custom mechanism build on top of Swinject , as my apps have much larger requirements (e.g. architectural layer abstractions, notifications for creation / destruction of custom scopes).

Hope this helps 馃槈

Hi @jakubvano,

I see, thanks for pointing it out!
Using your information; I think I'm going to use separate containers to accomplish (grouping) what I want, leaving the 'resetScope' function only for singletons in those containers.

Using the above course; how should you 'reset' the containers?
Let me cook up a context for that question:

class Environment  {
    let container: Container
}

class SubEnvironment1 {
    let container: Container
    init(_ parentContainer: Container) {
        container = Container(parent: parentContainer)
    }
}

class SubEnvironment2 {
    var container: Container
    init(_ parentContainer: Container) {
        container = Container(parent: parentContainer)
    }
}

With the above context; is resetting SubEnvironment2's container as simple as assigning it a new container (i.e. container = Container())?

Well, it depends on what you want to achieve by "resetting" the container. Assigning it a new container will also get rid of all the registrations, i.e. you will need to call all the register methods again.

Hm, after reading your answer and my own question I think I know the answer(s) to my problems.
Grouping as I meant it has nothing to do with resetting scopes. I'm starting to see what I want now 馃槃

Thanks for helping!

Glad to have helped 馃槈
You also might want to check-out Assembler and Assemblies which are supposed to provide some sort of grouping, maybe even the kind you need 馃槃

Yeah, I did check the Assembler and Assemblies. But I was not sure about two things regarding those:

  1. I'm worried about thread safety as the Assembler doesn't provide a synchronise or similar method
  2. I didn't see any way of resetting the object scope on assemblers.

Nevertheless I used the assemblers by initialising them with a container which I hold the reference to.

@geraldeersteling, do you still have the issue?

I'm currently not working on the project regarding this issue; will be in 3-4 months.
I might have questions about thread safety on Assemblies (in my last response) when I come back to work on the project again.
That being said, reading back on the answers here I think my issue is solved though.

Ok, so I'm going to close this issue because the original issue was resolved. If you need a new question, please open a new Issue or ask at stackoverflow馃憤

Was this page helpful?
0 / 5 - 0 ratings