diff --git a/README.md b/README.md index 6d83eac..1001100 100644 --- a/README.md +++ b/README.md @@ -115,11 +115,11 @@ c.AddOne() ```go // register -ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{}) +ore.RegisterCreator[Counter](ore.Scoped, &models.SimpleCounter{}) // OR -//ore.RegisterLazyCreator[Counter](ore.Transient, &models.SimpleCounter{}) -//ore.RegisterLazyCreator[Counter](ore.Singleton, &models.SimpleCounter{}) +//ore.RegisterCreator[Counter](ore.Transient, &models.SimpleCounter{}) +//ore.RegisterCreator[Counter](ore.Singleton, &models.SimpleCounter{}) ctx := context.Background() @@ -142,17 +142,17 @@ fmt.Println("TOTAL: ", c.GetCount()) ```go // register -ore.RegisterLazyFunc[Counter](ore.Scoped, func(ctx context.Context) (Counter, context.Context) { +ore.RegisterFunc[Counter](ore.Scoped, func(ctx context.Context) (Counter, context.Context) { return &models.SimpleCounter{}, ctx }) // OR -//ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) { +//ore.RegisterFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) { // return &models.SimpleCounter{}, ctx //}) // Keyed service registration -//ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { +//ore.RegisterFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { // return &models.SimpleCounter{}, ctx //}, "name here", 1234) @@ -180,15 +180,15 @@ fmt.Println("TOTAL: ", c.GetCount()) ```go // register -ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{}) +ore.RegisterCreator[Counter](ore.Scoped, &models.SimpleCounter{}) -ore.RegisterLazyCreator[Counter](ore.Scoped, &yetAnotherCounter{}) +ore.RegisterCreator[Counter](ore.Scoped, &yetAnotherCounter{}) -ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) { +ore.RegisterFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) { return &models.SimpleCounter{}, ctx }) -ore.RegisterLazyCreator[Counter](ore.Singleton, &yetAnotherCounter{}) +ore.RegisterCreator[Counter](ore.Singleton, &yetAnotherCounter{}) ctx := context.Background() @@ -217,11 +217,11 @@ The last registered implementation takes precedence, so you can register a mock ```go // register -ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { +ore.RegisterFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { return &models.SimpleCounter{}, ctx }, "name here", 1234) -//ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{}, "name here", 1234) +//ore.RegisterCreator[Counter](ore.Scoped, &models.SimpleCounter{}, "name here", 1234) //ore.RegisterEagerSingleton[Counter](&models.SimpleCounter{}, "name here", 1234) @@ -249,13 +249,13 @@ type Trader struct { } //implements IPerson func TestGetInterfaceAlias(t *testing.T) { - ore.RegisterLazyFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) { + ore.RegisterFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) { return &Broker{Name: "Peter"}, ctx }) - ore.RegisterLazyFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) { + ore.RegisterFunc(ore.Scoped, func(ctx context.Context) (*Broker, context.Context) { return &Broker{Name: "John"}, ctx }) - ore.RegisterLazyFunc(ore.Scoped, func(ctx context.Context) (*Trader, context.Context) { + ore.RegisterFunc(ore.Scoped, func(ctx context.Context) (*Trader, context.Context) { return &Trader{Name: "Mary"}, ctx }) @@ -360,7 +360,7 @@ Here how Ore can help you: type Disposer interface { Dispose() } -ore.RegisterLazyCreator(ore.Scoped, &SomeDisposableService{}) //*SomeDisposableService implements Disposer +ore.RegisterCreator(ore.Scoped, &SomeDisposableService{}) //*SomeDisposableService implements Disposer //a new request arrive ctx, cancel := context.WithCancel(context.Background()) @@ -399,8 +399,8 @@ The `ore.GetResolvedScopedInstances[TInterface](context)` function returns a lis | GetResolvedSingletons | GetResolvedSingletonsFromContainer | | RegisterAlias | RegisterAliasToContainer | | RegisterEagerSingleton | RegisterEagerSingletonToContainer | -| RegisterLazyCreator | RegisterLazyCreatorToContainer | -| RegisterLazyFunc | RegisterLazyFuncToContainer | +| RegisterCreator | RegisterCreatorToContainer | +| RegisterFunc | RegisterFuncToContainer | | RegisterPlaceHolder | RegisterPlaceHolderToContainer | | ProvideScopedValue | ProvideScopedValueToContainer | @@ -409,7 +409,7 @@ Most of time you only need the Default Container. In rare use case such as the M ```go //broker module brokerContainer := ore.NewContainer() -ore.RegisterLazyFuncToContainer(brokerContainer, ore.Singleton, func(ctx context.Context) (*Broker, context.Context) { +ore.RegisterFuncToContainer(brokerContainer, ore.Singleton, func(ctx context.Context) (*Broker, context.Context) { brs, ctx = ore.GetFromContainer[*BrokerageSystem](brokerContainer, ctx) return &Broker{brs}, ctx }) @@ -420,7 +420,7 @@ broker, _ := ore.GetFromContainer[*Broker](brokerContainer, context.Background() //trader module traderContainer := ore.NewContainer() -ore.RegisterLazyFuncToContainer(traderContainer, ore.Singleton, func(ctx context.Context) (*Trader, context.Context) { +ore.RegisterFuncToContainer(traderContainer, ore.Singleton, func(ctx context.Context) (*Trader, context.Context) { mkp, ctx = ore.GetFromContainer[*MarketPlace](traderContainer, ctx) return &Trader{mkp}, ctx }) @@ -438,7 +438,7 @@ A common scenario is that your "Service" depends on something which you couldn't ```go //register SomeService which depends on "someConfig" -ore.RegisterLazyFunc[*SomeService](ore.Scoped, func(ctx context.Context) (*SomeService, context.Context) { +ore.RegisterFunc[*SomeService](ore.Scoped, func(ctx context.Context) (*SomeService, context.Context) { someConfig, ctx := ore.Get[string](ctx, "someConfig") return &SomeService{someConfig}, ctx }) @@ -480,7 +480,7 @@ fmt.Println(service.someConfig) //"Admin config" - `ore.ProvideScopedValue[T](context, value T, key...)` injects a concrete value into the given context - `ore` can access (`Get()` or `GetList()`) to this value only if the corresponding placeholder (which matches the type and keys) is registered. -- A value provided to a placeholder 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. +- A value provided to a placeholder 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 `RegisterFunc`, `RegisterCreator`...), then the later would take precedent.
@@ -513,7 +513,7 @@ func (gc *genericCounter[T]) GetCount(ctx context.Context) T { ```go // register -ore.RegisterLazyFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) (GenericCounter[int], context.Context) { +ore.RegisterFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) (GenericCounter[int], context.Context) { return &genericCounter[int]{}, ctx }) @@ -530,8 +530,8 @@ goos: windows goarch: amd64 pkg: github.com/firasdarwish/ore cpu: 13th Gen Intel(R) Core(TM) i9-13900H -BenchmarkRegisterLazyFunc-20 5706694 196.9 ns/op -BenchmarkRegisterLazyCreator-20 6283534 184.5 ns/op +BenchmarkRegisterFunc-20 5706694 196.9 ns/op +BenchmarkRegisterCreator-20 6283534 184.5 ns/op BenchmarkRegisterEagerSingleton-20 5146953 211.5 ns/op BenchmarkInitialGet-20 3440072 352.1 ns/op BenchmarkGet-20 9806043 121.8 ns/op diff --git a/alias_test.go b/alias_test.go index d285b25..52c0088 100644 --- a/alias_test.go +++ b/alias_test.go @@ -11,10 +11,10 @@ import ( func TestAliasResolverConflict(t *testing.T) { clearAll() - RegisterLazyFunc(Singleton, func(ctx context.Context) (m.IPerson, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (m.IPerson, context.Context) { return &m.Trader{Name: "Peter Singleton"}, ctx }) - RegisterLazyFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "Mary Transient"}, ctx }) @@ -36,10 +36,10 @@ func TestAliasResolverConflict(t *testing.T) { func TestAliasOfAliasIsNotAllow(t *testing.T) { clearAll() - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.Trader, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.Trader, context.Context) { return &m.Trader{Name: "Peter Singleton"}, ctx }) - RegisterLazyFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "Mary Transient"}, ctx }) @@ -58,13 +58,13 @@ func TestAliasOfAliasIsNotAllow(t *testing.T) { func TestAliasWithDifferentScope(t *testing.T) { clearAll() module := "TestGetInterfaceAliasWithDifferentScope" - RegisterLazyFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "Transient"}, ctx }, module) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "Singleton"}, ctx }, module) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "Scoped"}, ctx }, module) RegisterAlias[m.IPerson, *m.Broker]() //link m.IPerson to *m.Broker @@ -80,24 +80,24 @@ func TestAliasWithDifferentScope(t *testing.T) { func TestAliasIsScopedByKeys(t *testing.T) { clearAll() - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "Peter1"}, ctx }, "module1") - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "John1"}, ctx }, "module1") - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) { return &m.Trader{Name: "Mary1"}, ctx }, "module1") - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) { return &m.Broker{Name: "John2"}, ctx }, "module2") - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) { return &m.Trader{Name: "Mary2"}, ctx }, "module2") - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) { return &m.Trader{Name: "Mary3"}, ctx }, "module3") @@ -138,7 +138,7 @@ func TestGetGenericAlias(t *testing.T) { for _, registrationType := range types { container := NewContainer() - RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) { + RegisterFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) { return &m.SimpleCounterUint{}, ctx }) RegisterAliasToContainer[interfaces.SomeCounterGeneric[uint], *m.SimpleCounterUint](container) @@ -157,7 +157,7 @@ func TestGetListGenericAlias(t *testing.T) { container := NewContainer() for i := 0; i < 3; i++ { - RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) { + RegisterFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) { return &m.SimpleCounterUint{}, ctx }) } diff --git a/benchmarks_test.go b/benchmarks_test.go index f7a9136..0f63f1b 100644 --- a/benchmarks_test.go +++ b/benchmarks_test.go @@ -7,23 +7,23 @@ import ( "testing" ) -func BenchmarkRegisterLazyFunc(b *testing.B) { +func BenchmarkRegisterFunc(b *testing.B) { clearAll() b.ResetTimer() for i := 0; i < b.N; i++ { - RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) } } -func BenchmarkRegisterLazyCreator(b *testing.B) { +func BenchmarkRegisterCreator(b *testing.B) { clearAll() b.ResetTimer() for i := 0; i < b.N; i++ { - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) } } @@ -32,20 +32,20 @@ func BenchmarkRegisterEagerSingleton(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) } } func BenchmarkInitialGet(b *testing.B) { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) Seal() Validate() @@ -61,13 +61,13 @@ func BenchmarkInitialGet(b *testing.B) { func BenchmarkGet(b *testing.B) { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) Seal() Validate() ctx := context.Background() @@ -81,13 +81,13 @@ func BenchmarkGet(b *testing.B) { func BenchmarkInitialGetList(b *testing.B) { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) Seal() Validate() @@ -102,13 +102,13 @@ func BenchmarkInitialGetList(b *testing.B) { func BenchmarkGetList(b *testing.B) { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) Seal() Validate() ctx := context.Background() diff --git a/creator_test.go b/creator_test.go index 231103c..693c9bc 100644 --- a/creator_test.go +++ b/creator_test.go @@ -9,11 +9,11 @@ import ( "github.com/stretchr/testify/assert" ) -func TestRegisterLazyCreator(t *testing.T) { +func TestRegisterCreator(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) c, _ := Get[interfaces.SomeCounter](context.Background()) @@ -26,36 +26,36 @@ func TestRegisterLazyCreator(t *testing.T) { } } -func TestRegisterLazyCreatorNilFuncTransient(t *testing.T) { +func TestRegisterCreatorNilFuncTransient(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyCreator[interfaces.SomeCounter](Transient, nil) + RegisterCreator[interfaces.SomeCounter](Transient, nil) }) } -func TestRegisterLazyCreatorNilFuncScoped(t *testing.T) { +func TestRegisterCreatorNilFuncScoped(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyCreator[interfaces.SomeCounter](Scoped, nil) + RegisterCreator[interfaces.SomeCounter](Scoped, nil) }) } -func TestRegisterLazyCreatorNilFuncSingleton(t *testing.T) { +func TestRegisterCreatorNilFuncSingleton(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyCreator[interfaces.SomeCounter](Singleton, nil) + RegisterCreator[interfaces.SomeCounter](Singleton, nil) }) } -func TestRegisterLazyCreatorMultipleImplementations(t *testing.T) { +func TestRegisterCreatorMultipleImplementations(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) counters, _ := GetList[interfaces.SomeCounter](context.Background()) @@ -65,15 +65,15 @@ func TestRegisterLazyCreatorMultipleImplementations(t *testing.T) { } } -func TestRegisterLazyCreatorMultipleImplementationsKeyed(t *testing.T) { +func TestRegisterCreatorMultipleImplementationsKeyed(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, "firas") + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, "firas") - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, "firas") + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, "firas") - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) counters, _ := GetList[interfaces.SomeCounter](context.Background(), "firas") @@ -83,12 +83,12 @@ func TestRegisterLazyCreatorMultipleImplementationsKeyed(t *testing.T) { } } -func TestRegisterLazyCreatorSingletonState(t *testing.T) { +func TestRegisterCreatorSingletonState(t *testing.T) { var registrationType Lifetime = Singleton clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) c, _ := Get[interfaces.SomeCounter](context.Background()) c.AddOne() @@ -107,12 +107,12 @@ func TestRegisterLazyCreatorSingletonState(t *testing.T) { } } -func TestRegisterLazyCreatorScopedState(t *testing.T) { +func TestRegisterCreatorScopedState(t *testing.T) { var registrationType Lifetime = Scoped clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) ctx := context.Background() @@ -133,12 +133,12 @@ func TestRegisterLazyCreatorScopedState(t *testing.T) { } } -func TestRegisterLazyCreatorTransientState(t *testing.T) { +func TestRegisterCreatorTransientState(t *testing.T) { var registrationType Lifetime = Transient clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) ctx := context.Background() @@ -159,26 +159,26 @@ func TestRegisterLazyCreatorTransientState(t *testing.T) { } } -func TestRegisterLazyCreatorNilKeyOnRegistering(t *testing.T) { +func TestRegisterCreatorNilKeyOnRegistering(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}, "", nil) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}, "", nil) }) } -func TestRegisterLazyCreatorNilKeyOnGetting(t *testing.T) { +func TestRegisterCreatorNilKeyOnGetting(t *testing.T) { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}, "firas") + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}, "firas") assert.Panics(t, func() { Get[interfaces.SomeCounter](context.Background(), nil) }) } -func TestRegisterLazyCreatorGeneric(t *testing.T) { +func TestRegisterCreatorGeneric(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) + RegisterCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) c, _ := Get[interfaces.SomeCounterGeneric[uint]](context.Background()) @@ -191,15 +191,15 @@ func TestRegisterLazyCreatorGeneric(t *testing.T) { } } -func TestRegisterLazyCreatorMultipleGenericImplementations(t *testing.T) { +func TestRegisterCreatorMultipleGenericImplementations(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) + RegisterCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) - RegisterLazyCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) + RegisterCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) - RegisterLazyCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) + RegisterCreator[interfaces.SomeCounterGeneric[uint]](registrationType, &models.CounterGeneric[uint]{}) counters, _ := GetList[interfaces.SomeCounterGeneric[uint]](context.Background()) diff --git a/eager_singleton_test.go b/eager_singleton_test.go index 10d6548..089154a 100644 --- a/eager_singleton_test.go +++ b/eager_singleton_test.go @@ -12,7 +12,7 @@ import ( func TestRegisterEagerSingleton(t *testing.T) { clearAll() - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) c, _ := Get[interfaces.SomeCounter](context.Background()) @@ -27,16 +27,16 @@ func TestRegisterEagerSingleton(t *testing.T) { func TestRegisterEagerSingletonNilImplementation(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterEagerSingleton[interfaces.SomeCounter](nil) + RegisterSingleton[interfaces.SomeCounter](nil) }) } func TestRegisterEagerSingletonMultipleImplementations(t *testing.T) { clearAll() - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) counters, _ := GetList[interfaces.SomeCounter](context.Background()) @@ -48,10 +48,10 @@ func TestRegisterEagerSingletonMultipleImplementations(t *testing.T) { func TestRegisterEagerSingletonMultipleImplementationsKeyed(t *testing.T) { clearAll() - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas") - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas") + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas") + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas") - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) counters, _ := GetList[interfaces.SomeCounter](context.Background(), "firas") @@ -63,7 +63,7 @@ func TestRegisterEagerSingletonMultipleImplementationsKeyed(t *testing.T) { func TestRegisterEagerSingletonSingletonState(t *testing.T) { clearAll() - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}) c, _ := Get[interfaces.SomeCounter](context.Background()) c.AddOne() @@ -85,13 +85,13 @@ func TestRegisterEagerSingletonSingletonState(t *testing.T) { func TestRegisterEagerSingletonNilKeyOnRegistering(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, nil, "") + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, nil, "") }) } func TestRegisterEagerSingletonNilKeyOnGetting(t *testing.T) { clearAll() - RegisterEagerSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas") + RegisterSingleton[interfaces.SomeCounter](&models.SimpleCounter{}, "firas") assert.Panics(t, func() { Get[interfaces.SomeCounter](context.Background(), nil, "") }) @@ -100,7 +100,7 @@ func TestRegisterEagerSingletonNilKeyOnGetting(t *testing.T) { func TestRegisterEagerSingletonGeneric(t *testing.T) { clearAll() - RegisterEagerSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) + RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) c, _ := Get[interfaces.SomeCounterGeneric[uint]](context.Background()) @@ -115,9 +115,9 @@ func TestRegisterEagerSingletonGeneric(t *testing.T) { func TestRegisterEagerSingletonMultipleGenericImplementations(t *testing.T) { clearAll() - RegisterEagerSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) - RegisterEagerSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) - RegisterEagerSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) + RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) + RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) + RegisterSingleton[interfaces.SomeCounterGeneric[uint]](&models.CounterGeneric[uint]{}) counters, _ := GetList[interfaces.SomeCounterGeneric[uint]](context.Background()) diff --git a/examples/benchperf/internal/DiOre.go b/examples/benchperf/internal/DiOre.go index e248b56..93f7804 100644 --- a/examples/benchperf/internal/DiOre.go +++ b/examples/benchperf/internal/DiOre.go @@ -14,45 +14,45 @@ func BuildContainerOre(disableValidation bool) *ore.Container { } func RegisterToOreContainer(container *ore.Container) { - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (*A, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (*A, context.Context) { b, ctx := ore.GetFromContainer[*B](container, ctx) c, ctx := ore.GetFromContainer[*C](container, ctx) return NewA(b, c), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (*B, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (*B, context.Context) { d, ctx := ore.GetFromContainer[*D](container, ctx) e, ctx := ore.GetFromContainer[*E](container, ctx) return NewB(d, e), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (*C, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (*C, context.Context) { return NewC(), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (*D, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (*D, context.Context) { f, ctx := ore.GetFromContainer[*F](container, ctx) h, ctx := ore.GetFromContainer[H](container, ctx) return NewD(f, h), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (*E, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (*E, context.Context) { gs, ctx := ore.GetListFromContainer[G](container, ctx) return NewE(gs), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Singleton, func(ctx context.Context) (*F, context.Context) { + ore.RegisterFuncToContainer(container, ore.Singleton, func(ctx context.Context) (*F, context.Context) { return NewF(), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (*Ga, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (*Ga, context.Context) { return NewGa(), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Singleton, func(ctx context.Context) (G, context.Context) { + ore.RegisterFuncToContainer(container, ore.Singleton, func(ctx context.Context) (G, context.Context) { return NewGb(), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (G, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (G, context.Context) { return NewGc(), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (G, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (G, context.Context) { ga, ctx := ore.GetFromContainer[*Ga](container, ctx) return NewDGa(ga), ctx }) - ore.RegisterLazyFuncToContainer(container, ore.Transient, func(ctx context.Context) (H, context.Context) { + ore.RegisterFuncToContainer(container, ore.Transient, func(ctx context.Context) (H, context.Context) { return NewHr(), ctx }) } diff --git a/examples/placeholderdemo/main.go b/examples/placeholderdemo/main.go index df90e9a..5460b59 100644 --- a/examples/placeholderdemo/main.go +++ b/examples/placeholderdemo/main.go @@ -15,14 +15,14 @@ type SomeService struct { func main() { //register SomeService which depends on "someConfig" - ore.RegisterLazyFunc[*SomeService](ore.Scoped, func(ctx context.Context) (*SomeService, context.Context) { + ore.RegisterFunc[*SomeService](ore.Scoped, func(ctx context.Context) (*SomeService, context.Context) { someConfig, ctx := ore.Get[string](ctx, "someConfig") return &SomeService{someConfig}, ctx }) //someConfig is unknow at registration time //the value of "someConfig" depends on the future user's request - ore.RegisterPlaceHolder[string]("someConfig") + ore.RegisterPlaceholder[string]("someConfig") //Seal registration, no further registration is allowed ore.Seal() diff --git a/examples/shutdownerdemo/main.go b/examples/shutdownerdemo/main.go index d300197..b65bae6 100644 --- a/examples/shutdownerdemo/main.go +++ b/examples/shutdownerdemo/main.go @@ -41,8 +41,8 @@ func (*myScopedRepo) New(ctx context.Context) (*myScopedRepo, context.Context) { } func main() { - ore.RegisterEagerSingleton[*myGlobalRepo](&myGlobalRepo{}) - ore.RegisterLazyCreator(ore.Scoped, &myScopedRepo{}) + ore.RegisterSingleton[*myGlobalRepo](&myGlobalRepo{}) + ore.RegisterCreator(ore.Scoped, &myScopedRepo{}) ore.Validate() diff --git a/examples/simple/main.go b/examples/simple/main.go index 05c9c33..2840fbd 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -8,20 +8,20 @@ import ( ) func main() { - ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { + ore.RegisterFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { fmt.Println("NEWLY INITIALIZED FROM FUNC") return &mycounter{}, ctx }, "firas") - ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { + ore.RegisterFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) { fmt.Println("NEWLY INITIALIZED FROM FUNC") return &mycounter{}, ctx }, "darwish") - ore.RegisterLazyCreator[Counter](ore.Singleton, &mycounter{}) + ore.RegisterCreator[Counter](ore.Singleton, &mycounter{}) cc := &mycounter{} - ore.RegisterEagerSingleton[Counter](cc) + ore.RegisterSingleton[Counter](cc) ctx := context.Background() @@ -37,7 +37,7 @@ func main() { fmt.Printf("Total Count: %v", c.Total()) - ore.RegisterLazyCreator[GenericCounter[uint]](ore.Scoped, &genCounter[uint]{}) + ore.RegisterCreator[GenericCounter[uint]](ore.Scoped, &genCounter[uint]{}) gc, ctx := ore.Get[GenericCounter[uint]](ctx) gc.Add(1) diff --git a/get_list_test.go b/get_list_test.go index 2803345..e044425 100644 --- a/get_list_test.go +++ b/get_list_test.go @@ -11,7 +11,7 @@ func TestGetList(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}) counters, _ := GetList[interfaces.SomeCounter](context.Background()) @@ -35,10 +35,10 @@ func TestGetListKeyed(t *testing.T) { key := "somekeyhere" - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, key) - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, key) - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, key) - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, "Firas") + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, key) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, key) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, key) + RegisterCreator[interfaces.SomeCounter](registrationType, &models.SimpleCounter{}, "Firas") counters, _ := GetList[interfaces.SomeCounter](context.Background(), key) if got := len(counters); got != 3 { diff --git a/get_test.go b/get_test.go index 40366ae..369a32f 100644 --- a/get_test.go +++ b/get_test.go @@ -15,7 +15,7 @@ func TestGet(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter{}) c, _ := Get[interfaces.SomeCounter](context.Background()) @@ -32,12 +32,12 @@ func TestGetLatestByDefault(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter{}) c, _ := Get[interfaces.SomeCounter](context.Background()) c.AddOne() c.AddOne() - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter2{}) + RegisterCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter2{}) c, _ = Get[interfaces.SomeCounter](context.Background()) c.AddOne() c.AddOne() @@ -63,7 +63,7 @@ func TestGetKeyed(t *testing.T) { key := fmt.Sprintf("keynum: %v", i) - RegisterLazyCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter{}, key) + RegisterCreator[interfaces.SomeCounter](registrationType, &m.SimpleCounter{}, key) c, _ := Get[interfaces.SomeCounter](context.Background(), key) @@ -80,24 +80,24 @@ func TestGetResolvedSingletons(t *testing.T) { t.Run("When multiple lifetimes and keys are registered", func(t *testing.T) { //Arrange clearAll() - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { return &m.DisposableService1{Name: "A1"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { return &m.DisposableService1{Name: "A2"}, ctx }) - RegisterEagerSingleton(&m.DisposableService2{Name: "E1"}) - RegisterEagerSingleton(&m.DisposableService2{Name: "E2"}) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterSingleton(&m.DisposableService2{Name: "E1"}) + RegisterSingleton(&m.DisposableService2{Name: "E2"}) + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "S1"}, ctx }) - RegisterLazyFunc(Transient, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(Transient, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "S2"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService4, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService4, context.Context) { return &m.DisposableService4{Name: "X1"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService4, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService4, context.Context) { return &m.DisposableService4{Name: "X2"}, ctx }, "somekey") @@ -135,13 +135,13 @@ func TestGetResolvedSingletons(t *testing.T) { t.Run("respect invocation chronological time order", func(t *testing.T) { //Arrange clearAll() - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { return &m.DisposableService1{Name: "A"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService2, context.Context) { return &m.DisposableService2{Name: "B"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "C"}, ctx }) @@ -166,15 +166,15 @@ func TestGetResolvedSingletons(t *testing.T) { t.Run("deeper invocation level is returned first", func(t *testing.T) { //Arrange clearAll() - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //2 calls 3 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "3"}, ctx }) @@ -195,11 +195,11 @@ func TestGetResolvedSingletons(t *testing.T) { func TestGetResolvedScopedInstances(t *testing.T) { t.Run("When multiple lifetimes and keys are registered", func(t *testing.T) { clearAll() - RegisterEagerSingleton(&m.DisposableService1{Name: "S1"}) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterSingleton(&m.DisposableService1{Name: "S1"}) + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { return &m.DisposableService1{Name: "S2"}, ctx }) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { return &m.DisposableService2{Name: "T1"}, ctx }, "module1") @@ -228,13 +228,13 @@ func TestGetResolvedScopedInstances(t *testing.T) { t.Run("respect invocation chronological time order", func(t *testing.T) { //Arrange clearAll() - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { return &m.DisposableService1{Name: "A"}, ctx }) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { return &m.DisposableService2{Name: "B"}, ctx }) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "C"}, ctx }) @@ -260,18 +260,18 @@ func TestGetResolvedScopedInstances(t *testing.T) { t.Run("respect invocation deep level", func(t *testing.T) { //Arrange clearAll() - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { //1 calls 3 _, ctx = Get[*m.DisposableService3](ctx) return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "3"}, ctx }) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService4, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService4, context.Context) { //4 calls 1, 2 _, ctx = Get[*m.DisposableService1](ctx) _, ctx = Get[*m.DisposableService2](ctx) diff --git a/initializer_test.go b/initializer_test.go index 10c2d75..06603c3 100644 --- a/initializer_test.go +++ b/initializer_test.go @@ -9,11 +9,11 @@ import ( "github.com/stretchr/testify/assert" ) -func TestRegisterLazyFunc(t *testing.T) { +func TestRegisterFunc(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) @@ -28,40 +28,40 @@ func TestRegisterLazyFunc(t *testing.T) { } } -func TestRegisterLazyFuncNilFuncTransient(t *testing.T) { +func TestRegisterFuncNilFuncTransient(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyFunc[interfaces.SomeCounter](Transient, nil) + RegisterFunc[interfaces.SomeCounter](Transient, nil) }) } -func TestRegisterLazyFuncNilFuncScoped(t *testing.T) { +func TestRegisterFuncNilFuncScoped(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyFunc[interfaces.SomeCounter](Scoped, nil) + RegisterFunc[interfaces.SomeCounter](Scoped, nil) }) } -func TestRegisterLazyFuncNilFuncSingleton(t *testing.T) { +func TestRegisterFuncNilFuncSingleton(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyFunc[interfaces.SomeCounter](Singleton, nil) + RegisterFunc[interfaces.SomeCounter](Singleton, nil) }) } -func TestRegisterLazyFuncMultipleImplementations(t *testing.T) { +func TestRegisterFuncMultipleImplementations(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) @@ -73,19 +73,19 @@ func TestRegisterLazyFuncMultipleImplementations(t *testing.T) { } } -func TestRegisterLazyFuncMultipleImplementationsKeyed(t *testing.T) { +func TestRegisterFuncMultipleImplementationsKeyed(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }, "firas") - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }, "firas") - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) @@ -97,12 +97,12 @@ func TestRegisterLazyFuncMultipleImplementationsKeyed(t *testing.T) { } } -func TestRegisterLazyFuncSingletonState(t *testing.T) { +func TestRegisterFuncSingletonState(t *testing.T) { var registrationType Lifetime = Singleton clearAll() - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) @@ -123,12 +123,12 @@ func TestRegisterLazyFuncSingletonState(t *testing.T) { } } -func TestRegisterLazyFuncScopedState(t *testing.T) { +func TestRegisterFuncScopedState(t *testing.T) { var registrationType Lifetime = Scoped clearAll() - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) @@ -151,12 +151,12 @@ func TestRegisterLazyFuncScopedState(t *testing.T) { } } -func TestRegisterLazyFuncTransientState(t *testing.T) { +func TestRegisterFuncTransientState(t *testing.T) { var registrationType Lifetime = Transient clearAll() - RegisterLazyFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](registrationType, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }) @@ -179,18 +179,18 @@ func TestRegisterLazyFuncTransientState(t *testing.T) { } } -func TestRegisterLazyFuncNilKeyOnRegistering(t *testing.T) { +func TestRegisterFuncNilKeyOnRegistering(t *testing.T) { clearAll() assert.Panics(t, func() { - RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }, "", nil) }) } -func TestRegisterLazyFuncNilKeyOnGetting(t *testing.T) { +func TestRegisterFuncNilKeyOnGetting(t *testing.T) { clearAll() - RegisterLazyFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { + RegisterFunc[interfaces.SomeCounter](Scoped, func(ctx context.Context) (interfaces.SomeCounter, context.Context) { return &models.SimpleCounter{}, ctx }, "firas") @@ -199,11 +199,11 @@ func TestRegisterLazyFuncNilKeyOnGetting(t *testing.T) { }) } -func TestRegisterLazyFuncGeneric(t *testing.T) { +func TestRegisterFuncGeneric(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { + RegisterFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { return &models.CounterGeneric[uint]{}, ctx }) @@ -218,19 +218,19 @@ func TestRegisterLazyFuncGeneric(t *testing.T) { } } -func TestRegisterLazyFuncMultipleGenericImplementations(t *testing.T) { +func TestRegisterFuncMultipleGenericImplementations(t *testing.T) { for _, registrationType := range types { clearAll() - RegisterLazyFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { + RegisterFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { return &models.CounterGeneric[uint]{}, ctx }) - RegisterLazyFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { + RegisterFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { return &models.CounterGeneric[uint]{}, ctx }) - RegisterLazyFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { + RegisterFunc[interfaces.SomeCounterGeneric[uint]](registrationType, func(ctx context.Context) (interfaces.SomeCounterGeneric[uint], context.Context) { return &models.CounterGeneric[uint]{}, ctx }) @@ -242,17 +242,17 @@ func TestRegisterLazyFuncMultipleGenericImplementations(t *testing.T) { } } -func TestRegisterLazyFuncScopedNested(t *testing.T) { +func TestRegisterFuncScopedNested(t *testing.T) { clearAll() - RegisterLazyFunc[*models.A](Transient, func(ctx context.Context) (*models.A, context.Context) { + RegisterFunc[*models.A](Transient, func(ctx context.Context) (*models.A, context.Context) { cc, ctx := Get[*models.C](ctx) return &models.A{ C: cc, }, ctx }) - RegisterLazyFunc[*models.C](Scoped, func(ctx context.Context) (*models.C, context.Context) { + RegisterFunc[*models.C](Scoped, func(ctx context.Context) (*models.C, context.Context) { return &models.C{}, ctx }) diff --git a/module_test.go b/module_test.go index 8d22208..056f9e5 100644 --- a/module_test.go +++ b/module_test.go @@ -13,12 +13,12 @@ func TestModuleIsolation(t *testing.T) { for _, lifetime := range types { t.Run(fmt.Sprintf("Module isolation %s", lifetime), func(t *testing.T) { con1 := NewContainer() - RegisterLazyFuncToContainer(con1, lifetime, func(ctx context.Context) (*m.Trader, context.Context) { + RegisterFuncToContainer(con1, lifetime, func(ctx context.Context) (*m.Trader, context.Context) { return &m.Trader{Name: "John"}, ctx }) con2 := NewContainer() - RegisterLazyFuncToContainer(con2, lifetime, func(ctx context.Context) (*m.Trader, context.Context) { + RegisterFuncToContainer(con2, lifetime, func(ctx context.Context) (*m.Trader, context.Context) { return &m.Trader{Name: "Mary"}, ctx }) diff --git a/ore_test.go b/ore_test.go index 95dca54..8c98f3d 100644 --- a/ore_test.go +++ b/ore_test.go @@ -10,16 +10,16 @@ import ( func TestSeal(t *testing.T) { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) Seal() assert.Panics(t, func() { - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) }) } func TestIsSeal(t *testing.T) { clearAll() - RegisterLazyCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) + RegisterCreator[interfaces.SomeCounter](Scoped, &models.SimpleCounter{}) if got := IsSealed(); got != false { t.Errorf("IsSealed() = %v; want %v", got, false) } diff --git a/registrars.go b/registrars.go index 26cddca..7e3a258 100644 --- a/registrars.go +++ b/registrars.go @@ -7,8 +7,8 @@ import ( "time" ) -// RegisterLazyCreatorToContainer Registers a lazily initialized value to the given container using a `Creator[T]` interface -func RegisterLazyCreatorToContainer[T any](con *Container, lifetime Lifetime, creator Creator[T], key ...KeyStringer) { +// RegisterCreatorToContainer Registers a lazily initialized value to the given container using a `Creator[T]` interface +func RegisterCreatorToContainer[T any](con *Container, lifetime Lifetime, creator Creator[T], key ...KeyStringer) { if creator == nil { panic(nilVal[T]()) } @@ -22,14 +22,14 @@ func RegisterLazyCreatorToContainer[T any](con *Container, lifetime Lifetime, cr addResolver[T](con, e, key...) } -// RegisterLazyCreator Registers a lazily initialized value using a `Creator[T]` interface -func RegisterLazyCreator[T any](lifetime Lifetime, creator Creator[T], key ...KeyStringer) { - RegisterLazyCreatorToContainer[T](DefaultContainer, lifetime, creator, key...) +// RegisterCreator Registers a lazily initialized value using a `Creator[T]` interface +func RegisterCreator[T any](lifetime Lifetime, creator Creator[T], key ...KeyStringer) { + RegisterCreatorToContainer[T](DefaultContainer, lifetime, creator, key...) } -// RegisterEagerSingletonToContainer Registers an eagerly instantiated singleton value to the given container. +// RegisterSingletonToContainer Registers an eagerly instantiated singleton value to the given container. // To register an eagerly instantiated scoped value use [ProvideScopedValueToContainer] -func RegisterEagerSingletonToContainer[T comparable](con *Container, impl T, key ...KeyStringer) { +func RegisterSingletonToContainer[T comparable](con *Container, impl T, key ...KeyStringer) { if isNil[T](impl) { panic(nilVal[T]()) } @@ -47,14 +47,14 @@ func RegisterEagerSingletonToContainer[T comparable](con *Container, impl T, key addResolver[T](con, e, key...) } -// RegisterEagerSingleton Registers an eagerly instantiated singleton value +// RegisterSingleton Registers an eagerly instantiated singleton value // To register an eagerly instantiated scoped value use [ProvideScopedValue] -func RegisterEagerSingleton[T comparable](impl T, key ...KeyStringer) { - RegisterEagerSingletonToContainer[T](DefaultContainer, impl, key...) +func RegisterSingleton[T comparable](impl T, key ...KeyStringer) { + RegisterSingletonToContainer[T](DefaultContainer, impl, key...) } -// RegisterLazyFuncToContainer Registers a lazily initialized value to the given container using an `Initializer[T]` function signature -func RegisterLazyFuncToContainer[T any](con *Container, lifetime Lifetime, initializer Initializer[T], key ...KeyStringer) { +// RegisterFuncToContainer Registers a lazily initialized value to the given container using an `Initializer[T]` function signature +func RegisterFuncToContainer[T any](con *Container, lifetime Lifetime, initializer Initializer[T], key ...KeyStringer) { if initializer == nil { panic(nilVal[T]()) } @@ -68,9 +68,9 @@ func RegisterLazyFuncToContainer[T any](con *Container, lifetime Lifetime, initi addResolver[T](con, e, key...) } -// RegisterLazyFunc Registers a lazily initialized value using an `Initializer[T]` function signature -func RegisterLazyFunc[T any](lifetime Lifetime, initializer Initializer[T], key ...KeyStringer) { - RegisterLazyFuncToContainer(DefaultContainer, lifetime, initializer, key...) +// RegisterFunc Registers a lazily initialized value using an `Initializer[T]` function signature +func RegisterFunc[T any](lifetime Lifetime, initializer Initializer[T], key ...KeyStringer) { + RegisterFuncToContainer(DefaultContainer, lifetime, initializer, key...) } // RegisterAliasToContainer Registers an interface type to a concrete implementation in the given container. @@ -92,11 +92,11 @@ func RegisterAlias[TInterface, TImpl any]() { RegisterAliasToContainer[TInterface, TImpl](DefaultContainer) } -// RegisterPlaceHolderToContainer registers a future value with Scoped lifetime to the given container. +// RegisterPlaceholderToContainer registers a future value with Scoped lifetime to the given container. // This value will be injected in runtime using the [ProvideScopedValue] function. // Resolving objects which depend on this value will panic if the value has not been provided. // Placeholder with the same type and key can be registered only once. -func RegisterPlaceHolderToContainer[T comparable](con *Container, key ...KeyStringer) { +func RegisterPlaceholderToContainer[T comparable](con *Container, key ...KeyStringer) { e := serviceResolverImpl[T]{ resolverMetadata: resolverMetadata{ lifetime: Scoped, @@ -105,17 +105,17 @@ func RegisterPlaceHolderToContainer[T comparable](con *Container, key ...KeyStri addResolver[T](con, e, key...) } -// RegisterPlaceHolder registers a future value with Scoped lifetime. +// RegisterPlaceholder registers a future value with Scoped lifetime. // This value will be injected in runtime using the [ProvideScopedValue] function. // Resolving objects which depend on this value will panic if the value has not been provided. // Placeholder with the same type and key can be registered only once. -func RegisterPlaceHolder[T comparable](key ...KeyStringer) { - RegisterPlaceHolderToContainer[T](DefaultContainer, key...) +func RegisterPlaceholder[T comparable](key ...KeyStringer) { + RegisterPlaceholderToContainer[T](DefaultContainer, key...) } // ProvideScopedValueToContainer injects a concrete value into the given context. // This value will be available only to the given container. And the container can only resolve this value if -// it has the matching (type and key's) Placeholder registered. Checkout the [RegisterPlaceHolderToContainer] function for more info. +// it has the matching (type and key's) Placeholder registered. Checkout the [RegisterPlaceholderToContainer] function for more info. func ProvideScopedValueToContainer[T comparable](con *Container, ctx context.Context, value T, key ...KeyStringer) context.Context { concreteValue := &concrete{ value: value, @@ -133,7 +133,7 @@ func ProvideScopedValueToContainer[T comparable](con *Container, ctx context.Con // ProvideScopedValue injects a concrete value into the given context. // This value will be available only to the default container. And the container can only resolve this value if -// it has the matching (type and key's) Placeholder registered. Checkout the [RegisterPlaceHolder] function for more info. +// it has the matching (type and key's) Placeholder registered. Checkout the [RegisterPlaceholder] function for more info. func ProvideScopedValue[T comparable](ctx context.Context, value T, key ...KeyStringer) context.Context { return ProvideScopedValueToContainer[T](DefaultContainer, ctx, value, key...) } diff --git a/registrars_placeholder_test.go b/registrars_placeholder_test.go index 11d53a1..ea28c5f 100644 --- a/registrars_placeholder_test.go +++ b/registrars_placeholder_test.go @@ -13,7 +13,7 @@ func TestPlaceHolder_HappyPath(t *testing.T) { clearAll() //register a placeHolder - RegisterPlaceHolder[*m.Trader]() + RegisterPlaceholder[*m.Trader]() //get the placeHolder value would failed assert2.PanicsWithError(t, assert2.ErrorStartsWith("No value has been provided for this placeholder"), func() { @@ -59,7 +59,7 @@ func TestPlaceHolder_ProvideValueBeforeRegistering(t *testing.T) { }) //register a matching placeHolder - RegisterPlaceHolder[*m.Trader]() + RegisterPlaceholder[*m.Trader]() //get the placeHolder value would success trader, _ := Get[*m.Trader](ctx) @@ -71,24 +71,24 @@ func TestPlaceHolder_OverrideRealResolver(t *testing.T) { clearAll() //register a real resolver - RegisterEagerSingleton(&m.Trader{Name: "Mary"}, "module1") + RegisterSingleton(&m.Trader{Name: "Mary"}, "module1") //register a placeHolder to override the real resolver should failed assert2.PanicsWithError(t, assert2.ErrorContains("has already been registered"), func() { - RegisterPlaceHolder[*m.Trader]("module1") + RegisterPlaceholder[*m.Trader]("module1") }) //register 2 time a placeHolder should failed - RegisterPlaceHolder[*m.Trader]("module2") + RegisterPlaceholder[*m.Trader]("module2") assert2.PanicsWithError(t, assert2.ErrorContains("has already been registered"), func() { - RegisterPlaceHolder[*m.Trader]("module2") + RegisterPlaceholder[*m.Trader]("module2") }) } func TestPlaceHolder_OverridePlaceHolder(t *testing.T) { clearAll() //register a placeHolder - RegisterPlaceHolder[*m.Trader]("module2") + RegisterPlaceholder[*m.Trader]("module2") //Provide the value to the placeHolder ctx := ProvideScopedValue[*m.Trader](context.Background(), &m.Trader{Name: "John"}, "module2") @@ -107,7 +107,7 @@ func TestPlaceHolder_OverridePlaceHolder(t *testing.T) { assert.Equal(t, "David", traders[0].Name) //Register a real resolver should override the placeHolder resolver - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.Trader, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.Trader, context.Context) { return &m.Trader{Name: "Mary"}, ctx }, "module2") @@ -137,10 +137,10 @@ func TestPlaceHolder_OverridePlaceHolder(t *testing.T) { // placeholder value of a module is not accessible from other module func TestPlaceHolder_PerModule(t *testing.T) { con1 := NewContainer() - RegisterPlaceHolderToContainer[*m.Trader](con1) + RegisterPlaceholderToContainer[*m.Trader](con1) con2 := NewContainer() - RegisterPlaceHolderToContainer[*m.Trader](con2) + RegisterPlaceholderToContainer[*m.Trader](con2) ctx := ProvideScopedValueToContainer(con1, context.Background(), &m.Trader{Name: "John"}) trader, ctx := GetFromContainer[*m.Trader](con1, ctx) diff --git a/validate_test.go b/validate_test.go index 20dfefe..6a22366 100644 --- a/validate_test.go +++ b/validate_test.go @@ -13,7 +13,7 @@ func TestValidate_CircularDepsUniformLifetype(t *testing.T) { for _, lt := range types { t.Run("Direct circular "+lt.String()+" (1 calls 1)", func(t *testing.T) { clearAll() - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService1](ctx) //1 calls 1 return &m.DisposableService1{Name: "1"}, ctx }) @@ -21,15 +21,15 @@ func TestValidate_CircularDepsUniformLifetype(t *testing.T) { }) t.Run("Indirect circular "+lt.String()+" (1 calls 2 calls 3 calls 1)", func(t *testing.T) { clearAll() - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //2 calls 3 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { _, ctx = Get[*m.DisposableService1](ctx) //3 calls 1 return &m.DisposableService3{Name: "3"}, ctx }) @@ -37,19 +37,19 @@ func TestValidate_CircularDepsUniformLifetype(t *testing.T) { }) t.Run("Middle circular "+lt.String()+" (1 calls 2 calls 3 calls 4 calls 2)", func(t *testing.T) { clearAll() - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //2 calls 3 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { _, ctx = Get[*m.DisposableService4](ctx) //3 calls 4 return &m.DisposableService3{Name: "3"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService4, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService4, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //4 calls 2 return &m.DisposableService4{Name: "4"}, ctx }) @@ -57,25 +57,25 @@ func TestValidate_CircularDepsUniformLifetype(t *testing.T) { }) t.Run("circular on complex tree "+lt.String()+"", func(t *testing.T) { clearAll() - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 _, ctx = Get[*m.DisposableService3](ctx) //1 calls 3 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService4](ctx) //2 calls 4 _, ctx = Get[*m.DisposableService5](ctx) //2 calls 5 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { _, ctx = Get[*m.DisposableService4](ctx) //3 calls 4 return &m.DisposableService3{Name: "3"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService4, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService4, context.Context) { _, ctx = Get[*m.DisposableService5](ctx) //4 calls 5 return &m.DisposableService4{Name: "4"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService5, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService5, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //5 calls 3 => circular here: 5->3->4->5 return &m.DisposableService5{Name: "5"}, ctx }) @@ -83,38 +83,38 @@ func TestValidate_CircularDepsUniformLifetype(t *testing.T) { }) t.Run("fake circular top down "+lt.String()+": (1 calls 2 (x2) calls 3 calls 4, 2 calls 4)", func(t *testing.T) { clearAll() - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 again return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //2 calls 3 _, ctx = Get[*m.DisposableService4](ctx) //2 calls 4 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { _, ctx = Get[*m.DisposableService4](ctx) //3 calls 4 _, ctx = Get[*m.DisposableService4](ctx) //3 calls 4 return &m.DisposableService3{Name: "3"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService4, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService4, context.Context) { return &m.DisposableService4{Name: "4"}, ctx }) assert.NotPanics(t, Validate) }) t.Run("fake circular sibling "+lt.String()+": 1 calls 2 & 3; 2 calls 3)", func(t *testing.T) { clearAll() - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 _, ctx = Get[*m.DisposableService3](ctx) //1 calls 3 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //2 calls 3 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(lt, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "3"}, ctx }) assert.NotPanics(t, Validate) @@ -125,24 +125,24 @@ func TestValidate_CircularDepsUniformLifetype(t *testing.T) { func TestValidate_CircularMixedLifetype(t *testing.T) { clearAll() - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService4](ctx) //2 calls 4 _, ctx = Get[*m.DisposableService5](ctx) //2 calls 5 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { _, ctx = Get[*m.DisposableService4](ctx) //3 calls 4 return &m.DisposableService3{Name: "3"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService4, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService4, context.Context) { _, ctx = Get[*m.DisposableService5](ctx) //4 calls 5 return &m.DisposableService4{Name: "4"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService5, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService5, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //5 calls 3 => circular here: 5->3->4->5 return &m.DisposableService5{Name: "5"}, ctx }) - RegisterLazyFunc(Transient, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Transient, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 calls 2 _, ctx = Get[*m.DisposableService3](ctx) //1 calls 3 return &m.DisposableService1{Name: "1"}, ctx @@ -155,10 +155,10 @@ func TestValidate_CircularMixedLifetype(t *testing.T) { func TestValidate_LifetimeAlignment_SingletonCallsScoped(t *testing.T) { con := NewContainer() - RegisterLazyFuncToContainer(con, Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFuncToContainer(con, Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFuncToContainer(con, Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFuncToContainer(con, Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = GetFromContainer[*m.DisposableService2](con, ctx) //1 depends on 2 return &m.DisposableService1{Name: "1"}, ctx }) @@ -166,26 +166,26 @@ func TestValidate_LifetimeAlignment_SingletonCallsScoped(t *testing.T) { } func TestValidate_LifetimeAlignment_ScopedCallsTransient(t *testing.T) { con := NewContainer() - RegisterLazyFuncToContainer(con, Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFuncToContainer(con, Scoped, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = GetFromContainer[*m.DisposableService2](con, ctx) //1 depends on 2 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFuncToContainer(con, Transient, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFuncToContainer(con, Transient, func(ctx context.Context) (*m.DisposableService2, context.Context) { return &m.DisposableService2{Name: "2"}, ctx }) assert2.PanicsWithError(t, assert2.ErrorStartsWith("detect lifetime misalignment"), con.Validate) } func TestValidate_LifetimeAlignment_SingletonCallsTransient(t *testing.T) { con := NewContainer() - RegisterLazyFuncToContainer(con, Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFuncToContainer(con, Singleton, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = GetFromContainer[*m.DisposableService2](con, ctx) //1 depends on 2 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFuncToContainer(con, Singleton, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFuncToContainer(con, Singleton, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = GetFromContainer[*m.DisposableService3](con, ctx) //2 depends on 3 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFuncToContainer(con, Transient, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFuncToContainer(con, Transient, func(ctx context.Context) (*m.DisposableService3, context.Context) { return &m.DisposableService3{Name: "3"}, ctx }) assert2.PanicsWithError(t, assert2.ErrorStartsWith("detect lifetime misalignment"), con.Validate) @@ -193,15 +193,15 @@ func TestValidate_LifetimeAlignment_SingletonCallsTransient(t *testing.T) { func TestValidate_MissingDependency(t *testing.T) { clearAll() - RegisterLazyFunc(Transient, func(ctx context.Context) (*m.DisposableService1, context.Context) { + RegisterFunc(Transient, func(ctx context.Context) (*m.DisposableService1, context.Context) { _, ctx = Get[*m.DisposableService2](ctx) //1 depends on 2 return &m.DisposableService1{Name: "1"}, ctx }) - RegisterLazyFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { + RegisterFunc(Scoped, func(ctx context.Context) (*m.DisposableService2, context.Context) { _, ctx = Get[*m.DisposableService3](ctx) //2 depends on 3 return &m.DisposableService2{Name: "2"}, ctx }) - RegisterLazyFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { + RegisterFunc(Singleton, func(ctx context.Context) (*m.DisposableService3, context.Context) { _, ctx = Get[*m.DisposableService4](ctx) //3 depends on 4 return &m.DisposableService3{Name: "3"}, ctx }) @@ -211,20 +211,20 @@ func TestValidate_MissingDependency(t *testing.T) { func TestValidate_WithPlaceHolder(t *testing.T) { con := NewContainer() - RegisterPlaceHolderToContainer[*m.Trader](con) + RegisterPlaceholderToContainer[*m.Trader](con) assert.NotPanics(t, con.Validate) } func TestValidate_WithPlaceHolderInterface(t *testing.T) { con := NewContainer() - RegisterPlaceHolderToContainer[m.IPerson](con) + RegisterPlaceholderToContainer[m.IPerson](con) assert.NotPanics(t, con.Validate) } func TestValidate_DisableValidation(t *testing.T) { con := NewContainer() - RegisterPlaceHolderToContainer[*m.Trader](con) - RegisterLazyFuncToContainer(con, Singleton, func(ctx context.Context) (*m.Broker, context.Context) { + RegisterPlaceholderToContainer[*m.Trader](con) + RegisterFuncToContainer(con, Singleton, func(ctx context.Context) (*m.Broker, context.Context) { _, ctx = GetFromContainer[*m.Trader](con, ctx) return &m.Broker{Name: "John"}, ctx })