You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Most of time you only need the Default Container. In rare use case such as the Modular Monolith Architecture, you might want to use multiple containers, one per module. Ore provides minimum support for "module" in this case:
Important: You will have to prevent cross modules access to the containers by yourself. For eg, don't let your "Broker
478
-
module" to have access to the `tranderContainer` of the "Trader module".
480
+
module" to have access to the `traderContainer` of the "Trader module".
479
481
480
482
<br />
481
483
484
+
### Injecting value at Runtime
485
+
486
+
A common scenario is that your "Service" depends on something which you couldn't provide on registration time. You can provide this dependency only when certain requests or events arrive later. Ore allows you to build an "incomplete" dependency graph using the "place holder".
487
+
488
+
```go
489
+
//register SomeService which depends on "someConfig"
ctx = ore.ProvideScopedValue(ctx, "Public user config", "someConfig")
513
+
} else {
514
+
ctx = ore.ProvideScopedValue(ctx, "Private user config", "someConfig")
515
+
}
516
+
}
517
+
518
+
//Get the service to handle this request
519
+
service, ctx:= ore.Get[*SomeService](ctx)
520
+
fmt.Println(service.someConfig) //"Admin config"
521
+
```
522
+
523
+
([See full codes here](./examples/placeholderdemo/main.go))
524
+
525
+
-`ore.RegisterPlaceHolder[T](key...)` registers a future value with Scoped lifetime.
526
+
- This value will be injected in runtime using the `ProvideScopedValue` function.
527
+
- Resolving objects which depend on this value will panic if the value has not been provided.
528
+
529
+
-`ore.ProvideScopedValue[T](context, value T, key...)` injects a concrete value into the given context
530
+
-`ore` can access (`Get()` or `GetList()`) to this value only if the corresponding place holder (which matches the type and keys) is registered.
531
+
532
+
- A value provided to a place holder would never replace value returned by other resolvers. It's the opposite, if a type (and key) could be resolved by a real resolver (such as `RegisterLazyFunc`, `RegisterLazyCreator`...), then the later would take precedent.
0 commit comments