← Back to Blog
When Your Domain Layer Is Just Properties

When Your Domain Layer Is Just Properties

I once heard a developer explain his architecture approach: "Domain layer contains only entity models. No behavior, no methods. All business logic lives in services."

It's a common pattern, and at first glance it seems clean. But over time, I've come to see it differently.

The issue isn't the approach being "wrong". It's what gets lost when we treat domain objects as mere data containers.

If your domain layer is just classes with properties, you don't have a domain model. You have a database schema written in C#.

Let me show what I mean in code.

The Anemic Domain Model: Data Bags + God Service

A typical "anemic" approach looks something like this:

// Domain layer
public class Order
{
    public Guid Id { get; set; }
    public Guid CustomerId { get; set; }
    public List<OrderLine> Lines { get; set; } = new();
    public decimal Total { get; set; }
    public string Status { get; set; } // "Draft", "Confirmed", "Cancelled"
    public DateTime CreatedAt { get; set; }
    public DateTime? ConfirmedAt { get; set; }
}

public class OrderLine
{
    public Guid ProductId { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
}

All the "smart" stuff is pushed into the service:

public class OrderService
{
    public void AddLine(Order order, Guid productId, int quantity, decimal unitPrice)
    {
        if (quantity <= 0)
            throw new ArgumentException("Quantity must be greater than zero", nameof(quantity));

        var line = new OrderLine
        {
            ProductId = productId,
            Quantity = quantity,
            UnitPrice = unitPrice
        };

        order.Lines.Add(line);
        order.Total = order.Lines.Sum(l => l.Quantity * l.UnitPrice);
    }

    public void Confirm(Order order)
    {
        if (!order.Lines.Any())
            throw new InvalidOperationException("Cannot confirm an order without lines.");

        if (order.Status == "Confirmed")
            return;

        order.Status = "Confirmed";
        order.ConfirmedAt = DateTime.UtcNow;
    }

    public void Cancel(Order order)
    {
        if (order.Status == "Cancelled")
            return;

        if (order.Status == "Confirmed")
            throw new InvalidOperationException("Cannot cancel a confirmed order.");

        order.Status = "Cancelled";
    }
}

On paper it looks "clean": entities are "pure models", services do the work. In practice, this has serious problems:

  • Rules are scattered across services
  • Anyone can change Status directly (order.Status = "Confirmed";)
  • Every new service can bypass existing logic and break invariants
  • Tests are written against services, not against the domain

This is procedural style wrapped in OOP syntax.

Rich Domain Model: Let the Aggregate Defend Itself

Now the same thing, but as a rich domain model. First, the entity is no longer a "public set" bag:

public class Order
{
    private readonly List<OrderLine> _lines = new();

    private Order() { } // for EF

    public Order(Guid customerId)
    {
        Id = Guid.NewGuid();
        CustomerId = customerId;
        Status = OrderStatus.Draft;
        CreatedAt = DateTime.UtcNow;
    }

    public Guid Id { get; private set; }
    public Guid CustomerId { get; private set; }
    public IReadOnlyCollection<OrderLine> Lines => _lines.AsReadOnly();
    public decimal Total => _lines.Sum(l => l.TotalPrice);
    public OrderStatus Status { get; private set; }
    public DateTime CreatedAt { get; private set; }
    public DateTime? ConfirmedAt { get; private set; }

    public void AddLine(Guid productId, int quantity, decimal unitPrice)
    {
        if (Status != OrderStatus.Draft)
            throw new InvalidOperationException("Can only modify draft orders.");

        if (quantity <= 0)
            throw new ArgumentException("Quantity must be greater than zero.", nameof(quantity));

        var existing = _lines.SingleOrDefault(l => l.ProductId == productId);

        if (existing is null)
            _lines.Add(new OrderLine(productId, quantity, unitPrice));
        else
            existing.IncreaseQuantity(quantity);
    }

    public void Confirm()
    {
        if (Status != OrderStatus.Draft)
            throw new InvalidOperationException("Only draft orders can be confirmed.");

        if (!_lines.Any())
            throw new InvalidOperationException("Cannot confirm an order without lines.");

        Status = OrderStatus.Confirmed;
        ConfirmedAt = DateTime.UtcNow;
    }

