Database Reference
In-Depth Information
Using saga and NHibernate
We will walk through a modified example of a basic saga, originally from
ht-
tps://github.com/jkillingsworth/NServiceBus-BasicSagas
. However, this example has been
modified to use NHibernate, which uses a local SQL Express database. NHibernate was ad-
ded using some of the steps from
http://docs.particular.net/nservicebus/relational-
persistence-using-nhibernate
.
We also added logging using the NLog framework to log the
functionality as we go. The NHibernate ORM framework was chosen because it can con-
nect to a multitude of different databases using the same code, the difference being to the
different databases is the connection string in the
app.config
file.
To elaborate on this Saga example, there is a
MySaga
program that directs the messages
using message handlers that creates new messages to send and respond through the work-
flow. The saga object is persisting the message states to be used during these message
handlers. The saga persistence keeps track of the information that we defined to be saved in
a saga entity object. It is the state and session information of the message that we deem rel-
evant.
The saga acts as an anchor that we can persist as we orchestrate messages moving across
the bus. We can retrieve the instance of the saga associated with the message, update it, and
keep it stored, as even the original message morphs into different types of messages. The
following screenshot demonstrates this orchestration:
In this application, we sent
IAmStartedByMessages<SubmitRequestCommand>
from an
AppSubmittingRequests
application that is seen here as
Purchase Order
Requests
. It creates and submits
SubmitRequestCommand
that takes the data from this
message and creates a saga on the bus, along with a unique ID. It also sets a 60-second
timer that will send a timeout message from the bus once 60 seconds are over.