|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: First Steps |
| 4 | +parent: How-To |
| 5 | +nav_order: 2 |
| 6 | +--- |
| 7 | + |
| 8 | +# First Steps |
| 9 | + |
| 10 | +OpenSleigh is intended to be flexible and developer-friendly. It makes use of Dependency Injection for its own initialization and the setup of the dependencies. |
| 11 | + |
| 12 | +## Configure Transport and Persistence |
| 13 | + |
| 14 | +The first step, once you have installed the [Core library](https://www.nuget.org/packages/OpenSleigh/), is to add OpenSleigh to the Services collection: |
| 15 | + |
| 16 | +```csharp |
| 17 | +Host.CreateDefaultBuilder(args) |
| 18 | + .ConfigureServices((hostContext, services) => { |
| 19 | + services.AddOpenSleigh(cfg =>{ ... }); |
| 20 | + }); |
| 21 | +``` |
| 22 | + |
| 23 | +OpenSleigh needs to be configured to point to a specific Transport bus and a Persistence mechanism for the Saga States: |
| 24 | + |
| 25 | +```csharp |
| 26 | +Host.CreateDefaultBuilder(args) |
| 27 | + .ConfigureServices((hostContext, services) => { |
| 28 | + services.AddOpenSleigh(cfg =>{ |
| 29 | + var rabbitSection = hostContext.Configuration.GetSection("Rabbit"); |
| 30 | + var rabbitCfg = new RabbitConfiguration(rabbitSection["HostName"], |
| 31 | + rabbitSection["UserName"], |
| 32 | + rabbitSection["Password"]); |
| 33 | + |
| 34 | + cfg.UseRabbitMQTransport(rabbitCfg); |
| 35 | + |
| 36 | + var mongoSection = hostContext.Configuration.GetSection("Mongo"); |
| 37 | + var mongoCfg = new MongoConfiguration(mongoSection["ConnectionString"], |
| 38 | + mongoSection["DbName"], |
| 39 | + MongoSagaStateRepositoryOptions.Default); |
| 40 | + |
| 41 | + cfg.UseMongoPersistence(mongoCfg); |
| 42 | + }); |
| 43 | + }); |
| 44 | +``` |
| 45 | + |
| 46 | +In this example, the system is configured to use RabbitMQ as message bus and MongoDB to persist the data. |
| 47 | + |
| 48 | +**IMPORTANT**: for detailed instructions on each Transport and Persistence mechanism, please refer to the library's documentation. |
| 49 | + |
| 50 | +## Adding a Saga |
| 51 | + |
| 52 | +A Saga is a simple class inheriting from the base [`Saga`](https://github.com/mizrael/OpenSleigh/blob/develop/src/OpenSleigh/Saga.cs) class: |
| 53 | + |
| 54 | +```csharp |
| 55 | +public record MyAwesomeSagaState { } |
| 56 | + |
| 57 | +public class MyAwesomeSaga : Saga |
| 58 | +{ |
| 59 | + private readonly ILogger<MyAwesomeSaga> _logger; |
| 60 | + |
| 61 | + public MyAwesomeSaga(ILogger<MyAwesomeSaga> logger) |
| 62 | + { |
| 63 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Dependency injection can be used to provide services to a Saga. |
| 69 | + |
| 70 | +Now, all you have to do is register and configure the Saga: |
| 71 | + |
| 72 | +```csharp |
| 73 | +services.AddOpenSleigh(cfg =>{ |
| 74 | + cfg.AddSaga<MyAwesomeSaga>(); |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +Sagas can also hold some _state_. In this case, we need to define its shape by creating a `class` or a `record` and setting it on the Saga: |
| 79 | + |
| 80 | +```csharp |
| 81 | +public record MySagaState |
| 82 | +{ |
| 83 | + public int Foo = 42; |
| 84 | + public string Bar = "71"; |
| 85 | +}; |
| 86 | + |
| 87 | +public class SagaWithState : Saga<MySagaState> { |
| 88 | + |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +Now you can register it this way: |
| 93 | + |
| 94 | +```csharp |
| 95 | +services.AddOpenSleigh(cfg =>{ |
| 96 | + cfg.AddSaga<SagaWithState, MySagaState>(); |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +The State can be accessed later on via `this.Context.State` and is also persisted automatically after a message is processed. For more details, check the [Handling Messages]({% link how-to/handling-messages.md %}) page. |
| 101 | + |
| 102 | +**IMPORTANT**: each Saga should have its own State class. Don't reuse State classes! |
0 commit comments