I'd like to expand on this question When to update audit fields? DDD.
In my domain model, and this may not be the right place for it, but we have property for CreatedBy and ModifiedBy, both of type User, both value objects.
I am new to DDD and confused on which layer is responsible for updating these properties ... Data? Domain? or Application?
In the question, linked above, they mention using an event to update these properties ... the assumption would be then that these fields are updatable on the Domain?
Here is an example of my class ...
public class SpritePalette
{
private readonly SpritePaletteColors _colors;
public string Id { get; private set; }
public string Name { get; private set; }
public SpritePaletteColors Colors { get { return _colors; } }
public bool IsPublic { get; private set; }
public User CreatedBy { get; private set; }
public DateTime CreatedDate { get; private set; }
public User ModifiedBy { get; private set; }
public DateTime ModifiedDate { get; private set; }
public SpritePalette(
string name)
{
this.Name = name;
this.IsPublic = false;
_colors = new SpritePaletteColors();
}
internal void UpdateId(string value)
{
Validate.IsNotEmpty(value, "Id is required.");
this.Id = value;
}
public void UpdateName(string value)
{
this.Name = value;
}
public void MarkAsCreated(User value)
{
this.CreatedBy = value;
this.CreatedDate = DateTime.UtcNow;
}
public void MarkAsModified(User value)
{
this.ModifiedBy = value;
this.ModifiedDate = DateTime.UtcNow;
}
public bool HasColor(string color)
{
return _colors.HasColor(color);
}
public void AddColor(string color)
{
_colors.AddColor(color);
}
public void RemoveColor(string color)
{
_colors.RemoveColor(color);
}
public void UpdateIsPublic(bool value)
{
this.IsPublic = value;
}
}
I employee two methods, one for marking the model as created, which updates the CreatedBy and the CreatedDate, and a similar method for marking the model as modified.
Is this acceptable, when it comes to DDD? What is the better way to handle updating audit properties like these? Is there a better way, i.e., using events?
I admit this is a bit subjective, but would appreciate any help anyone can offer!