Changeset 1358 for branches/New Persistence Exploration/Persistence
- Timestamp:
- 03/19/09 11:00:29 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1357 r1358 77 77 private readonly Dictionary<Type, Handler> handlers; 78 78 private readonly Stack<IAccessibleObject> compositeStack; 79 private readonly PersistenceConfiguration persistenceConfiguration;79 private readonly Configuration configuration; 80 80 private readonly Dictionary<int, Type> typeIds; 81 81 private List<Thunk> finalFixes; … … 83 83 public DeSerializer( 84 84 IEnumerable<KeyValuePair<string, int>> typeCache, 85 PersistenceConfiguration persistenceConfiguration) {86 this. persistenceConfiguration = persistenceConfiguration;85 Configuration configuration) { 86 this.configuration = configuration; 87 87 id2obj = new Dictionary<int, object>(); 88 88 compositeStack = new Stack<IAccessibleObject>(); … … 116 116 object instance; 117 117 Type type = typeIds[(int)start.TypeId]; 118 if ( persistenceConfiguration.GetDecomposer(type) != null) {118 if (configuration.GetDecomposer(type) != null) { 119 119 instance = new ParentReference(); 120 120 compositeStack.Push(new CustomObject(instance)); … … 132 132 EndToken end = (EndToken)token; 133 133 Type type = typeIds[(int)end.TypeId]; 134 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(type);134 IDecomposer decomposer = configuration.GetDecomposer(type); 135 135 if (decomposer != null) { 136 136 CustomObject customObject = (CustomObject)compositeStack.Pop(); … … 150 150 PrimitiveToken primitive = (PrimitiveToken)token; 151 151 Type type = typeIds[(int)primitive.TypeId]; 152 object value = persistenceConfiguration153 .GetFormatter( XmlFormat.Instance,type)152 object value = configuration 153 .GetFormatter(type) 154 154 .DeSerialize(primitive.SerialData); 155 155 if ( ! value.GetType().IsValueType ) -
branches/New Persistence Exploration/Persistence/Persistence/PersistenceConfiguration.cs
r1357 r1358 6 6 namespace HeuristicLab.Persistence { 7 7 8 public class PersistenceConfiguration {8 public class Configuration { 9 9 10 private readonly List<IFormatter> formatters;10 private readonly Dictionary<Type, IFormatter> formatters; 11 11 private readonly List<IDecomposer> decomposers; 12 private readonly Dictionary<IFormat, Dictionary<Type, IFormatter>> formatterConfig;13 12 private readonly Dictionary<Type, IDecomposer> decomposerCache; 13 public readonly IFormat Format; 14 14 15 private static PersistenceConfiguration instance; 16 public static PersistenceConfiguration Instance { 15 public Configuration(Dictionary<Type, IFormatter> formatters, IEnumerable<IDecomposer> decomposers) { 16 this.formatters = new Dictionary<Type, IFormatter>(); 17 foreach ( var pair in formatters ) { 18 if (Format == null) { 19 Format = pair.Value.Format; 20 } else if (pair.Value.Format != Format ) { 21 throw new ArgumentException("All formatters must have the same IFormat."); 22 } 23 this.formatters.Add(pair.Key, pair.Value); 24 } 25 this.decomposers = new List<IDecomposer>(decomposers); 26 decomposerCache = new Dictionary<Type, IDecomposer>(); 27 } 28 29 public IFormatter GetFormatter(Type type) { 30 IFormatter formatter; 31 formatters.TryGetValue(type, out formatter); 32 return formatter; 33 } 34 35 public IDecomposer GetDecomposer(Type type) { 36 IDecomposer decomposer; 37 decomposerCache.TryGetValue(type, out decomposer); 38 if (decomposer != null) 39 return decomposer; 40 foreach (IDecomposer d in decomposers) { 41 if (d.CanSerialize(type)) { 42 decomposerCache.Add(type, d); 43 return d; 44 } 45 } 46 return null; 47 } 48 49 } 50 51 public class ConfigurationService { 52 53 private readonly Dictionary<IFormat, List<IFormatter>> formatters; 54 private readonly List<IDecomposer> decomposers; 55 56 private static ConfigurationService instance; 57 public static ConfigurationService Instance { 17 58 get { 18 59 if (instance == null) { 19 instance = new PersistenceConfiguration(); 20 instance.DefaultConfig(); 60 instance = new ConfigurationService(); 21 61 } 22 62 return instance; … … 24 64 } 25 65 26 public PersistenceConfiguration() {27 formatters = new List<IFormatter>();66 public ConfigurationService() { 67 formatters = new Dictionary<IFormat, List<IFormatter>>(); 28 68 decomposers = new List<IDecomposer>(); 29 formatterConfig = new Dictionary<IFormat, Dictionary<Type, IFormatter>>(); 30 decomposerCache = new Dictionary<Type, IDecomposer>(); 69 Reset(); 31 70 } 32 71 33 public void DiscoverFrom(Assembly a) { 34 foreach ( Type t in a.GetTypes() ) { 35 if (t.GetInterface(typeof(IFormatter).FullName) != null) { 72 public void Reset() { 73 Assembly defaultAssembly = Assembly.GetExecutingAssembly(); 74 DiscoverFrom(defaultAssembly); 75 foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) 76 if ( a != defaultAssembly ) 77 DiscoverFrom(a); 78 } 79 80 protected void DiscoverFrom(Assembly a) { 81 foreach (Type t in a.GetTypes()) { 82 if (t.GetInterface(typeof (IFormatter).FullName) != null) { 36 83 try { 37 formatters.Add((IFormatter) Activator.CreateInstance(t, true)); 84 IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true); 85 if ( ! formatters.ContainsKey(formatter.Format) ) { 86 formatters.Add(formatter.Format, new List<IFormatter>()); 87 } 88 formatters[formatter.Format].Add(formatter); 38 89 } catch (MissingMethodException) { 39 90 Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName); 40 } 91 } 41 92 } 42 if (t.GetInterface(typeof (IDecomposer).FullName) != null) {93 if (t.GetInterface(typeof (IDecomposer).FullName) != null) { 43 94 try { 44 95 decomposers.Add((IDecomposer) Activator.CreateInstance(t, true)); … … 50 101 } 51 102 52 public void DiscoverAll() { 53 DiscoverFrom(Assembly.GetAssembly(this.GetType())); 54 foreach ( Assembly a in AppDomain.CurrentDomain.GetAssemblies() ) 55 DiscoverFrom(a); 56 } 57 58 public void DefaultConfig() { 59 DiscoverAll(); 60 foreach ( IFormatter f in formatters ) { 61 if ( ! formatterConfig.ContainsKey(f.Format) ) 62 formatterConfig.Add(f.Format, new Dictionary<Type, IFormatter>()); 63 if (!formatterConfig[f.Format].ContainsKey(f.Type)) 64 formatterConfig[f.Format][f.Type] = f; 103 public Configuration GetDefaultConfig(IFormat format) { 104 Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>(); 105 foreach ( IFormatter f in formatters[format] ) { 106 if ( ! formatterConfig.ContainsKey(f.Type) ) 107 formatterConfig.Add(f.Type, f); 65 108 } 66 } 67 68 public IFormatter GetFormatter(IFormat format, Type type) { 69 Dictionary<Type, IFormatter> typeMap; 70 formatterConfig.TryGetValue(format, out typeMap); 71 if ( typeMap == null ) 72 return null; 73 IFormatter formatter; 74 typeMap.TryGetValue(type, out formatter); 75 return formatter; 76 } 77 78 public IDecomposer GetDecomposer(Type type) { 79 IDecomposer decomposer; 80 decomposerCache.TryGetValue(type, out decomposer); 81 if (decomposer != null) 82 return decomposer; 83 foreach ( IDecomposer d in decomposers ) { 84 if (d.CanSerialize(type)) { 85 decomposerCache.Add(type, d); 86 return d; 87 } 88 } 89 return null; 109 return new Configuration(formatterConfig, decomposers); 90 110 } 91 111 -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1357 r1358 13 13 private readonly Dictionary<object, int> obj2id; 14 14 private readonly Dictionary<Type, int> typeCache; 15 private readonly PersistenceConfiguration persistenceConfiguration;15 private readonly Configuration configuration; 16 16 17 17 public Dictionary<string, int> TypeCache { … … 24 24 } 25 25 26 public Serializer(object obj, PersistenceConfiguration persistenceConfiguration) :27 this(obj, persistenceConfiguration, "ROOT") { }26 public Serializer(object obj, Configuration configuration) : 27 this(obj, configuration, "ROOT") { } 28 28 29 public Serializer(object obj, PersistenceConfiguration persistenceConfiguration, string rootName) {29 public Serializer(object obj, Configuration configuration, string rootName) { 30 30 this.obj = obj; 31 31 this.rootName = rootName; 32 this. persistenceConfiguration = persistenceConfiguration;32 this.configuration = configuration; 33 33 obj2id = new Dictionary<object, int> {{new object(), 0}}; 34 34 typeCache = new Dictionary<Type, int>(); … … 62 62 obj2id.Add(value, (int)id); 63 63 } 64 IFormatter formatter = persistenceConfiguration.GetFormatter(XmlFormat.Instance,value.GetType());64 IFormatter formatter = configuration.GetFormatter(value.GetType()); 65 65 if (formatter != null) 66 66 return PrimitiveEnumeration(accessor.Name, typeId, formatter.Serialize(value), id); 67 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType());67 IDecomposer decomposer = configuration.GetDecomposer(value.GetType()); 68 68 if (decomposer != null) 69 69 return CompositeEnumeration(accessor.Name, decomposer.Serialize(value), id, typeId); -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1357 r1358 145 145 public class NewSerializationTest { 146 146 147 public static void Test1() { 147 public static void Test1() { 148 148 Root r = new Root(); 149 149 r.selfReferences = new List<Root> {r, r}; … … 152 152 r.dict.Add("two", 2); 153 153 r.dict.Add("three", 3); 154 Serializer s = new Serializer(r, PersistenceConfiguration.Instance);154 Serializer s = new Serializer(r, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 155 155 Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); 156 156 StreamWriter writer = new StreamWriter("test.xml"); … … 171 171 DeSerializer deSerializer = new DeSerializer( 172 172 XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), 173 PersistenceConfiguration.Instance);173 ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 174 174 object o = deSerializer.DeSerialize(parser); 175 175 Root t = CloningFactory.DefaultClone(r); … … 179 179 public static void Test2() { 180 180 Manager m = new Manager(); 181 Serializer s = new Serializer(m, PersistenceConfiguration.Instance); 181 Serializer s = new Serializer(m, 182 ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 182 183 Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); 183 184 StreamWriter writer = new StreamWriter("test2.xml"); … … 191 192 DeSerializer deSerializer = new DeSerializer( 192 193 XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), 193 PersistenceConfiguration.Instance);194 ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 194 195 object o = deSerializer.DeSerialize(parser); 195 196 Manager n = CloningFactory.DefaultClone(m);
Note: See TracChangeset
for help on using the changeset viewer.