Using one transaction with many containers


Good day!
I have a part of a legacy code, where transactions declared like that:

transaction = Dry.Transaction(container) do
  step :one, ""
  step :two, "operations.two"

But after updating it doesn’t work.
As described there I should create a transaction-class and include my container as a module. But in my case, I have several containers with different implementations of same methods. And one transaction for starting these methods in the same queue. So is there any way to pass container to transaction dynamically, or I should just create many identical transactions with different containers?


@fedorkk you can define a transaction without a container. Or you can inject (override in other words) steps, have a look

class MyTransaction
  include Dry::Transaction

  step :one
  step :two

container_a = {
  one: -> v { Dry::Monads.Right(v + '/one a') },
  two: -> v { Dry::Monads.Right(v + '/two a') }

container_b = {
  one: -> v { Dry::Monads.Right(v + '/one b') },
  two: -> v { Dry::Monads.Right(v + '/two b') }

txn_a =
txn_b =
[1] pry(main)> txn_a.('foo')
=> Right("foo/one a/two a")
[2] pry(main)> txn_b.('foo')
=> Right("foo/one b/two b")