Dependency Injection from config file

Written by
reddit
Link to Post

https://www.reddit.com/r/golang/comments/b0efka/dependency_injection_from_config_file/ by 

I’ve been reading around a bit about Dependency Injection in Go tonight, and especially about wire, which does it by code generation.

What I’ve not seen is anything that does it from a config file instead of from Go code. This is surprising to me since, from my Java background, I’m most used to DI using Spring, which itself was traditionally powered by XML files.

I’m envisioning something like this:

{ "database": { "package": "github.com/sazzer/project/database", "factory": "New" }, "userRepository": { "package": "github.com/sazer/project/users", "factory": "NewUserRepository", "parameters": [ {"type": "bean", "ref": "database"} ] }, "userHandler": { "package": "github.com/sazer/project/users", "factory": "NewUserHandler", "parameters": [ {"type": "bean", "ref": "userRepository" ] } } 

This could then generate appropriate Go code to:

I’ve no idea how to do this – yet – but it doesn’t seem unreasonable to me, and possibly a lot cleaner than some of the options that I’ve seen.

Does this seem completely crazy? Or has this already been done somewhere?

submitted by /u/sazzer
[link] [comments]

Article Tags:
· ·
Article Categories:
reddit

Leave a Reply