New scopes: TyphoonScopeWeakSingleton & TyphoonScopeObjectGraph
[Migrated discussion from closed ticket here]
Currently we have two scopes: _TyphoonScopeSingleton_ and _TyphoonScopePrototype_. However, we've outlined four scopes that can be useful:
_TyphoonScopePrototype_
Always return a new instance.
_TyphoonScopeObjectGraph_
Similar to prototype. However, within the scope of a request, treat any references to a component with this scope as shared. . . Example: if component A depends on component B, and component C depends on component B - both A and C will receive a shared instance. . . This is useful for eg view controllers with a view and delegate.
This scope would not be thread-safe.
_TyphoonScopeSingleton_
Typhoon retains this and it remains in effect as long as the container is alive.
_TyphoonScopeWeakSingleton_
This is the same as singleton, but if nothing is retaining, it will go away. When asked for, it will come back again.
Major Issues/Questions
- As it stands now, TyphoonScopePrototype is behaving like TyphoonScopeObjectGraph . . . How/when/why/who will fix this?
- I think it would be useful to have both the new scopes. We need at least one of the two (or at least both @cesteban and I have found this). Should we introduce both or just one?
Difference between TyphoonScopeObjectGraph and TyphoonScopeWeakSingleton
- Say you set up a dependency chain that includes a view controller, with a view. And the view has a delegate pointing back to the controller. (Circular dependency), as well as another component (A validator) that depends on the view. . . . this whole object graph needs to be released after use.
- With the request scope, each request to the container will return a new graph.
- With the weak singleton scope, a subsequent request to the container returns the shared view from the first request.
Thoughts @cesteban, @rhgills @eriksundin
Folks we're getting more and more users so the longer we leave this the more impact its going to have. What I'd like to do is the following:
- Make TyphoonScopePrototype behave how it did initially.
- Add the new scope TyphoonScopeResolve that behaves as per @cesteban 's fix. This is a really useful feature, and will be for immediate implementation.
- No change to TyphoonScopeSingleton
- Put TyphoonScopeWeakSingleton in the backlog for implementation later.
Any objections @rhgills @cesteban @eriksundin ?
bump.
@cesteban ? Would you be happy with this? This would be the default scope. So:
- If you're setting the scope explicitly you'd have to change to TyphoonScopeRequest
- Otherwise no change.
Any objections to having this as the default scope?
Sounds good. Preserve the existing behavior (still as a default) as TyphoonScopeRequest (which I assume is what you mean by TyphoonScopeResolve). We can change prototype to disallow circular dependencies and add weak singleton later.
Which name do you like better?
- TyphoonScopeRequest
- TyphoonScopeResolve
I want to rename TyphoonScopePrototype immediately, since we're now in an inconsistent state. . And then hopefully this weekend, I'll be able to put plain-old-prototypes back again, for any folks who've come to rely on it.
Sorry for late reply, I've being trying to cope with so much work the past few weeks.
I'm probably missing something, but I don't see how the stash approach of my fix is disrupting the expected behavior of prototype scope. The stashed instance is only used when a cycle is detected. Yes, in that case you don't build a new instance, but that is the expected behavior, even for a pure prototype.
I mean, if you have A that depends on B, and vice versa, being both prototypes. Then, you need to build two and only two instances of A and B, and make the arrows point one to each other: A <--> B. I honestly think that is the expected behavior for prototypes (it wouldn't make sense to build 4 instances, that would be an error), and that is exactly what my fix does, unless there is some bug that I'm not seeing right now.
Furthermore, the stashed approach allows different cycles to have their own instance of the prototypes. So it not an instance per request (which I believe is what the TyphoonScopeRequest name suggests), but an instance per cycle, which is --again-- the expected behavior for pure prototypes.
Please, provide an example in which the stash approach violates the prototype scope by injecting the same instance to different components (but not involved in the same cycle! because in that case, that is exactly what you expect, right?).
I'm sorry if I'm slowing you guys down, but I really don't see the problem.
BTW, if you want to rename the prototype scope, please, don't use TyphoonScopeRequest, IMHO that name suggest you are only creating one instance per componentForType invocation, which is not even close to what is actually happening.
@cesteban
Thanks for the clarification. It seems that I incorrectly thought that your recent commits were moving towards the new scope that I suggested.
In actual fact they were about circular dependencies with TyphoonScopePrototype - as its currently defined. Ie always return a new instance.
In that case, I'm still concerned, because other DI containers (eg Spring) disallow circular dependencies with this scope. It can lead to surprising/unexpected results when dealing with a complex object graph.
However, if you really need this (and not the new proposed 'Resolve'^ scope) then we can look at firing a warning instead of an exception.
^ Still need to find the best name for the proposed new scope. These are the traits of the scope:
- Any relationships between components within the context of building an instance (ie 'componentForKey') will return a shared instance.
- These components are not retained by Typhoon. So you can build an object graph, with some circular dependencies and weak references. This whole object graph will be dealloc'd after it has been used.
Any suggestions for a name that sums this up in one or two words would be appreciated. It will not be request!! ;)
How about
- TyphoonScopeConstruct
- TyphoonScopeObjectGraph
I work every day with Typhoon and very complex object graphs, and it really does a pretty good job. If there were any unexpected results managing prototypes and circular dependencies, I would already noticed. If you feel we should be firing a warning, go for it, but I don't see any reason to throw an exception for a functionality that is already there and working fine...
Regarding the new scope... Perhaps we could think out the box and let the user define custom scopes. That would solve your problem, but in a much more generic way, right?
Do you think this would be possible? I believe it would be awesome to have that kind of functionality.
I still like the idea of the 'ObjectGraphScope' -
(This is the scope that I suggested (and stopped) calling 'RequestScope'. . I think 'ObjectGraph' is a better name. . .)
To summarize again: Any component within an object graph having this scope will be a "shared prototype". . .I think this would be fairly easy to implement. . . You know the (contrived) example I posted the other day with the:
- Controller
- View with controller as delegate.
- Validator with view as property
And the view because its referenced in both controller and validator is shared. It will not be retained by Typhoon, so it all goes away, when you're done with the whole graph - the benefits of DI, but not using up loads of memory.
Certainly I'd be happy to look at custom scopes. But how would this work? What's the best way to communicate what you mean here? Would you like to write something up? Have a skype call? Create an issue?
- ObjectGraphScope could be a much better name. If you think this functionality could be useful and easy to implement, we should go for it.
- Custom scopes means user can define an scope in some way:
Let's say: TyphoonScope *sessionScope = [TyphoonScope scopeWithKey:@"session"]; [[TyphoonComponentFactory defaultFactory] registerScope:sessionScope];
Then, you can set the scope of your definitions to sessionScope: [definition setScope:sessionScope];
Then, in objectForDefinition we detect custom scopes just as we do with singleton scope, and proceed in the same manner, only that instead of using the instances stored in _singletons, we use another cache local to the scope.
Then we would need some way to reset the scope. I mean, when the session expires, all the objects in the session cache must be cleaned and rebuilt. Not sure how to do this, sorry.
Makes sense? I'm just thinking in loud voice.
I'm not sure, but perhaps with this approach you can address your need for a ObjectGraphScope, but in a much general, abstract way. I mean, you create a TyphoonDetailsViewScope, which includes the view controller, the view and the validator. All objects are cached within that scope. Every time you generate a new group of controller, view, and validator, you reset the scope.
As I said, I'm thinking in loud voice, so please feel free to let me know if I'm saying nonsenses. But if I'm not, I believe this could be such a great feature for Typhoon.
Perhaps we can open a new ticket (issue), for this experimental functionality and discuss there. What do you think?
:+1:
ObjectGraph scope implemented.