Miguel
Miguel

Reputation: 53

NServiceBus - 2 Classes Using the same Saga, is that possible?

Normally I create 1 class that implements Saga<T>:

public class MyClass1 : Saga<MySagaData>;
{
            ………
}

MySagaData code:

[SagaIndex("ExternalCombinedIdentifier")]
[SagaIndex("MyOwnId")]
public class MySagaData: IContainSagaData
{
    public MySagaData()
    {
                            ……

    }


    public Guid Id { get; set; }
    public string Originator { get; set; }
    public string OriginalMessageId { get; set; }
    ……

}

Now I need to use the same Saga (Saga<MySagaData>) in a another class, let’s call it MyClass2.

If I implement MyClass2 like the following: public class MyClass2 : Saga<MySagaData> { ……… }

will the ACID properties of Saga<MySagaData> be present for MyClass1 and MyClass2 as if Saga<MySagaData> would just being used in 1 class? And in the ACID properties I include for example rollback a message handler and retry it in MyClass1, if another message handler for the other class (MyClass2) just commited changes to the MySagaData?

Upvotes: 0

Views: 193

Answers (1)

David Boike
David Boike

Reputation: 18625

It looks like you are trying to reuse the saga data class between two sagas. This is not a good idea.

Each saga should be logically independent. If you use the same class for saga data, the various persistence mechanisms used by NServiceBus will store them together and there's a good chance the wires could cross, resulting in the wrong data in the wrong saga.

Additionally, doing so would couple the sagas together. You would not be able to add things to one without affecting the other. This is a good place to apply SRP (Single Responsibility Principle) far ahead of DRY (Don't Repeat Yourself).

To make it easier to create saga data classes, you can inherit from ContainSagaData which has the three properties required by IContainSagaData already. This didn't work well several versions of NServiceBus ago (NHibernate persistence would create too many tables because of the inheritance, for example) but these issues are solved in recent versions of NServiceBus.

Upvotes: 2

Related Questions