Created
January 20, 2018 22:42
-
-
Save aramkoukia/6980ca0ddef3ba1e9be42abca0272c99 to your computer and use it in GitHub Desktop.
Product Command Handlers
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 Products.Common.Dto; | |
using Products.Service.MicroServices.Products.Commands; | |
using MicroServices.Common.Repository; | |
namespace Products.Service.MicroServices.Products.Handlers | |
{ | |
public class ProductCommandHandlers | |
{ | |
private readonly IRepository repository; | |
public ProductCommandHandlers(IRepository repository) | |
{ | |
this.repository = repository; | |
} | |
public void Handle(CreateProduct message) | |
{ | |
// Validation | |
ProductDto existingProduct = null; | |
// Process | |
var series = new Products.Domain.Product(message.Id, message.Name, message.Description, message.Price); | |
repository.Save(series); | |
} | |
public void Handle(AlterProduct message) | |
{ | |
var product = repository.GetById<Products.Domain.Product>(message.Id); | |
int committedVersion = message.OriginalVersion; | |
if (!String.Equals(product.Name, message.NewTitle, StringComparison.OrdinalIgnoreCase)) | |
{ | |
product.ChangeName(message.NewTitle, committedVersion++); | |
} | |
if (!String.Equals(product.Description, message.NewDescription, StringComparison.OrdinalIgnoreCase)) | |
{ | |
product.ChangeDescription(message.NewDescription, committedVersion++); | |
} | |
if (message.NewPrice != product.Price) | |
{ | |
product.ChangePrice(message.NewPrice, committedVersion); | |
} | |
repository.Save(product); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should ...
product.ChangePrice(message.NewPrice, committedVersion);
be...
product.ChangePrice(message.NewPrice, committedVersion++);