Skip to content

Instantly share code, notes, and snippets.

@jjvdangelo
Last active August 29, 2015 13:56
Show Gist options
  • Save jjvdangelo/9224409 to your computer and use it in GitHub Desktop.
Save jjvdangelo/9224409 to your computer and use it in GitHub Desktop.
module CommandBus =
let createCommandRouter handlers =
let event = Event<Command>()
handlers |> List.iter(fun handler -> event.Publish |> Event.add handler)
let agent = MailboxProcessor.Start(fun inbox ->
let loop() = async {
let! cmd = inbox.Receive()
cmd |> event.Trigger
return! loop() }
loop())
{ new IRounter<Command> with
member __.Send (cmd: Command) =
cmd |> agent.Post }
type Issuer =
| System
| User of username: string
override this.ToString() =
match this with
| System -> "System" // Replace "System" with what ever you'd like
| User name -> name
type CommandData = { EntityId: Guid; Issuer: Issuer }
type Command = { Data: CommandData; Body: obj }
open Simple.Web.StructureMap
type DomainStartup() =
inherit StructureMapStartupBase()
let commandRouter =
[ MyDomain.createHandler1;
MyDomain.createHandler2;
MyDomain.createHandler3; ]
|> CommandBus.createCommandRouter
override __.Configure(config) =
config.For(typedefof<IRouter<Command>>).Singleton().Use(commandRouter) |> ignore
type IRouter<'a> =
abstract Send: 'a -> unit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment