ardanlabs / service

Starter-kit for writing services in Go using Kubernetes.
https://www.ardanlabs.com
Apache License 2.0
3.59k stars 649 forks source link

Why NewWithTx returns an interface instead of concrete type? #416

Closed nhaancs closed 2 months ago

nhaancs commented 2 months ago

https://github.com/ardanlabs/service/blob/f522e6646ac4836aabf29cea237baefdba23b720/business/domain/userbus/stores/userdb/userdb.go#L36C1-L37C1

func (s *Store) NewWithTx(tx sqldb.CommitRollbacker) (userbus.Storer, error) {

ardan-bkennedy commented 2 months ago

That method is implementing an interface where the return value can be any concrete value that implements the interface. We do this in the web package as well with the Encoder interface.

there are times where your API represents behavior that must be open to different concrete types because you will have unknown implementations.