-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add Circular Dependency Detection #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…sure when register the type
…andle dataraces tests failed
…to simplify type detection
ddc89e7 to
8528129
Compare
|
As near as I can tell, it looks like you're planning on using arguments to pass the dependencies needed for construction of an object. What I haven't seen is a basic example that shows, say, your APIService requiring an instance of the NetworkManager to do its job. How is that resolved, since the outside world doesn't want to know that APIService uses NetworkManager under the hood? Also, since the circular dependency check only appears to check arguments, what happens should APIService just use the @dependency property wrapper to obtain a NetworkManager? One other note is that you're executing detectCircularDependencies on registration. First issue is that registration happens at startup and that wants to be as fast as possible. Second is what happens if a dependent type hasn't been registered yet? README example shows: func registerDependencies(in container: DependencyContainer) {
await container.register(
type: APIService.self,
instance: { APIService() },
scope: .singleton
)
await container.register(
type: NetworkManager.self,
instance: { NetworkManager() },
scope: .transient
)
}
}If APIService had a network manager as a dependency, how can the circular check succeed since NetworkManager hasn't been registered yet? There's an order-of-registration problem. The circular check needs to happen on resolution, not registration. |
|
Hello @hmlongco yes I think I should move the circular check on resolve instead of register. |
|
"Also, since the circular dependency check only appears to check arguments, what happens should a service just use the dependency property wrapper?" class ServiceA {
@Dependency(assembler) var service: ServiceB
}
class ServiceB {
@Dependency(assembler) var service: ServiceA
}Simply checking the args won't do the trick. |
Ok thank you Michael I will try to check how I can detect circular dependency even using property wrapper @dependency |
…ncy property wrapper case
…property wrapper case
|
Hello @hmlongco this is a summary of the changes that I did it to check circular dependency injection even with @dependency property wrapper Summary of Changes: Core Dependency Resolution:
Test Improvements:
Key Bug Fixes:
|
|
Hello @hmlongco maybe the changes that I made didn't correspond that you want me to resolve for dependency wrapper case ? |
This PR adds circular dependency detection to our dependency injection framework. Here are the main changes:
Dependency Graph Architecture:
Added a dependencyGraph that maintains a map of dependencies between services
Key is an ObjectIdentifier of the service type
Value is a Set of ObjectIdentifier representing its dependencies
Cycle Detection:
API Changes:
Extended the register method with an optional dependencies parameter
Dependencies must be explicitly declared to enable