Created
January 20, 2018 23:07
-
-
Save aramkoukia/83db7f6d72ff0d1c78b52904fee068bb to your computer and use it in GitHub Desktop.
Product Domain Class
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.Events; | |
using MicroServices.Common; | |
namespace Products.Service.MicroServices.Products.Domain | |
{ | |
public class Product : Aggregate | |
{ | |
private Product() { } | |
public Product(Guid id, string name, string description, decimal price) | |
{ | |
ValidateName(name); | |
ApplyEvent(new ProductCreated(id, name, description, price)); | |
} | |
public string Name { get; private set; } | |
public string Description { get; private set; } | |
public decimal Price { get; set; } | |
private void Apply(ProductCreated e) | |
{ | |
Id = e.Id; | |
Name = e.Name; | |
Description = e.Description; | |
Price = e.Price; | |
} | |
private void Apply(ProductPriceChanged e) | |
{ | |
Price = e.NewPrice; | |
} | |
private void Apply(ProductDescriptionChanged e) | |
{ | |
Description = e.NewDescription; | |
} | |
private void Apply(ProductNameChanged e) | |
{ | |
Name = e.NewName; | |
} | |
public void ChangeName(string newName, int originalVersion) | |
{ | |
ValidateName(newName); | |
ValidateVersion(originalVersion); | |
ApplyEvent(new ProductNameChanged(Id, newName)); | |
} | |
public void ChangeDescription(string newDescription, int originalVersion) | |
{ | |
ValidateVersion(originalVersion); | |
ApplyEvent(new ProductDescriptionChanged(Id, newDescription)); | |
} | |
public void ChangePrice(decimal newPrice, int originalVersion) | |
{ | |
ValidateVersion(originalVersion); | |
ApplyEvent(new ProductPriceChanged(Id, newPrice)); | |
} | |
void ValidateName(string name) | |
{ | |
if (string.IsNullOrWhiteSpace(name)) | |
{ | |
throw new ArgumentException("Invalid name specified: cannot be empty.", "name"); | |
} | |
} | |
void ValidateVersion(int version) | |
{ | |
if (Version != version) | |
{ | |
throw new ArgumentOutOfRangeException("version", "Invalid version specified: the version is out of sync."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment