In the context of this question, if I use the word Entity I'm referring to a DDD entity, unless I explicitly state that I'm talking about an EF Core Entity
I've been reading Microsoft's documentation on DDD patterns for .NET Core.
I've also been looking at the GitHub repo they've created that contains the implementation described in the aforementioned documentation. Specifically, I'm looking at the Order.cs
aggregate (which is where the link points to).
Background to the problem
I'm trying to understand how they've implemented Enumeration
properties.
As you can see, the Enumeration
type is similar to a Value Object but is a more convenient way (imho) of modelling attributes that behave like enums.
Excerpt of a DDD entity that uses the Enumeration
type (the full entity is in the repo link above):
public class Order
: Entity, IAggregateRoot
{
public OrderStatus OrderStatus { get; private set; }
private int _orderStatusId;
public Order()
{
_orderStatusId = OrderStatus.Submitted.Id;
}
}
Problem
Implementing a property this way means that whenever you get
the OrderStatus
property, it is null
.
I understand that encapsulating an Entity in DDD is of paramount concern, but surely that's only a concern when you're modifying it's state. Implementing a GetOrderStatus
method seems like a very hacky way of just reading the property's value:
public class Order
: Entity, IAggregateRoot
{
public OrderStatus OrderStatus { get; private set; }
private int _orderStatusId;
public Order()
{
_orderStatusId = OrderStatus.Submitted.Id;
}
public OrderStatus GetOrderStatus()
{
return Enumeration.FromValue<OrderStatus>(_orderStatusId);
}
}
The only other thing I thought of was implementing the property like the below, but that breaks EF Core:
public class Order
: Entity, IAggregateRoot
{
public OrderStatus OrderStatus
{
get
{
return Enumeration.FromValue<OrderStatus>(_orderStatusId);
}
private set
{
Status = value;
}
}
private int _orderStatusId;
public Order()
{
_orderStatusId = OrderStatus.Submitted.Id;
}
}
Is there a cleaner way to implement this?