Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Order // Ovo je AGGREGATE ROOT
- {
- public Guid Id { get; private set; } // Identitet agregatnog korena
- public DateTime CreatedOn { get; private set; }
- public Address ShippingAddress { get; private set; } // Value Object
- public OrderStatus Status { get; private set; } // Stanje porudžbine
- // Kolekcija OrderItem-a (interni entiteti ili value objekti)
- // Pristup ovoj kolekciji je samo preko metoda Order agregata
- private List<OrderItem> _orderItems;
- public IReadOnlyCollection<OrderItem> OrderItems => _orderItems.AsReadOnly();
- public Money TotalPrice { get; private set; } // Izračunata vrednost, Value Object
- public Order(Guid id, DateTime createdOn, Address shippingAddress)
- {
- Id = id;
- CreatedOn = createdOn;
- ShippingAddress = shippingAddress ?? throw new ArgumentNullException(nameof(shippingAddress));
- Status = OrderStatus.Created;
- _orderItems = new List<OrderItem>();
- TotalPrice = new Money(0, "EUR"); // Inicijalna vrednost
- }
- // Sva interakcija sa agregatom ide kroz koren (Order klasu)
- public void AddItem(Product product, int quantity)
- {
- // Poslovno pravilo: ne dozvoliti negativnu količinu
- if (quantity <= 0)
- {
- throw new ArgumentException("Quantity must be positive.");
- }
- // Provera da li proizvod već postoji u porudžbini
- var existingItem = _orderItems.FirstOrDefault(item => item.ProductId == product.Id);
- if (existingItem != null)
- {
- // Ažuriranje postojeće stavke
- existingItem.IncreaseQuantity(quantity); // Ova metoda bi bila definisana unutar OrderItem
- }
- else
- {
- // Dodavanje nove stavke
- _orderItems.Add(new OrderItem(Guid.NewGuid(), product.Id, product.Name, product.Price, quantity));
- }
- // Izračunavanje ukupne cene (poslovno pravilo, konzistentnost agregata)
- CalculateTotalPrice();
- }
- public void RemoveItem(Guid orderItemId)
- {
- var itemToRemove = _orderItems.FirstOrDefault(item => item.Id == orderItemId);
- if (itemToRemove == null)
- {
- throw new InvalidOperationException("Order item not found.");
- }
- _orderItems.Remove(itemToRemove);
- CalculateTotalPrice();
- }
- public void UpdateShippingAddress(Address newAddress)
- {
- // Validacija za promenu adrese ako je potrebno (npr. samo pre isporuke)
- if (Status != OrderStatus.Created)
- {
- throw new InvalidOperationException("Shipping address can only be updated for new orders.");
- }
- ShippingAddress = newAddress;
- }
- private void CalculateTotalPrice()
- {
- decimal totalAmount = 0;
- string currency = "EUR"; // Pretpostavka jedne valute za TotalPrice
- foreach (var item in _orderItems)
- {
- totalAmount += item.ProductPrice.Amount * item.Quantity;
- // Provera valute ako su stavke u različitim valutama
- if (currency != item.ProductPrice.Currency)
- {
- // Možda kompleksnija logika za konverziju ili bacanje izuzetka
- throw new InvalidOperationException("Mixed currencies in order items.");
- }
- }
- TotalPrice = new Money(totalAmount, currency);
- }
- // Mogući unutrašnji entitet (OrderItem)
- public class OrderItem // Ovo je entitet unutar agregata, nema sopstveni repozitorijum
- {
- public Guid Id { get; private set; } // Identitet unutar agregata
- public Guid ProductId { get; private set; }
- public string ProductName { get; private set; }
- public Money ProductPrice { get; private set; } // Value Object
- public int Quantity { get; private set; }
- public OrderItem(Guid id, Guid productId, string productName, Money productPrice, int quantity)
- {
- Id = id;
- ProductId = productId;
- ProductName = productName;
- ProductPrice = productPrice;
- Quantity = quantity;
- }
- public void IncreaseQuantity(int amount)
- {
- if (amount <= 0)
- {
- throw new ArgumentException("Amount must be positive.");
- }
- Quantity += amount;
- }
- }
- }
- public enum OrderStatus
- {
- Created,
- Paid,
- Shipped,
- Delivered,
- Cancelled
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement