Auto wiring

ToJans edited this page Nov 14, 2011 · 4 revisions

The plumbing is done for you by the framework based on some conventions/assumptions.

When I call the constructor var bus = new ScritchyBus() - typically upon starting your app - this is what happens in the HandlerRegistry:

I scan all the inmem assemblies for classes & properties:

  • First I find all ARs (public classes derived from Scritchy.Domain.AR) (class BankAccount)
  • Second I find all the commands (public void methods in the AR's where there exists a class with the same name as the method that contain a property with the same name as the AR+"Id" (RegisterBankAccount {BankAccountId,OwnerName})
  • Then I find all public void methods on all public classes which start with "On" (OnBankAccountRegistered)
  • Then I look if I can find a public class named like the method, but without the "On" prefix (class BankAccountRegistered), which would be a possible event.
  • Now I have a registry of all AR's, events, commands and handlers in a registry.

Note: event handlers should be stateless in this framework, but support IOC through the bus construction (except for the AR ones of course).

Note 2: there might be some false positives on the events & event handlers, but these event handlers will never be called as the commands will only generate domain events.

When you now call

bus.RunCommand(new RegisterBankAccount {BankAccountId="account/1",OwnerName="blah"})
  • An instance of BankAccount is being loaded with ID = "account/1"
  • All previous events from the eventstore for the BankAccount with this Id are being replayed on this instance, so we have an up to date snapshot.
  • The command is passed in the command method
  • The generated events get stored in the eventstore (which is inmem by default, or using a database if you set the connectionstring "eventstore" in the web/app.config)
  • If you did not add the DoNotAppyEvents = true parameter in the bus constructor, the different handler types are loaded one by one, and the On"Eventname" methods will be executed for every single handler type and event
  • If you did add the DoNotAppyEvents = true in the bus constructor, you can call bus.ApplyNewEventsToAllHandlers() whenever you feel like updating your views
  • There is both a synchronous and an asynchronous method available for both RunCommand and ApplyNewEventsToAllHandlers with a callback (might not be production-ready ;) )