These are chat archives for dry-rb/chat

28th
Feb 2018
Jonah
@jonahx
Feb 28 2018 02:00

Quick questons about dry-transaction. From the docs:

Each operation shouldn’t accumulate state, instead it should receive an input and return an output without causing any side-effects.

What about the persist example — doesn’t that operation have the side effect of saving to a DB?
_

Jonah
@jonahx
Feb 28 2018 02:25
Also, small point of clarification from the dry-struct docs: Is the schema constructor type the one appropriate for creating objects from user submitted form values. Eg, in the process step of a dry-transaction?
Tim Riley
@timriley
Feb 28 2018 03:05
@jonahx we’re talking there about side effects for the ruby execution context
I really need to change those dry-transaction examples
I wouldn’t recommend breaking transactions down into such fine grained steps
start by having one operation object handling your form input
think about transactions for joining together bigger, higher-level pieces of functionality
I would not construct a struct direct from user input
I would run it through a dry-validation schema and with with the resulting data
Jonah
@jonahx
Feb 28 2018 03:09
so validation step first on raw user input, then creation of schema type dry-struct using validated input?
Tim Riley
@timriley
Feb 28 2018 03:10
require "my_app/import"
require_relative "form_schema"

module MyApp
  module Posts
    class Create < Dry::Transaction::Operation
      include Import["post_repo"]

      def call(params)
        validation = FormSchema.(params)

        if validation.success?
          Success(post_repo.create(validation.to_h))
        else
          Failure(validation)
        end
      end
    end
  end
end
I don’t think something like that is worth breaking up into separate transaction steps
that’d make for super fine-grained steps, it makes no sense to me
I mean, I guess you can make them steps based on individual instance methods within the transaction class, as opposed to step operations coming from the container
Jonah
@jonahx
Feb 28 2018 03:12
@timriley in that example is the formschema returning a dry-struct object?
Tim Riley
@timriley
Feb 28 2018 03:15
no
it accepts data, returns data
in this case, a hash either end
Jonah
@jonahx
Feb 28 2018 03:16
hmm… yeah. i was under the impression that we were encouraged to use dry-struct based domain objects for passing into the repo, and getting out of the repo. is that mistaken?
Tim Riley
@timriley
Feb 28 2018 03:16
passing plain data to e.g. create methods on a repo is fine
repos should return struct objects
rom-rb repos will do that for you automatically
(n.b. they don’t have to return struct objects, sometimes you may not want that)
but at this point you’re still just dealing with user input data
it’s fine for it to be data still
at that point it’s not a domain entity yet
it’s just data
it’s not a “post”
it becomes a post once you’ve created it
or later queried for it again
Jonah
@jonahx
Feb 28 2018 03:18
sure, but a domain object could represent a validated form submission
essentially, a datatype for the parameter of create
Tim Riley
@timriley
Feb 28 2018 03:18
sure, if that’s what you want
but in my experience that kind of thing is not really helpful/needed
Jonah
@jonahx
Feb 28 2018 03:19
maybe overkill for most cases
ok, cool. i think my understanding of the recommended way to use all the libraries is finally congealing. i’m sure i’ll have some more questions over the next weeks, but thanks for you time — that was helpful
Tim Riley
@timriley
Feb 28 2018 03:20
np!