Domain-Driven Design Basics
🎯 What You'll Learn
- What is DDD
- Entities vs Value Objects
- Aggregates
- Domain Services
- Ubiquitous Language
What is Domain-Driven Design?
Domain-Driven Design (DDD) is an approach to software development that focuses on modeling the business domain and using a common language between developers and domain experts.
Entities
Objects with a unique identity that persists over time.
public class Product
{
public int Id { get; private set; } // Identity
public string Name { get; private set; }
public Money Price { get; private set; }
public void UpdatePrice(Money newPrice)
{
if (newPrice.Amount <= 0)
throw new ArgumentException("Price must be positive");
Price = newPrice;
}
}
Value Objects
Objects without identity, defined by their attributes.
public class Money
{
public decimal Amount { get; }
public string Currency { get; }
public Money(decimal amount, string currency)
{
Amount = amount;
Currency = currency;
}
public override bool Equals(object obj)
{
if (obj is Money other)
return Amount == other.Amount && Currency == other.Currency;
return false;
}
public override int GetHashCode()
{
return HashCode.Combine(Amount, Currency);
}
}
Aggregates
A cluster of entities and value objects with a root entity.
public class Order // Aggregate Root
{
public int Id { get; private set; }
private readonly List<OrderItem> _items = new();
public IReadOnlyCollection<OrderItem> Items => _items.AsReadOnly();
public void AddItem(Product product, int quantity)
{
var item = new OrderItem(product, quantity);
_items.Add(item);
}
public Money GetTotal()
{
var total = _items.Sum(i => i.Price.Amount * i.Quantity);
return new Money(total, "USD");
}
}
public class OrderItem // Part of Order aggregate
{
public int Id { get; private set; }
public Product Product { get; private set; }
public int Quantity { get; private set; }
public Money Price { get; private set; }
public OrderItem(Product product, int quantity)
{
Product = product;
Quantity = quantity;
Price = product.Price;
}
}
Domain Services
Business logic that doesn't belong to a single entity.
public class PricingService
{
public Money CalculateDiscount(Order order, Customer customer)
{
var total = order.GetTotal();
var discountPercent = customer.IsPremium ? 0.10m : 0.05m;
return new Money(total.Amount * discountPercent, total.Currency);
}
}
Ubiquitous Language
A common language shared by developers and domain experts.
// ❌ Bad: Technical language
public void ProcessData() { }
// ✅ Good: Domain language
public void PlaceOrder() { }
public void ShipOrder() { }
public void CancelOrder() { }
InvenTrack Example
public class Inventory
{
public int Id { get; private set; }
public Product Product { get; private set; }
public int QuantityOnHand { get; private set; }
public int ReorderLevel { get; private set; }
public void ReceiveStock(int quantity)
{
if (quantity <= 0)
throw new ArgumentException("Quantity must be positive");
QuantityOnHand += quantity;
}
public void AllocateStock(int quantity)
{
if (quantity > QuantityOnHand)
throw new InvalidOperationException("Insufficient stock");
QuantityOnHand -= quantity;
}
public bool NeedsReorder() => QuantityOnHand < ReorderLevel;
}
Key Takeaways
- DDD: Focus on business domain
- Entities: Objects with unique identity
- Value Objects: Objects defined by attributes
- Aggregates: Cluster with root entity
- Domain Services: Business logic across entities
- Ubiquitous Language: Common vocabulary
You've completed the ASP.NET Core Mastery learning guide! You've covered 122 sections across 20 parts, from the basics to advanced topics.
You now have the knowledge to build production-ready ASP.NET Core applications with:
- ✅ Solid fundamentals (.NET Core, C#, MVC)
- ✅ Database access (EF Core, migrations, relationships)
- ✅ Authentication & authorization (Identity, JWT, policies)
- ✅ API development (REST, validation, versioning)
- ✅ Advanced features (SignalR, gRPC, microservices)
- ✅ Testing (unit, integration, TDD)
- ✅ Performance optimization (caching, async, profiling)
- ✅ Deployment (IIS, Linux, Docker, Azure, CI/CD)
- ✅ Architecture (SOLID, Clean Architecture, DDD)
Keep building, keep learning, and happy coding! 🚀