Sounds reasonable. Though destructuring a component (I was guessing "db" etc were fields of this particular component) isn't the same as passing the whole system. I'm guessing that you have a lot bigger system in mind than these few commands. I don't have experience with that approach. I'm curious to see what you'll end up with.
Cheers, Jeroen On Wed, Feb 25, 2015 at 2:01 PM, Colin Yates <[email protected]> wrote: > Yes, I did consider that but didn't like the idea of passing the > system around. Also handle-command would need access to the system as > well. > > I am wondering whether simply having a 'registry' component which > handlers register themselves with (e.g. a map of {predicate handler}) > isn't a way forward and not using defmulti... > > On 25 February 2015 at 12:51, Jeroen van Dijk > <[email protected]> wrote: > > So I guess another question is where "handle-command" is being called. > What > > about providing the component as argument to the multimethod (there are > > other approaches too depending on your need). So: > > > > (defmulti handle-command(fn [component command] (first command))) > > > > (defmethod handle-command :add-customer [{:keys [db eventstore} [[_ > > customer]]] > > ... ) > > > > Isn't something like this enough? > > > > Jeroen > > > > On Wed, Feb 25, 2015 at 1:34 PM, Colin Yates <[email protected]> > wrote: > >> > >> Hi Jeroen - sure. > >> > >> I have a gateway which can handle a number of 'commands', let's say: > >> > >> AddCustomerCommand > >> ExportQueryCommand > >> > >> to handle the AddCustomerCommand I need access to a database and an > >> eventstore. To handle ExportQueryCommand I need access to the > >> database, a PDF service and so on. > >> > >> One way of achieving this is (assuming commands are of the form > >> [:command-discriminator payload]: > >> > >> (defmulti handle-command first) > >> > >> (defn add-customer [db eventstore customer] > >> ...) > >> > >> (defrecord AddCustomerComponent [db eventstore] > >> ... > >> (start [this] (defmethod handle-command :add-customer [[_ customer]] > >> (add-customer db eventstore customer))..) > >> > >> (defn export-reports [db pdf-generator] > >> ...) > >> > >> (defrecord ExportQueryCommand [db pdf-generator] > >> ... > >> (start [this] (defmethod handle-command :export-query [[_]] > >> (export-query db pdf-generator))..) > >> > >> and so on. > >> > >> Fundamentally my question is one of dispatching where the target of > >> the dispatch requires collaborators that aren't part of the dispatch > >> payload. > >> > >> Does that clarify? > >> > >> On 25 February 2015 at 12:11, Jeroen van Dijk > >> <[email protected]> wrote: > >> > I would try to stay away from any nested def's in components (but also > >> > in > >> > other functions). I'm using component a lot and I never had the need > for > >> > this. > >> > > >> > Maybe you can elaborate on why you think you need a multimethod inside > >> > the > >> > component? Maybe a full example? > >> > > >> > Cheers, > >> > Jeroen > >> > > >> > > >> > > >> > On Wed, Feb 25, 2015 at 12:08 PM, Colin Yates <[email protected]> > >> > wrote: > >> >> > >> >> Hi, > >> >> > >> >> I have a number of commands flying around which need to be handled. > >> >> defmulti/defmethod seem a nice answer. The problem is that each > command > >> >> handler will need different collaborators (those with long memories > >> >> will > >> >> realise this isn't new ground here ;)). > >> >> > >> >> I want to do something like: > >> >> > >> >> (ns one) > >> >> (defmulti handle first) > >> >> > >> >> (ns two) > >> >> (defrecord CommandAHandler [collab-1 collab-2] > >> >> component/Lifecycle > >> >> (start [this] > >> >> (defmethod handle :command-a .... > >> >> (collab-1 ...))) > >> >> > >> >> I realise that the lifecycle of defmethod doesn't match the component > >> >> (i.e. stopping can't unregister the defmethod), but my main > reservation > >> >> is > >> >> that a nested defmethod feels a bit weird. Has anyone done anything > >> >> like > >> >> this before? > >> >> > >> >> Thanks! > >> >> > >> >> -- > >> >> You received this message because you are subscribed to the Google > >> >> Groups "Clojure" group. > >> >> To post to this group, send email to [email protected] > >> >> Note that posts from new members are moderated - please be patient > with > >> >> your first post. > >> >> To unsubscribe from this group, send email to > >> >> [email protected] > >> >> For more options, visit this group at > >> >> http://groups.google.com/group/clojure?hl=en > >> >> --- > >> >> You received this message because you are subscribed to the Google > >> >> Groups > >> >> "Clojure" group. > >> >> To unsubscribe from this group and stop receiving emails from it, > send > >> >> an > >> >> email to [email protected]. > >> >> For more options, visit https://groups.google.com/d/optout. > >> > > >> > > >> > -- > >> > You received this message because you are subscribed to the Google > >> > Groups "Clojure" group. > >> > To post to this group, send email to [email protected] > >> > Note that posts from new members are moderated - please be patient > with > >> > your > >> > first post. > >> > To unsubscribe from this group, send email to > >> > [email protected] > >> > For more options, visit this group at > >> > http://groups.google.com/group/clojure?hl=en > >> > --- > >> > You received this message because you are subscribed to the Google > >> > Groups > >> > "Clojure" group. > >> > To unsubscribe from this group and stop receiving emails from it, send > >> > an > >> > email to [email protected]. > >> > For more options, visit https://groups.google.com/d/optout. > >> > >> -- > >> You received this message because you are subscribed to the Google > >> Groups "Clojure" group. > >> To post to this group, send email to [email protected] > >> Note that posts from new members are moderated - please be patient with > >> your first post. > >> To unsubscribe from this group, send email to > >> [email protected] > >> For more options, visit this group at > >> http://groups.google.com/group/clojure?hl=en > >> --- > >> You received this message because you are subscribed to the Google > Groups > >> "Clojure" group. > >> To unsubscribe from this group and stop receiving emails from it, send > an > >> email to [email protected]. > >> For more options, visit https://groups.google.com/d/optout. > > > > > > -- > > You received this message because you are subscribed to the Google > > Groups "Clojure" group. > > To post to this group, send email to [email protected] > > Note that posts from new members are moderated - please be patient with > your > > first post. > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > > http://groups.google.com/group/clojure?hl=en > > --- > > You received this message because you are subscribed to the Google Groups > > "Clojure" group. > > To unsubscribe from this group and stop receiving emails from it, send an > > email to [email protected]. > > For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
