<commit_before>Defining Sagas using the Saga State Machine
===========================================

Sagas are one of the more powerful features in MassTransit, allowing complex state and behavior to be
defined using a fluent syntax.


What is a saga?
---------------

A saga is a long-lived transaction managed by a coordinator. Sagas are initiated by an event, sagas orchestrates events, and sagas maintain the state of the overall transaction. They are designed to manage the complexity of a distributed transaction without locking and immediate consistency. They manage state, and track compensations that are required if a partial failure occurs.

We didn’t create it, we learned it from:

Cornell paper: http://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf

<NAME>’s book chapter: http://www.rgoarchitects.com/Files/SOAPatterns/Saga.pdf


Defining a Saga
---------------

There are two ways to define a saga using MassTransit. The first approach is similar to creating a _consumer_
and uses interfaces on a class to define the messages that can initiate, orchestrate, or be observed by a saga
instance. The second approach creates a state machine from a class definition that defines the events, states,
and actions that make up the state machine.


Defining a Saga Using the State Machine Syntax
----------------------------------------------

To define a saga using the state machine, a class that inherits from SagaStateMachine must be created.


.. sourcecode:: csharp
    :linenos:
    
    public class AuctionSaga : 
        SagaStateMachine<AuctionSaga>,
        ISaga
    {
        static CombineSaga()
        {
            Define(() =>
            {
                // the state machine behavior is defined here
            });
        }
        public Guid CorrelationId { get; set; }
        public IServiceBus Bus { get; set; }<commit_msg>Fixed static ctor name for AuctionSaga<commit_after>Defining Sagas using the Saga State Machine
===========================================

Sagas are one of the more powerful features in MassTransit, allowing complex state and behavior to be
defined using a fluent syntax.


What is a saga?
---------------

A saga is a long-lived transaction managed by a coordinator. Sagas are initiated by an event, sagas orchestrates events, and sagas maintain the state of the overall transaction. They are designed to manage the complexity of a distributed transaction without locking and immediate consistency. They manage state, and track compensations that are required if a partial failure occurs.

We didn’t create it, we learned it from:

Cornell paper: http://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf

Arnon Rotem-Gal-Oz’s book chapter: http://www.rgoarchitects.com/Files/SOAPatterns/Saga.pdf


Defining a Saga
---------------

There are two ways to define a saga using MassTransit. The first approach is similar to creating a _consumer_
and uses interfaces on a class to define the messages that can initiate, orchestrate, or be observed by a saga
instance. The second approach creates a state machine from a class definition that defines the events, states,
and actions that make up the state machine.


Defining a Saga Using the State Machine Syntax
----------------------------------------------

To define a saga using the state machine, a class that inherits from SagaStateMachine must be created.


.. sourcecode:: csharp
    :linenos:
    
    public class AuctionSaga : 
        SagaStateMachine<AuctionSaga>,
        ISaga
    {
        static AuctionSaga()
        {
            Define(() =>
            {
                // the state machine behavior is defined here
            });
        }
        public Guid CorrelationId { get; set; }
        public IServiceBus Bus { get; set; }
{'fraction_non_alphanumeric': 0.10843692144935202, 'fraction_numerical': 0.0037027241470510446, 'mean_word_length': 2.808660624370594, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4011604', 'n_tokens_mistral': 989, 'n_tokens_neox': 915, 'n_words': 458}