Let’s say you have a hypothetical Repo
class, and you want one per database table, something like this:
user_repo = Repo.new(db, :users, has_many: :tweets)
tweet_repo = Repo.new(db, :tweets, has_one: :user)
foo_repo = Repo.new(db, :foos)
And you want each of those to be registered with a container.
Autoloading won’t work, because it expects a unique class per object.
You could just register them all in a file, like app.rb
or something:
class App < Dry::Component::Container
namespace 'repos' do
register 'user' { Repo.new(resolve('db'), :users, has_many: :tweets) }
register 'tweet' { Repo.new(resolve('db'), :tweets, has_one: :user) }
register 'foo' { Repo.new(resolve('db'), :foos) }
end
end
Is that the preferred way to do this – just one file where you register everything that can’t be autoloaded? Or would you put them into seperate files, and autoload them somehow? Or maybe loop over some config values and create them all that way.
I’m interested to hear your approach.