    public void Cancel()
    {
        if (Status == OrderStatus.Confirmed)
            throw new InvalidOperationException("Cannot cancel a confirmed order.");

        if (Status == OrderStatus.Cancelled)
            return;

        Status = OrderStatus.Cancelled;
    }
}
public class OrderLine
{
    private OrderLine() { }

    public OrderLine(Guid productId, int quantity, decimal unitPrice)
    {
        if (quantity <= 0)
            throw new ArgumentException("Quantity must be greater than zero.", nameof(quantity));

        ProductId = productId;
        Quantity = quantity;
        UnitPrice = unitPrice;
    }

    public Guid ProductId { get; private set; }
    public int Quantity { get; private set; }
    public decimal UnitPrice { get; private set; }
    public decimal TotalPrice => UnitPrice * Quantity;

    public void IncreaseQuantity(int amount)
    {
        if (amount <= 0)
            throw new ArgumentException("Increase must be positive.", nameof(amount));

        Quantity += amount;
    }
}

public enum OrderStatus
{
    Draft = 0,
    Confirmed = 1,
    Cancelled = 2
}

Notice the difference:

  • Nobody external can change Status, Lines or Total directly
  • The only way to modify an order is through domain methods: AddLine, Confirm, Cancel
  • Rules live in the domain, close to the data they relate to
  • The domain object guards its own invariants

The Service Becomes a Thin Orchestrator

The service now becomes a thin orchestrator, not a logic carrier:

public class OrderApplicationService
{
    private readonly IOrderRepository _orders;

    public OrderApplicationService(IOrderRepository orders)
    {
        _orders = orders;
    }

    public async Task AddLineAsync(Guid orderId, Guid productId, int quantity, decimal unitPrice)
    {
        var order = await _orders.GetByIdAsync(orderId);
        if (order == null)
            throw new InvalidOperationException("Order not found.");

        order.AddLine(productId, quantity, unitPrice);
        await _orders.SaveAsync(order);
    }

    public async Task ConfirmAsync(Guid orderId)
    {
        var order = await _orders.GetByIdAsync(orderId);
        if (order == null)
            throw new InvalidOperationException("Order not found.");

        order.Confirm();
        await _orders.SaveAsync(order);
    }

    public async Task CancelAsync(Guid orderId)
    {
        var order = await _orders.GetByIdAsync(orderId);
        if (order == null)
            throw new InvalidOperationException("Order not found.");

        order.Cancel();
        await _orders.SaveAsync(order);
    }
}

Logic stays in the Order class. The service just calls the appropriate domain method and handles persistence / IO.

Testing the Domain Instead of the Service

The biggest benefit shows when you write tests. Instead of testing the "service", you directly test business rules:

[Fact]
public void Confirm_Should_Set_Status_And_Timestamp()
{
    var order = new Order(customerId: Guid.NewGuid());
    order.AddLine(Guid.NewGuid(), 2, 10m);

    order.Confirm();

    Assert.Equal(OrderStatus.Confirmed, order.Status);
    Assert.NotNull(order.ConfirmedAt);
    Assert.Equal(20m, order.Total);
}

[Fact]
public void Confirm_Should_Throw_If_No_Lines()
{
    var order = new Order(customerId: Guid.NewGuid());

    Assert.Throws<InvalidOperationException>(() => order.Confirm());
}

No database, no HTTP, no mocking circus. You test the domain itself, clean and fast.

Why This Pattern Falls Short

Going back to that architecture approach. The idea that the domain layer should contain only "entity models" because it's "clean" and "separated" sounds reasonable. But it's actually a pure anemic domain model — essentially procedural code, without any invariant protection where it belongs.

If entities are just DTOs, then:

  • You're not using OOP for what it was created (objects that encapsulate state and behavior)
  • You're not using DDD, even though you might think you have bounded contexts and aggregates
  • You're allowing business rules to leak everywhere in the code, instead of living in the domain core

A domain layer without behavior is just a database appendage.

A rich domain model is the moment when classes stop being mere data bags and start defending the rules of the system. That's when the object knows what can and cannot happen to it.

That's the difference between an "entity" and a domain model.