forked from aurelia/dependency-injection
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added extra check for registration _key
- Loading branch information
Showing
1 changed file
with
1 addition
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this, a better fix (so we don't rely on an internal property) is to re-structure the code to only register the dependency as itself if there is no registration on the parent but return it if there is. Something on the lines of:
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Magrangs thanks for the Q on SO and this gist. Let's loop @fkleuver and @EisenbergEffect in
SO Q: here https://stackoverflow.com/questions/59968454/aurelia-testing-components-with-transient-dependencies-never-uses-a-mock
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit nervous about changing this behavior in the core DI. My recommendation would be to remove the registration decorator from the class itself and instead add a registration as part of your app startup code. Then I believe your test should work fine.
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does however point to a bug in the code somewhere as the code should work with the transient decorator on the class. Plus it wont help other developers going forward as it won't be obvious you'd need to do that.
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difficulty is that if we change this, we could break a lot of apps potentially, since it has worked this way for five years. Since this is the only occurrence we have of an issue, and it's isolated to the testing scenario, I'd prefer to go with the workaround for now. We can re-evaluate if this comes up repeatedly and we can certainly address this in Aurelia 2 directly.
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK no problem. I'll try it out on that github repo posted in the SO question and let you know. So basically, remove the transient decorator from the class and in app start, register the class as transient?
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly 😄 I should mention that this is my personal preferred way to do registration actually. I like to have registration centralized as part of the app-level configuration, so that different registrations can be used in different scenarios.
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EisenbergEffect @bigopon I can confirm that this change has worked. I have updated the issue repo here: https://github.com/Magrangs/aurelia-transient-dependency-issue. I shall update the question on SO and mark as resolved so that if any developers get the same issue they can find a work-around for it. Thanks.
56c7d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now seeing this, I can appreciate the benefits of seeing the registrations at composition root, it also, like you said, gives you the flexibility of changing the configuration in different circumstances. It is more akin to the approach you'd use in c# with a DI container.