Changes between Version 3 and Version 4 of Documentation/DevelopmentCenter/UsePersistence


Ignore:
Timestamp:
06/26/14 20:18:06 (5 years ago)
Author:
abeham
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/DevelopmentCenter/UsePersistence

    v3 v4  
    1 = HeuristicLab Persistence Quick Start Guide =
     1= Quick guide: HeuristicLab persistence =
    22
    33== 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.
     4If 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
     6The 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.
    57
    68== 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
     9Content types that should be saved and restored have to be decorated with attributes.
    1210
    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]
     18public 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
     27If 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]
     32public 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}}}
    1440
    1541== Support for Other Classes ==