Event Sourcing and CQRS Pattern for Angular App
This is a sample code for Event Sourcing and CQRS pattern. | |
* http://blog.aliencube.org/ko/2015/11/12/building-applications-on-cloud-with-event-sourcing-pattern-and-cqrs-pattern/ |
public async Task<bool> ProcessAsync(BaseEvent ev) | |
{ | |
return await this.OnProcessingAsync(ev); | |
} | |
protected override async Task<bool> OnProcessingAsync(BaseEvent ev) | |
{ | |
var stream = this._mapper.Map(ev as SalutationChangedEvent); | |
this._repository.Add(stream); | |
return await Task.FromResult(true); | |
} |
POST http://localhost:29289/api/events/salutation-changed |
[HttpPost] | |
[Route("salutation-changed")] | |
public virtual async Task<SalutationChangeResponse> SetSalutation([FromBody] SalutationChangeRequest request) | |
{ | |
var response = await this._service.ChangeSalutationAsync(request); | |
return response; | |
} |
public async Task<bool> ProcessEventsAsync(IEnumerable<BaseEvent> evs) | |
{ | |
var results = new List<bool>(); | |
using (var uow = this._uowm.CreateInstance<SampleDbContext>()) | |
{ | |
uow.BeginTransaction(); | |
try | |
{ | |
foreach (var ev in evs) | |
{ | |
var handlers = this.GetHandlers(ev); | |
foreach (var handler in handlers) | |
{ | |
var result = await handler.ProcessAsync(ev); | |
results.Add(result); | |
} | |
} | |
uow.Commit(); | |
} | |
catch | |
{ | |
uow.Rollback(); | |
results.Add(false); | |
throw; | |
} | |
} | |
return await Task.FromResult(results.TrueForAll(p => p)); | |
} |
public async Task<SalutationChangeResponse> ChangeSalutationAsync(SalutationChangeRequest request) | |
{ | |
var handler = this._handlers.SingleOrDefault(p => p.CanHandle(request)); | |
if (handler == null) | |
{ | |
return await Task.FromResult(default(SalutationChangeResponse)); | |
} | |
var ev = handler.CreateEvent(request) as SalutationChangedEvent; | |
PopulateBaseProperties(ev); | |
SalutationChangeResponse response; | |
try | |
{ | |
await this._processor.ProcessEventsAsync(new[] { ev }); | |
response = new SalutationChangeResponse() | |
{ | |
Data = new SalutationResponseData() | |
{ | |
Value = request.Value | |
} | |
}; | |
} | |
catch (Exception ex) | |
{ | |
... | |
} | |
return await Task.FromResult(response); | |
} |
/// <reference path="../../../Scripts/typings/angularjs/angular.d.ts" /> | |
/// <reference path="../../factories/salutationsFactory.ts" /> | |
/// <reference path="../../factories/replayViewFactory.ts" /> | |
"use strict"; | |
module app.angular.Directives { | |
import SalutationCollectionDataModel = angular.Models.SalutationCollectionDataModel; | |
import SalutationChangeRequestModel = angular.Models.SalutationChangeRequestModel; | |
export interface IUserSalutationScope extends ng.IScope { | |
model: angular.Models.SalutationCollectionDataModel; | |
change: Function; | |
} | |
export class UserSalutation implements ng.IDirective { | |
replace = true; | |
restrict = "EA"; | |
scope = {}; | |
templateUrl = "/App/components/userSalutation/userSalutation.html"; | |
link($scope: IUserSalutationScope, element: JQuery, attributes: ng.IAttributes) { | |
var $select = element.find("select"); | |
$select.on("change", () => { | |
var streamId = element.data("stream-id"); | |
var value = $scope.model.value; | |
$scope.change($select.attr("id"), $select.attr("name"), value, streamId); | |
}); | |
} | |
controller($scope: IUserSalutationScope, salutationsFactory: angular.Factories.SalutationsFactory, replayViewFactory: angular.Factories.ReplayViewFactory) { | |
$scope.model = new SalutationCollectionDataModel(); | |
salutationsFactory.getSalutations() | |
.success((response: angular.Models.SalutationResoponseModel) => { | |
$scope.model.salutations = response.data; | |
console.log($scope.model); | |
}); | |
$scope.change = (id, name, value, streamId) => { | |
var request = new SalutationChangeRequestModel(id, name, value, streamId); | |
salutationsFactory.postSalutationChange(request) | |
.success((response: angular.Models.SalutationChangeResponseModel) => { | |
replayViewFactory.setSalutation(response.data.value); | |
console.log(response); | |
}) | |
.error((response: angular.Models.SalutationChangeResponseModel) => { | |
console.log(response); | |
}); | |
} | |
} | |
} | |
} | |
angular.module("app") | |
.directive("userSalutation", () => new app.angular.Directives.UserSalutation()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment