views:

64

answers:

1

My domain model looks like this:

class Group
{
    private List<Person> persons;

    public void AddPerson(Person p) {
       persons.Add(p);
       DoSideEffect()
    }

    public List<Person> GetPersons() {...}
}

Now I need to persist it. By DDD I cannot add any persistence attributes to this class so xml serializers will not work. BinaryFormatter cannot be used since the format should be readable. I can manually call GetPersons() and persist them - but how am I going to load them back? If I call AddPerson() then there is a side effect. The side effect should only happen when a person is "really" added to the domain, not with persistrancy.

A: 

Lack of attributes isn't a show-stopper; XmlSerializer has a constructor to pass in this model at runtime ( but to be honest, most times the defaults are fine) - as so several other serializers. XML via XmlSerializer is obviously desirable if readability is a concern though. See XmlAttributeOverrides. I can also suggest some binary serializers that would work here.

Marc Gravell
How can XmlSerializer work with private fields?
Yaron Naveh
@Yaron; ah, no, it can't. DataConractSerializer *can* though. As can protobuf-net "v2" if you don't mind binary. I know the latter supports attribute-less models ('cos I wrote it).
Marc Gravell
@Yaron - the other approach is a dedicated DTO layer.
Marc Gravell
@Marc - I want the DTO (for various other reasons too). It still has the same problem - the only way to add a person is by causing the side-effect (see my code). For loading (persistance) I do not want the side effect to happen - no person is "really" added, I just load a previous state. Sure, I can expose a method that adds a person w/o the effect - but this adds persistance helper code to domain model...
Yaron Naveh