Skip to content
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

Dependency injection is neither type-safe nor intuitive #108

Open
thelissimus-work opened this issue Feb 6, 2025 · 0 comments
Open

Dependency injection is neither type-safe nor intuitive #108

thelissimus-work opened this issue Feb 6, 2025 · 0 comments

Comments

@thelissimus-work
Copy link
Contributor

Our in-house dependency injection framework has some flaws with the order of initialization and inter-module dependencies. Take a look at this example from the PR #107:

Doesn't work:

app.RegisterServices(
	services.NewEmbeddingService(app),
	services.NewDialogueService(persistence.NewDialogueRepository(), app),
)

Fails with:

go run cmd/migrate/main.go up
panic: service EmbeddingService not found

goroutine 1 [running]:
github.com/iota-uz/iota-sdk/pkg/application.(*application).Service(0x10635f220?, {0x10635f220?, 0x140003ee560?})
        /iota-sdk/pkg/application/application.go:218 +0xe0
github.com/iota-uz/iota-sdk/modules/bichat/services.NewDialogueService({0x1065b41b8, 0x1071fc8a0}, {0x1065c11a8, 0x140001c2400})
        /iota-sdk/modules/bichat/services/dialogue_service.go:42 +0x80
github.com/iota-uz/iota-sdk/modules/bichat.(*Module).Register(0x1400016e600?, {0x1065c11a8, 0x140001c2400})
       /iota-sdk/modules/bichat/module.go:34 +0xec
github.com/iota-uz/iota-sdk/modules.Load(...)
        /iota-sdk/modules/load.go:24
github.com/iota-uz/iota-sdk/pkg/commands.Migrate({0x107192d20, 0x5, 0x107076a58?})
        /iota-sdk/pkg/commands/migrate_command.go:35 +0x170
main.main()
        /iota-sdk/cmd/migrate/main.go:9 +0x34
exit status 2

Works (kinda):

app.RegisterServices(
	services.NewEmbeddingService(app),
)
app.RegisterServices(
	services.NewDialogueService(persistence.NewDialogueRepository(), app),
)

Fails with:

go run cmd/migrate/main.go up
panic: ERROR: relation "users" does not exist (SQLSTATE 42P01) handling infrastructure/persistence/schema/bichat-schema.sql

goroutine 1 [running]:
main.main()
        /iota-sdk/cmd/migrate/main.go:11 +0x50
exit status 2

As you can see there can even be implicit dependencies which needs to be annotated by hand (meaning we can't infer this using just Go's type system).

This is not intuitive, nor type-safe, which makes it flawed and leads to future frustrations and wasted time debugging.

The problem is - dependencies really are a graph but we're treating them as some linear structure (combination of a Type->Serivce map + varargs). It would be okay if services didn't depend on each other, but that is not the case and really is not possible in our program of this scale and complexity. Also, I was not able to find a way to specify the order of dependencies to resolve the above issue with SQL schema dependency of dialogs to users. Even though .NewModules are called in the correct order the initialization isn't in that order:

var (
	BuiltInModules = []application.Module{
		core.NewModule(),
		bichat.NewModule(),
		finance.NewModule(),
		warehouse.NewModule(),
		crm.NewModule(),
	}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant