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
Copy file name to clipboardExpand all lines: README.md
+102-8Lines changed: 102 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ the management of object lifetimes and the inversion of control in your applicat
18
18
19
19
<br />
20
20
21
-
# Features
21
+
##Features
22
22
23
23
-**Singletons**: Register components as singletons, ensuring that there's only one instance throughout the entire
24
24
application.
@@ -46,15 +46,15 @@ the management of object lifetimes and the inversion of control in your applicat
46
46
47
47
<br />
48
48
49
-
# Installation
49
+
##Installation
50
50
51
51
```bash
52
52
go get -u github.com/firasdarwish/ore
53
53
```
54
54
55
55
<br />
56
56
57
-
# Usage
57
+
##Usage
58
58
59
59
### Import
60
60
@@ -433,12 +433,106 @@ cancel() //cancel the ctx
433
433
The `ore.GetResolvedScopedInstances[TInterface](context)` function returns a list of implementations of the `[TInterface]` which are Scoped in the input context:
434
434
435
435
- It returns only the instances which had been invoked (a.k.a resolved) during the context lifetime.
436
-
- All the implementations including "keyed" one will be returned.
436
+
- All the implementations (of all modules) including "keyed" one will be returned.
437
437
- The returned instances are sorted by invocation order, the first one being the latest invoked one.
438
438
- if "A" depends on "B", "C", Ore will make sure to return "B" and "C" first in the list so that they would be Disposed before "A".
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
480
+
module" to have access to the `traderContainer` of the "Trader module".
481
+
482
+
<br />
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.
533
+
534
+
<br/>
535
+
442
536
## More Complex Example
443
537
444
538
```go
@@ -488,7 +582,7 @@ func main() {
488
582
489
583
<br />
490
584
491
-
# Benchmarks
585
+
##Benchmarks
492
586
493
587
```bash
494
588
goos: windows
@@ -510,16 +604,16 @@ Checkout also [examples/benchperf/README.md](examples/benchperf/README.md)
0 commit comments