Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@maxim-chipeev
Copy link
Contributor

At Faire we use a custom property wrapper @Resolved to make Swinject calls (hopefully we can land it in this repo soon), and had to make some significant performance trade-offs to cache the GraphIdentifier without it being properly exposed (involved a workaround using the Lazy wrapper).

This one method & a simple ServiceKey check won't cause any change in Swinject native performance but will permit a few powerful use cases. :)

@maxim-chipeev maxim-chipeev self-assigned this May 31, 2024
let key = ServiceKey(serviceType: Service.self, argumentsType: Arguments.self, name: name, option: option)

if key == Self.graphIdentifierKey {
return currentObjectGraph as! Service?

Choose a reason for hiding this comment

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

We will crash if this cast fails.
Would it make sense to throw an error with a message instead? Or default to a nil Service?

Copy link
Contributor Author

@maxim-chipeev maxim-chipeev May 31, 2024

Choose a reason for hiding this comment

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

Sure, I can do that.

But, it should be impossible. The cast is always to GraphIdentifier? as graphIdentifierKey ensures we're making a call to resolve a GraphIdentifier.

If the currentObjectGraph is value nil that will successfully cast to GraphIdentifier?.

let key = ServiceKey(serviceType: Service.self, argumentsType: Arguments.self, name: name, option: option)

if key == Self.graphIdentifierKey {
return currentObjectGraph as! Service?
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return currentObjectGraph as! Service?
return currentObjectGraph as? Service

Has similar runtime performance

@maxim-chipeev maxim-chipeev merged commit 0ee26f4 into master May 31, 2024
@maxim-chipeev maxim-chipeev deleted the maxim/expose-graph branch May 31, 2024 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants