Changes between Version 3 and Version 4 of Documentation/DevelopmentCenter/UsePersistence
- Timestamp:
- 06/26/14 20:18:06 (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Documentation/DevelopmentCenter/UsePersistence
v3 v4 1 = HeuristicLab Persistence Quick Start Guide =1 = Quick guide: HeuristicLab persistence = 2 2 3 3 == Invocation == 4 Use the classes `XmlGenerator` and `XmlParser` to transform an object graph into XML and read it back again. You can serialize into a file that is essentially a ZIP file with two entries or directly to a stream. 4 If you want to save to a file you are advised to use the //ContentManager// static class of the HeuristicLab.Common namespace. A concrete content manager is set when the //MainForm// is initializing, by default this is the //PersistenceContentManager// in HeuristicLab.Core. 5 6 The HeuristicLab persistence can be used directly in case the output should be a stream. Use the classes //XmlGenerator// and //XmlParser// in the HeuristicLab.Persistence.Default.Xml namespace to transform an object graph into XML and read it back again. 5 7 6 8 == Custom Classes == 7 * Add the `[StorableClass]` attribute to your class 8 * Add the `[Storable]` attribute to any fields or properties you would like to be persisted 9 * Choose a constructor that is available to the persistence: 10 * Either a default constructor with no arguments (can be private) 11 * Or a `[StorableConstructor]` with a single bool argument (e.g. `isDeserializing`) that is set to true when invoked by the persistence 9 Content types that should be saved and restored have to be decorated with attributes. 12 10 13 If you need more logic you can add a `[PersistenceHook]` attribute to parameterless methods that will be called before serialization starts or after serialization finishes for an object. 11 * Add the [StorableClass] attribute to a new class 12 * Add the [Storable] attribute to any fields or properties you would like to be persisted 13 * If the default constructor takes longer to initialize, persistence can be directed to use a different constructor by decorating it with the [StorableConstructor] attribute. This constructor has to have a single boolean parameter which will be set to true. 14 15 {{{ 16 #!csharp 17 [Storable] 18 public class SomeClass : Item { 19 [StorableConstructor] 20 protected SomeClass(bool deserializing) : base(deserializing) { } 21 public SomeClass() { 22 // a lot of initialization code that should be skipped during deserialization 23 } 24 } 25 }}} 26 27 If you need more control you can add a [StorableHook] attribute to a parameterless method that will be called after deserialization finishes or before serialization starts. 28 29 {{{ 30 #!csharp 31 [StorableClass] 32 public class SomeClass { 33 [StorableHook(HookType.AfterDeserialization)] 34 private void AfterDeserialization() { 35 // backwards-compatible code (loading files stored with previous versions) 36 // register event handlers 37 } 38 } 39 }}} 14 40 15 41 == Support for Other Classes ==