Wait a second! We said that IAmStartedByMessages<T>
tells the saga to create a new saga instance if the existing saga data is not found, but how does it know where to look for the saga data?
As it turns out, we need to give NServiceBus a bit of help with this, by telling it how to match message properties with saga data. Because the ConfigureHowToFindSaga
method is now abstract in 5.0, we have already created it, but now we have to fill in the details using the mapper
object it provides:
mapper.ConfigureMapping<CreateNewUserCmd>(msg => msg.EmailAddress) .ToSaga(data => data.EmailAddress);
The ConfigureMapping<TMessageType>
method accepts an Expression
(a specialized lambda expression that can be evaluated at runtime) that identifies a property on the incoming message to match with the saga data. Then, with the result, we call the ToSaga
method that accepts a similar expression pointing to the matching property in the saga data. So in essence, this...