Compile time -- the documentation does not discuss this, but I would think that is the lightest weight way for data to get initialized.
Constructor -- I understand the advice to keep this as light as possible. As far as I know, that is because it helps the system for constructors to be as light as possible. After all, the constructors are run a lot. Also, for architectural reasons, some things cannot be done there. For example: serialized data and data that depends on system items that are not yet available. I don't see any architectural reason why other data cannot be initialized there -- I see only the lightweight reason, which is good enough for me, so I'll be a good citizen and postpone data initialization.
SetDefaults -- This is my major question -- what is the state of the data when the code enters SetDefaults? Is there a difference between serializable and non-serializable (XmlIgnore) configurable data? I understand that configurable data must be initialized here and that, to keep it lightweight, other data should not be initialized here. I need a better understanding of the state of serializable data when the code enters this state. Has any de-serialization already taken place, has cloning already taken place, or is the data as I would expect from the compiler plus whatever the code did in the constructor? Can I depend on compile-time initialization, or can SetDefaults be called on a non-virgin instance that really does need runtime initialization?
How far off is my mental model?
- The constructor sees virgin data but should, as discussed above, be kept lightweight. Most initialization should be done elsewhere.
- On entry to SetDefaults, the data is still virgin, which suggests to me that compile-time initialization should be fine and the code cannot expect any previous state to be set yet.
- Once the code has done whatever initialization it wants to in SetDefaults, the system will overlay de-serialization.
- Then the user gets to configure whatever is represented in the configuration dialog.
- The next thing the code sees is the Configure state when everything has been melded and the code sees fully-configured data.
Thanks for any guidance.



Comment