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.

Reply via email to