Changeset 1349


Ignore:
Timestamp:
03/16/09 17:46:28 (12 years ago)
Author:
epitzer
Message:

Central persistence configuration class. (#506)

Location:
branches/New Persistence Exploration/Persistence/Persistence
Files:
1 added
3 edited

Legend:

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

    r1348 r1349  
    11using System.Collections.Generic;
    22using System;
    3 using System.Reflection;
    4 using System.Text;
    53
    64namespace Persistence {
     
    6462    private readonly Dictionary<Type, Handler> handlers;
    6563    private readonly Stack<IAccessibleObject> compositeStack;
    66 
    67     private readonly Dictionary<Type, IFormatter> primitiveSerializers;
    68     private readonly List<IDecomposer> customSerializers;
     64    private readonly PersistenceConfiguration persistenceConfiguration;
    6965
    7066    delegate void Thunk();
    7167    private List<Thunk> finalFixes;
    7268
    73     public DeSerializer() : this(
    74       InterfaceInstantiatior.InstantiateAll<IFormatter>(),
    75       InterfaceInstantiatior.InstantiateAll<IDecomposer>()) {}   
    76 
    77     public DeSerializer(
    78         IEnumerable<IFormatter> primitiveSerializers,
    79         IEnumerable<IDecomposer> customSerializers) {
     69    public DeSerializer() : this(PersistenceConfiguration.Instance) {}
     70 
     71    public DeSerializer(PersistenceConfiguration persistenceConfiguration) {
     72      this.persistenceConfiguration = persistenceConfiguration;
    8073      id2obj = new Dictionary<int, object>();
    8174      compositeStack = new Stack<IAccessibleObject>();
     
    8679                     {typeof (Reference), ReferenceHandler},
    8780                     {typeof (Null), NullHandler}
    88                    };
    89       this.primitiveSerializers = new Dictionary<Type, IFormatter>();
    90       foreach (IFormatter ps in primitiveSerializers) {
    91         this.primitiveSerializers.Add(ps.Type, ps);
    92       }
    93       this.customSerializers = new List<IDecomposer>(customSerializers);
     81                   };     
    9482    }
    9583
     
    10896      CompositeStart start = (CompositeStart)token;
    10997      object instance;
    110       if (FindCompoundSerializer(start.Type) != null) {
     98      if (persistenceConfiguration.GetDecomposer(start.Type) != null) {
    11199        instance = new ParentReference();
    112100        compositeStack.Push(new CustomObject(instance));       
     
    121109    }
    122110    private void CompositeEndHandler(IParseToken token) {
    123       CompositeEnd end = (CompositeEnd)token;
    124       IDecomposer decomposer = FindCompoundSerializer(end.Type);
     111      CompositeEnd end = (CompositeEnd)token;     
     112      IDecomposer decomposer = persistenceConfiguration.GetDecomposer(end.Type);
    125113      if (decomposer != null) {
    126114        CustomObject customObject = (CustomObject)compositeStack.Pop();
     
    135123        SetValue(end.Name, compositeObject.Obj);
    136124      }
    137     }
    138     private IDecomposer FindCompoundSerializer(Type type) {
    139       foreach (IDecomposer serializer in customSerializers) {
    140         if (serializer.CanSerialize(type))
    141           return serializer;
    142       }
    143       return null;
    144     }
     125    }   
    145126    private void PrimitiveHandler(IParseToken token) {
    146127      Primitive primitive = (Primitive)token;
    147       object value = primitiveSerializers[primitive.Type].DeSerialize(primitive.SerializedValue);
     128      object value = persistenceConfiguration
     129        .GetFormatter(XmlFormat.Instance, primitive.Type)
     130        .DeSerialize(primitive.SerializedValue);
    148131      if ( ! value.GetType().IsValueType )
    149132        id2obj[(int)primitive.Id] = value;
  • branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj

    r1348 r1349  
    5454  <ItemGroup>
    5555    <Compile Include="DeSerializer.cs" />
     56    <Compile Include="PersistenceConfiguration.cs" />
    5657    <Compile Include="Properties\AssemblyInfo.cs" />
    5758    <Compile Include="Serializer.cs" />
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1348 r1349  
    1010    private readonly string rootName;
    1111    private readonly Dictionary<object, int> obj2id;
    12     private readonly Dictionary<Type, IFormatter> primitiveSerializers;
    13     private readonly List<IDecomposer> compoundSerializers;
    1412    private readonly Dictionary<Type, int> typeCache;
     13    private readonly PersistenceConfiguration persistenceConfiguration;
    1514
    16     public Serializer(object obj) :
    17       this(obj,
    18       InterfaceInstantiatior.InstantiateAll<IFormatter>(),
    19       InterfaceInstantiatior.InstantiateAll<IDecomposer>()) {}
     15    public Serializer(object obj) : this(obj, PersistenceConfiguration.Instance) {}
    2016
    21     public Serializer(object obj,
    22         IEnumerable<IFormatter> primitiveSerializers,
    23         IEnumerable<IDecomposer> compoundSerializers) :
    24       this(obj, primitiveSerializers, compoundSerializers, "ROOT") { }
     17    public Serializer(object obj, PersistenceConfiguration persistenceConfiguration) :       
     18      this(obj, persistenceConfiguration, "ROOT") { }
    2519
    26     public Serializer(object obj,
    27         IEnumerable<IFormatter> primitiveSerializers,
    28         IEnumerable<IDecomposer> compoundSerializers, string rootName) {
     20    public Serializer(object obj, PersistenceConfiguration persistenceConfiguration, string rootName) {
    2921      this.obj = obj;
    3022      this.rootName = rootName;
    31       this.primitiveSerializers = new Dictionary<Type, IFormatter>();
    32       foreach (IFormatter serializer in primitiveSerializers) {
    33         this.primitiveSerializers.Add(serializer.Type, serializer);
    34       }
    35       this.compoundSerializers = new List<IDecomposer>(compoundSerializers);
     23      this.persistenceConfiguration = persistenceConfiguration;     
    3624      obj2id = new Dictionary<object, int> {{new object(), 0}};
    3725      typeCache = new Dictionary<Type, int>();
     
    7967      }
    8068
    81       if (primitiveSerializers.ContainsKey(value.GetType())) {
    82        
     69      IFormatter formatter = persistenceConfiguration.GetFormatter(XmlFormat.Instance, value.GetType());     
     70      if (formatter != null) {
    8371        yield return new PrimitiveToken(
    8472          accessor,
    85           primitiveSerializers[value.GetType()].Serialize(value),
     73          formatter.Serialize(value),         
    8674          id);
    8775        yield break;
     
    8977     
    9078      yield return new BeginToken(accessor, id);
    91       IDecomposer custom = FindCompoundSerializer(value.GetType());
     79      IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType());     
    9280
    93       if (custom != null) {
    94         foreach (object o in custom.Serialize(value)) {
     81      if (decomposer != null) {
     82        foreach (object o in decomposer.Serialize(value)) {
    9583          IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o));
    9684          while (iterator.MoveNext())
     
    117105      yield return new EndToken(accessor, id);
    118106    }
    119 
    120     private IDecomposer FindCompoundSerializer(Type type) {
    121       foreach (IDecomposer s in compoundSerializers) {
    122         if (s.CanSerialize(type))
    123           return s;
    124       }
    125       return null;
    126     }
     107   
    127108  }
    128109}
Note: See TracChangeset for help on using the changeset viewer.