Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1420


Ignore:
Timestamp:
03/25/09 17:52:02 (15 years ago)
Author:
epitzer
Message:

Remove old storable persistence mechanism. (#506)

Location:
branches/New Persistence Exploration/Persistence/Persistence/Core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs

    r1419 r1420  
    66
    77  struct ParentReference { }
    8   delegate void Setter(object value);
     8  delegate void Setter(object value); 
    99
    10 
    11   interface IAccessibleObject {
    12     object Obj { get; }
    13     Setter GetSetter(string name);
    14   }
    15 
    16 
    17   class CustomComposite : IAccessibleObject {
     10  class CompositeObject {
    1811
    1912    public object Obj { get; private set; }
    2013    public List<Tag> customValues;
    2114
    22     public CustomComposite(object obj) {
     15    public CompositeObject(object obj) {
    2316      Obj = obj;
    2417      customValues = new List<Tag>();
     
    2922    }
    3023
    31     public Setter GetSetter(string name) {     
    32       int index = customValues.Count - 1;
    33       Tag t = customValues[index];     
     24    public Setter GetSetter(string name) {           
     25      Tag t = customValues[customValues.Count - 1];     
    3426      return value => t.Value = value;
    3527    }
    36   }
    37 
    38 
    39   class StorableComposite : IAccessibleObject {
    40 
    41     public object Obj { get; private set; }
    42     public readonly Dictionary<string, DataMemberAccessor> accessorDict;
    43 
    44     public StorableComposite(object obj, Dictionary<string, DataMemberAccessor> accessorDict) {
    45       Obj = obj;
    46       this.accessorDict = new Dictionary<string, DataMemberAccessor>();
    47       foreach (KeyValuePair<string, DataMemberAccessor> pair in accessorDict) {
    48         this.accessorDict.Add(
    49           pair.Value.Name,
    50           pair.Value);
    51       }
    52     }
    53 
    54     public void SetValue(string name, object value) {
    55       accessorDict[name].Set(value);
    56       accessorDict.Remove(name);
    57     }
    58 
    59     public Setter GetSetter(string name) {
    60       return value => accessorDict[name].Set(value);
    61     }
    62 
    63     public void PopulateDefaultValues() {
    64       foreach (var pair in accessorDict) {
    65         pair.Value.Set(pair.Value.DefaultValue);
    66       }
    67     }
    68   }
    69 
     28  } 
    7029
    7130  public class DeSerializer {
     
    7736    private readonly Dictionary<Type, object> serializerMapping;
    7837    private readonly Dictionary<Type, Handler> handlers;
    79     private readonly Stack<IAccessibleObject> parentStack;   
     38    private readonly Stack<CompositeObject> parentStack;   
    8039    private readonly Dictionary<int, Type> typeIds;   
    8140    private List<Thunk> finalFixes;
     
    8443      IEnumerable<TypeMapping> typeCache) {     
    8544      id2obj = new Dictionary<int, object>();
    86       parentStack = new Stack<IAccessibleObject>();
     45      parentStack = new Stack<CompositeObject>();
    8746      handlers = new Dictionary<Type, Handler> {
    8847                     {typeof (BeginToken), CompositeStartHandler},
     
    10564
    10665    public object DeSerialize(IEnumerable<ISerializationToken> tokens) {
    107       finalFixes = new List<Thunk>();
     66      finalFixes = new List<Thunk>();     
    10867      foreach (ISerializationToken token in tokens) {
    10968        handlers[token.GetType()].Invoke(token);
     
    12685        if (instance == null)
    12786          instance = new ParentReference();
    128         parentStack.Push(new CustomComposite(instance));       
    129       } else {       
    130         instance = Activator.CreateInstance(type, true);
    131         Dictionary<string, DataMemberAccessor> accessorDict =
    132           StorableAttribute.GetAutostorableAccessors(instance);
    133         parentStack.Push(new StorableComposite(instance, accessorDict));       
     87        parentStack.Push(new CompositeObject(instance));       
     88      } else {
     89        throw new ApplicationException(String.Format(
     90          "No suitable method for deserialization of type \"{0}\" found.",
     91          type.FullName));
    13492      }
    13593      if ( start.Id != null )
     
    144102        decomposer = serializerMapping[type] as IDecomposer;           
    145103      if (decomposer != null) {
    146         CustomComposite customComposite = (CustomComposite)parentStack.Pop();
     104        CompositeObject customComposite = (CompositeObject)parentStack.Pop();
    147105        object deserializedObject =         
    148106          decomposer.Populate(customComposite.Obj, customComposite.customValues, type);
     
    151109        SetValue(end.Name, deserializedObject);         
    152110      } else {
    153         StorableComposite storableComposite = (StorableComposite)parentStack.Pop();
    154         storableComposite.PopulateDefaultValues();
    155         SetValue(end.Name, storableComposite.Obj);
     111        throw new ApplicationException(String.Format(
     112          "No suitable method for deserialization of type \"{0}\" found.",
     113          type.FullName));
    156114      }
    157115    }
     
    183141
    184142    private void SetValue(string name, object value) {
    185       if (parentStack.Count == 0) {
    186         parentStack.Push(new StorableComposite(value, new Dictionary<string, DataMemberAccessor>()));
    187       } else {
    188         object accessibleObject = parentStack.Peek();
    189         if (accessibleObject is StorableComposite) {
    190           ((StorableComposite)accessibleObject).SetValue(name, value);
    191         } else if (accessibleObject is CustomComposite) {
    192           ((CustomComposite)accessibleObject).AddValue(name, value);
    193         }
     143      if (parentStack.Count == 0) {       
     144        parentStack.Push(new CompositeObject(value));
     145      } else {       
     146        parentStack.Peek().AddValue(name, value);       
    194147      }
    195148    }
  • branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs

    r1419 r1420  
    9292      IDecomposer decomposer = configuration.GetDecomposer(value.GetType());
    9393      if (decomposer != null)
    94         return CompositeEnumerator(accessor.Name, decomposer.DeCompose(value), id, typeId);           
    95       return StorableEnumerator(accessor.Name, value, id, typeId);
     94        return CompositeEnumerator(accessor.Name, decomposer.DeCompose(value), id, typeId);                 
     95      throw new ApplicationException(
     96          String.Format(
     97          "No suitable method for serializing values of type \"{0}\" found.",
     98          value.GetType().FullName));     
    9699    }
    97100
     
    121124    }
    122125
    123     private IEnumerator<ISerializationToken> StorableEnumerator(string name,
    124         object value, int? id, int typeId) {           
    125       yield return new BeginToken(name, typeId, id);
    126       int nSubComponents = 0;
    127       foreach (KeyValuePair<string, DataMemberAccessor> mapping in
    128         StorableAttribute.GetAutostorableAccessors(value)) {
    129         nSubComponents += 1;               
    130         IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value);
    131         while (iterator.MoveNext()) {
    132           yield return iterator.Current;
    133         }
    134       }
    135       if (nSubComponents == 0 && ! EmptyStorableClassAttribute.IsEmpyStorable(value.GetType())) {
    136         throw new ApplicationException(
    137           String.Format(
    138             "Value of type \"{0}\" has no formatter or decomposer, " +
    139             "contains no storable subcomponents, " +
    140             "and is not marked as empty storable class.",
    141             value.GetType().FullName));
    142       }
    143       yield return new EndToken(name, typeId, id);
    144     }
    145126  }
    146127
Note: See TracChangeset for help on using the changeset viewer.