Created
May 5, 2012 10:29
-
-
Save di97mni/2601398 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MTTest | |
{ | |
public class CustomerRegistrationSaga : | |
SagaStateMachine<CustomerRegistrationSaga>, | |
ISaga | |
{ | |
//Using internal state instead of Combine | |
private bool isSettingsCreated; | |
private bool isUserCreated; | |
static CustomerRegistrationSaga() | |
{ | |
Define(() => | |
{ | |
Correlate(Initialised).By((saga, message) => saga.SystemId == message.SystemId); | |
Correlate(SettingsCreated).By((saga, message) => saga.SystemId == message.SystemId); | |
Correlate(UserCreated).By((saga, message) => saga.SystemId == message.SystemId); | |
Correlate(SystemActivated).By((saga, message) => saga.SystemId == message.SystemId); | |
Initially( | |
When(Initialised) | |
.Then((saga, message) => saga.SystemId = message.SystemId) | |
.Then((saga, message) => saga.Bus.Publish(new CreateSettingsCommand {SystemId = message.SystemId})) | |
.Then((saga, message) => saga.Bus.Publish(new CreateUserCommand {SystemId = message.SystemId})) | |
.TransitionTo(WaitingForCustomerAndSettings)); | |
During(WaitingForCustomerAndSettings, | |
When(SettingsCreated) | |
.Then((saga, message) => | |
{ | |
saga.isSettingsCreated = true; | |
saga.RaiseEventIfUserAndSettingsCreated(); | |
}), | |
When(UserCreated) | |
.Then((saga, message) => | |
{ | |
saga.isUserCreated = true; | |
saga.RaiseEventIfUserAndSettingsCreated(); | |
}), | |
//Replaced Combine() with this | |
When(SettingsAndUserCreated) | |
.Then((saga, message) => saga.Bus.Publish(new ActivateSystemCommand())) | |
.TransitionTo(WaitingForSystemActivation)); | |
During( | |
WaitingForSystemActivation, | |
When(SystemActivated) | |
.Complete()); | |
}); | |
} | |
public CustomerRegistrationSaga(Guid correlationId) | |
{ | |
CorrelationId = correlationId; | |
} | |
public Guid SystemId { get; set; } | |
public static State Initial { get; set; } | |
public static State WaitingForCustomerAndSettings { get; set; } | |
public static State WaitingForSystemActivation { get; set; } | |
public static State Completed { get; set; } | |
public static Event<ClientSystemCreatedEvent> Initialised { get; set; } | |
public static Event<SettingsCreatedEvent> SettingsCreated { get; set; } | |
public static Event<UserCreatedEvent> UserCreated { get; set; } | |
public static Event<SystemActivatedEvent> SystemActivated { get; set; } | |
public static Event SettingsAndUserCreated { get; set; } | |
#region ISaga Members | |
public Guid CorrelationId { get; set; } | |
public IServiceBus Bus { get; set; } | |
#endregion | |
private void RaiseEventIfUserAndSettingsCreated() | |
{ | |
if (isSettingsCreated && isUserCreated) | |
{ | |
RaiseEvent(SettingsAndUserCreated); | |
} | |
} | |
} | |
public class ClientSystemCreatedEvent | |
//: CorrelatedBy<Guid> | |
{ | |
public Guid SystemId { get; set; } | |
//public Guid CorrelationId { get; set; } | |
} | |
public class UserCreatedEvent | |
//: CorrelatedBy<Guid> | |
{ | |
public Guid SystemId { get; set; } | |
//public Guid CorrelationId { get; set; } | |
} | |
public class SettingsCreatedEvent | |
//: CorrelatedBy<Guid> | |
{ | |
public Guid SystemId { get; set; } | |
//public Guid CorrelationId { get; set; } | |
} | |
public class SystemActivatedEvent | |
//: CorrelatedBy<Guid> | |
{ | |
public Guid SystemId { get; set; } | |
//public Guid CorrelationId { get; set; } | |
} | |
public class ActivateSystemCommand | |
{ | |
public Guid SystemId { get; set; } | |
} | |
public class CreateUserCommand | |
{ | |
public Guid SystemId { get; set; } | |
} | |
public class CreateSettingsCommand | |
{ | |
public Guid SystemId { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment