public void MakeAnElection(MakeAnElection command, IBenefitsReadModel benefitsService) { //The method that handles the command is the ONLY place to do any validation //If this method thinks all is well, then applying the change MUST succeed. //The Apply(...) method can not fail now - the command has been deemed to be //valid and the state transition can now go ahead. if (ElectionAmountBreachsLimitForPlanYear(command.ElectionAmount.Dollars(), benefitsService, command.PlanYearBenefitId)) { throw new DomainException( string.Format("The new election amount {0} exceeds the annual limit for the plan year benefit.", command.ElectionAmount.Dollars())); } ApplyEvent(new ElectionMadeEvent { AdministratorCode = command.AdministratorCode, CompanyCode = command.CompanyCode, ParticipantId = command.ParticipantId, PlanYearBenefitId = command.PlanYearBenefitId, ElectionAmount = command.ElectionAmount, ElectionReason = command.ElectionReason, PerPayPeriodAmount = 0.00m, Id = command.Id, }, @event => _state.Apply(@event)); }
public void WhenIIssueACreateElectionCommand() { //setup the "bus" components m_commandDispatcher = new CommandDispatcher(); m_eventPublisher = new EventDispatcher(); //register the command handler var repository = MockRepository.GenerateStub <IRepository <Election> >(); m_commandDispatcher.Register(new MakeAnElectionCommandHandler(repository, null)); //register the event handler m_eventPublisher.RegisterHandler <ElectionMadeEvent>(@event => m_electionCreatedEvent = @event); //wire-up the domain event to the event publisher DomainEvents.Register <ElectionMadeEvent>(@event => m_eventPublisher.Publish(@event)); //create and send the command var command = new MakeAnElection { AdministratorCode = "AdmCode", CompanyCode = "CoCode", ParticipantId = "12345", ElectionAmount = 1000, ElectionReason = "election reason", }; m_commandDispatcher.Dispatch <MakeAnElection>(command); Assert.Pass(); }
//private readonly IDispatchCommandsAndWaitForResponse m_commandDispatcher; public ElectionModule(IReadModelFacade readModel) : base("/elections") { //initialize stuff //m_commandDispatcher = commandDispatcher; var bus = ServiceBusFactory.New(configurator => { configurator.UseRabbitMq(); configurator.ReceiveFrom("rabbitmq://localhost/command_request_queue"); }); //define routes Get["/"] = parameters => { //get non-terminated elections for this participant from our read model //note: we can filter (only get un-terminated elections) on the read model side so that we do not send back //a load of election records that we discard here var elections = readModel.GetElectionsForParticipant(Participantid) .Where(election => election.IsTerminated == false) .ToList(); var electionsModel = new ElectionsModel { Elections = elections, ShowElectionDetail = false, SelectedElection = null, }; return(View["Elections", electionsModel]); }; Get["/{electionid}"] = parameters => { var elections = readModel.GetElectionsForParticipant(Participantid) .Where(election => election.IsTerminated == false) .ToList(); var selectedElection = elections.FirstOrDefault(election => election.Id == parameters.electionid); var electionsModel = new ElectionsModel { Elections = elections, ShowElectionDetail = selectedElection != null, SelectedElection = selectedElection, }; return(View["Elections", electionsModel]); }; Get["/newelection"] = paramerters => View["NewElection"]; Post["/newelection"] = parameters => { MakeAnElection command = this.Bind(); command.PlanYearBenefitId = Guid.NewGuid().ToString(); command.Id = Guid.NewGuid().ToString(); bool commandFailed = false; var model = new ErrorModel { LinkAddress = "/elections/newelection" }; bus.PublishRequest(command, configurator => { configurator.Handle <CommandResponse>( cmdResponse => { if (cmdResponse.CommandStatus != CommandStatusEnum.Succeeded) { model.HasError = true; model.ErrorMessage = cmdResponse.Message; commandFailed = true; } }); configurator.HandleTimeout(TimeSpan.FromSeconds(30), () => { model.HasError = true; model.ErrorMessage = "Your request timed out."; commandFailed = true; }); }); //m_commandDispatcher.Dispatch(command, // cmdResponse => // { // if (cmdResponse.CommandStatus != // CommandStatusEnum.Succeeded) // { // model.HasError = true; // model.ErrorMessage = cmdResponse.Message; // commandFailed = true; // } // }, // () => // { // model.HasError = true; // model.ErrorMessage = "Your request timed out."; // commandFailed = true; // }); if (commandFailed) { return(View["/errorview", model]); } return(Response.AsRedirect("/elections")); //why cant i just return a View here? Why redirect? }; Get["/terminate/{electionid}"] = parameters => { var terminateCommand = new TerminateElection { ElectionId = parameters.electionid, TerminationDate = DateTime.Now, }; bool commandFailed = false; var model = new ErrorModel { LinkAddress = "/elections" }; bus.PublishRequest(terminateCommand, configurator => { configurator.Handle <CommandResponse>( cmdResponse => { if (cmdResponse.CommandStatus != CommandStatusEnum.Succeeded) { model.HasError = true; model.ErrorMessage = cmdResponse.Message; commandFailed = true; } }); configurator.HandleTimeout( TimeSpan.FromSeconds(30), () => { model.HasError = true; model.ErrorMessage = "Your request timed out."; commandFailed = true; }); }); //m_commandDispatcher.Dispatch(terminateCommand, // response => // { // if (response.CommandStatus != CommandStatusEnum.Succeeded) // { // model.HasError = true; // model.ErrorMessage = response.Message; // commandFailed = true; // } // }, // () => // { // model.HasError = true; // model.ErrorMessage = "Your request timed out."; // commandFailed = true; // }); //return Response.AsRedirect("/elections"); if (commandFailed) { return(View["/errorview", model]); } return(Response.AsRedirect("/elections")); //why cant i just return a View here? Why redirect? }; }
public ActionResult NewHsa(MakeAnElection command, FormCollection collection) { return(View()); }