I think we should look at the principles of DDD and derive the correct answer from there.
Public auto-property getters/setters in C# are functionally just public properties. Using auto-property getters/setters is not inherently bad as long as there are no business rules regarding the correct values of the respective properties and no domain events that need to fire when those properties change.
Also, one should not build aggregates or entities that have only public auto-properties, as that leads to an anemic model and an anemic domain. Such an "aggregate" is not an actual aggregate, but more a DTO or Value Object.
Personally, I think that if we use property accessors (get/set) with bodies to integrate business logic, we can make our code a little bit more readable and probably a lot less verbose.
For example:
// Instead of this:
public DemoAggregate : IAggregate
{
public string Name { get; private set; }
public void ChangeName(string newName)
{
Name = Check.MinMaxLength(newName, 1, 100,
$"{nameof(newName)} length must be between 1 and 100 characters.");
}
}
/* MinMaxLength throws a business exception
* if the new name is outside of the accepted range
* otherwise it returns the value unchanged.
*/
// ...you can write this to get one method less:
public AltDemoAggregate : IAggregate
{
private string _name;
public string Name
{
get => _name;
set => value = Check.MinMaxLength(newName, 1, 100,
$"{nameof(newName)} length must be between 1 and 100 characters.");
}
}
The only issue with the above is that, internally, you can bypass business logic if you set _name
directly in some method. But if you're disciplined enough, I don't think it's an issue. It might seem scary for some though and I understand.
On the upside, if you're using something like Entity Framework, I think you can configure it to hydrate new instances by calling properties (not backing fields), thus preventing loading an invalid aggregate from the database (say if you're imported some bulk data that might contain some garbage). I haven't tested this though.
Expression bodied accessors are used in the second example just to show that you can reduce boilerplate a lot.
It's possible to use an expression bodied getter like above because strings have value semantics in C# so that expression returns a copy of _name
, thus not exposing a reference to an internal variable.
Note that with C# 9 records for example, you have only value-based equality semantics. A record is still passed by reference! Since records should ideally be immutable (init only), you could return references to such records (which is more performant) and skip having to clone (which is simple for shallow clones but hard for deep clones).
If you have such an object inside an aggregate, for example a DDD Value Object that isn't an immutable record or a record that can be easily cloned, you need to make sure you're not returning a reference to an internal object that can be mutated, thus bypassing business logic and messing with aggregate integrity.
Take a List for example. You can use IReadOnlyList
as the return type, but that is not enough if you're only casting a private internal property, because that reference can be "up-casted" outside back to a List and then used to modify it.
In this case you should also use the .AsReadOnly()
method of List
to return a new read only wrapper list over the elements of the original list.
Beware though that only the wrapper list is protected from change (it has no add or remove methods) but not the elements themselves. It's their responsibility to protected themselves from change.
EDIT:
I just realised that my example is not exactly correct. Such (private?) accessors with logic can be used for simple logic like making sure that when setting an end date, it is not before a start date, but for complex cases, setting an end date for example can happen for a number of reasons which must be all modelled as verbs, for example terminateContract(DateTime finalDay, string reason)
or closeContract(DateTime closedEarlyDate)
, they are must more explicit on the reason to set the end date. In this case anyway, generic logic that should always be applied can live in the setter accessor (this provides deduplication of code) and per-case per-action logic can live in the specific action method.