Skip to content

Commit

Permalink
Added extra check for registration _key
Browse files Browse the repository at this point in the history
  • Loading branch information
mbx-mbx committed Jan 29, 2020
1 parent 191d7d6 commit 56c7d96
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ export class Container {

const registration = metadata.get(metadata.registration, key);

if (registration === undefined) {
if (registration === undefined || registration._key === undefined) {
return this.parent._get(key);
}

Expand Down

9 comments on commit 56c7d96

@mbx-mbx
Copy link
Owner Author

@mbx-mbx mbx-mbx commented on 56c7d96 Jan 31, 2020

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:

var parentRegistration = this.parent._get(key);
 if (!parentRegistration){
    return registration.registerResolver(this, key, key).get(this, key);              
} else {
    return parentRegistration ;
}

@bigopon
Copy link

@bigopon bigopon commented on 56c7d96 Feb 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EisenbergEffect
Copy link

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.

@mbx-mbx
Copy link
Owner Author

@mbx-mbx mbx-mbx commented on 56c7d96 Feb 1, 2020

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.

@EisenbergEffect
Copy link

@EisenbergEffect EisenbergEffect commented on 56c7d96 Feb 1, 2020

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.

@mbx-mbx
Copy link
Owner Author

@mbx-mbx mbx-mbx commented on 56c7d96 Feb 1, 2020

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?

@EisenbergEffect
Copy link

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.

@mbx-mbx
Copy link
Owner Author

@mbx-mbx mbx-mbx commented on 56c7d96 Feb 2, 2020

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.

@mbx-mbx
Copy link
Owner Author

@mbx-mbx mbx-mbx commented on 56c7d96 Feb 3, 2020

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.

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.

Please sign in to comment.