Looking for help with object init problem
i have a problem-
An object has to initialize some nested objects on construction, but it also can be deserialized, and constructor is called before deserialization, and the nested objects depend on the initial data, so by the time object is deserailized nested has wrong data. One way to solve it is lazy initialization, which i dont want, another is some sort of a factory? Is there some third way i don't know about? I want some pattern to have the object seamlessly handle both ways internally.
2 Replies
Why not just initialize the properties with default values?
You could also add a default, empty constructor for the JSON serializer to use
Alternatively, forgo the constructor completely and just use
required
properties and the initializer syntaxi think i came up with a middle ground solution
i will have to write a very simple factory method that calls some interface after
new()
to init the object, and i can do the same thing in the serializer code delayed until deserialization is complete, so both behave identically but independently