I’ve been modeling a domain for several days, and didn’t think about persistence at all, but instead I focused on the domain logic. Now I am ready to save domain objects, some of which contain IEnumerable child objects. Using RavenDb, resilience is "light", but when loading my objects all IEnumerables are empty again.
I realized that this is because they don’t have any properties at all, but instead use a list as a support field. The user of the domain’s root aggregate can add children through the public method, and not directly to the collection.
private readonly List<VeryImportantPart> _veryImportantParts; public IEnumerable<VeryImportantPart> VeryImportantParts { get { return _veryImportantParts; } }
And the adding method, nothing unusual ...
public void AddVeryImportantPart(VeryImportantPart part) {
I can fix this by adding a private / secure setter to all of my IEnumerables with support fields, but it looks ... well ... not super sexy.
private List<VeryImportantPart> _veryImportantParts; public IEnumerable<VeryImportantPart> VeryImportantParts { get { return _veryImportantParts; } protected set { _veryImportantParts = value.ToList(); } }
Now the RavenDb json serializer will populate my objects again on boot, but I'm curious if there is a cleaner way to do this?
I searched for JsonContractResolver but haven't found a solution yet ...
source share