Hi all, this is my first post here. I’m reading Practical Object
Oriented Design in Ruby by Sandy Metz right now and loving it, but there
are
areas of ‘dependency injection’ that aren’t quite sticking.
What’s the best way to inject *many dependencies into an object? I’m
building a class that relies on 7 Aws::CloudSearchDomain::Client objects
in order to upload data to Amazon Web Services. Based on the type of
data to be uploaded, I need to select the specific AWS ‘Client’ object
used to upload that type of data. This is a simplified example of what
I’m doing at the moment:
class Uploader
def self.build
attributes = {}
# AWS Client objects are instantiated with a table 'endpoint' as the
argument
attributes[:songs] =
Aws::CloudSearchDomain::Client.new(‘http://songs…’)
attributes[:albums] =
Aws::CloudSearchDomain::Client.new(‘http://albums…’)
attributes[:artists] =
Aws::CloudSearchDomain::Client.new(‘http://artists…’)
…
new(attributes)
end
def initialize(attributes)
@songs_aws_client = attributes[:songs]
@albums_aws_client = attributes[:albums]
@artists_aws_client = attributes[:artists]
…
end
def upload_record(aws_domain:, data_to_upload:)
# Logic in here would select the corresponding ‘aws_client’ based on
# the ‘aws_domain’ argument passed in. It would then upload the
record,
# something like client.upload_documents(record)
end
…
end
I got the idea of using #build from
Porno.
For argument sake, what if I had 1000 different tables and 1000
different types of AWS ‘Client’ objects? Is there a better way to go
about this?