These are chat archives for dry-rb/chat
this gem allows you to make use of the dependency inversion principle, which would normally be ignored in idiomatic Ruby libraries in favour of hard-coded dependencies and/or wide interfaces.
what are wide interfaces?
Would it be fair to say that this:
Import = Dry::AutoInject(MyContainer) class CreateUser include Import["users_repository"] def call(user_attrs) users_repository.create(user_attrs) end end
is syntactic sugar for this:
require 'users_repo' class CreateUser attr_reader :users_repo def initialize(users_repo: UsersRepo.new) @users_repo = users_repo end def call(user_attrs) users_repo.create(user_attrs) end end
Are there benefits beside the removal of boilerplate?
UserRepo, and use that in 6 places, do you really want make the same change 6 times rather than having a single place to handle construction?
CreateArticleis exactly what i’ve been doing for the last few months. i really like that method.
why does injecting deps manually cause that?the wide interfaces issue was referring to "idiomatic" Ruby, in which DI/composition seems to be largely ignored
PodRequest = Struct.new(:a, :b) class ValidatePodRequest # class to avoid passing pod_request inside call ValidatePodRequest#call # ie, to avoid: # # methA(pod_request) # methB(pod_request) # class Call < SimpleDelegator def initialize(validate_pr, pod_request) super(validate_pr) @pod_request = pod_request end def call methA methB # imagine more methods we don't have to pass pod_request to... end private def methA puts dep1 puts @pod_request.a end def methB puts dep2 puts @pod_request.b end end attr_reader :dep1, :dep2 def initialize( dep1: "Some Dep 1", dep2: "Some Dep 2" ) @dep1 = dep1 @dep2 = dep2 end def call(pod_request) Call.new(self, pod_request).call end end pr = PodRequest.new('aa', 'bb') ValidatePodRequest.new.call(pr)
# ./app/transactions/operations/global_container.rb:29:in `new' # ./app/transactions/operations/global_container.rb:29:in `block (4 levels) in <class:GlobalContainer>' # /home/funky/.gem/ruby/2.5.1/gems/dry-container-0.6.0/lib/dry/container/item.rb:29:in `call' # /home/funky/.gem/ruby/2.5.1/gems/dry-container-0.6.0/lib/dry/container/resolver.rb:25:in `call' # /home/funky/.gem/ruby/2.5.1/gems/dry-container-0.6.0/lib/dry/container/mixin.rb:112:in `resolve' # /home/funky/.gem/ruby/2.5.1/gems/dry-container-0.6.0/lib/dry/container/mixin.rb:125:in `' # /home/funky/.gem/ruby/2.5.1/gems/dry-transaction-0.13.0/lib/dry/transaction/operation_resolver.rb:10:in `block (4 levels) in initialize' # /home/funky/.gem/ruby/2.5.1/gems/dry-transaction-0.13.0/lib/dry/transaction/operation_resolver.rb:8:in `fetch' # /home/funky/.gem/ruby/2.5.1/gems/dry-transaction-0.13.0/lib/dry/transaction/operation_resolver.rb:8:in `block (3 levels) in initialize' # /home/funky/.gem/ruby/2.5.1/gems/dry-transaction-0.13.0/lib/dry/transaction/operation_resolver.rb:7:in `map' # /home/funky/.gem/ruby/2.5.1/gems/dry-transaction-0.13.0/lib/dry/transaction/operation_resolver.rb:7:in `block (2 levels) in initialize'