Created
January 20, 2018 23:52
-
-
Save aramkoukia/749a6ec9fea201bb17475f00494366d7 to your computer and use it in GitHub Desktop.
Product View
This file contains hidden or 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
| using System; | |
| using System.Collections.Generic; | |
| using Products.Common.Dto; | |
| using MicroServices.Common; | |
| using Products.Common.Events; | |
| using MicroServices.Common.Exceptions; | |
| using MicroServices.Common.Repository; | |
| namespace Products.ReadModels.Service.Views | |
| { | |
| public class ProductView : ReadModelAggregate, | |
| IHandle<ProductCreated>, | |
| IHandle<ProductDescriptionChanged>, | |
| IHandle<ProductNameChanged>, | |
| IHandle<ProductPriceChanged> | |
| { | |
| private const string displayFormat = "{1} ({0})"; | |
| private readonly IReadModelRepository<ProductDto> repository; | |
| public ProductView(IReadModelRepository<ProductDto> repository) | |
| { | |
| this.repository = repository; | |
| } | |
| public ProductDto GetById(Guid id) | |
| { | |
| try | |
| { | |
| return repository.Get(id); | |
| } | |
| catch | |
| { | |
| throw new ReadModelNotFoundException(id, typeof(ProductDto)); | |
| } | |
| } | |
| public IEnumerable<ProductDto> GetAll() | |
| { | |
| return repository.GetAll(); | |
| } | |
| public void Apply(ProductCreated e) | |
| { | |
| var dto = new ProductDto | |
| { | |
| Id = e.Id, | |
| Name = e.Name, | |
| Description = e.Description, | |
| Price = e.Price, | |
| Version = e.Version, | |
| DisplayName = string.Format(displayFormat, e.Name, e.Description), | |
| }; | |
| repository.Insert(dto); | |
| } | |
| public void Apply(ProductNameChanged e) | |
| { | |
| var product = GetById(e.Id); | |
| product.Name = e.NewName; | |
| product.Version = e.Version; | |
| product.DisplayName = string.Format(displayFormat, product.Name, product.Description); | |
| repository.Update(product); | |
| } | |
| public void Apply(ProductDescriptionChanged e) | |
| { | |
| var product = GetById(e.Id); | |
| product.Description = e.NewDescription; | |
| product.Version = e.Version; | |
| product.DisplayName = string.Format(displayFormat, product.Name, product.Description); | |
| repository.Update(product); | |
| } | |
| public void Apply(ProductPriceChanged e) | |
| { | |
| var product = GetById(e.Id); | |
| product.Price = e.NewPrice; | |
| product.Version = e.Version; | |
| repository.Update(product); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment