- Timestamp:
- 05/15/09 12:29:13 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs
r1703 r1823 17 17 private static ConfigurationService instance; 18 18 private readonly Dictionary<IFormat, Configuration> customConfigurations; 19 public Dictionary<Type, List<I Formatter>> Formatters { get; private set; }20 public List<I Decomposer> Decomposers { get; private set; }19 public Dictionary<Type, List<IPrimitiveSerializer>> PrimitiveSerializers { get; private set; } 20 public List<ICompositeSerializer> CompositeSerializers { get; private set; } 21 21 public List<IFormat> Formats { get; private set; } 22 22 … … 30 30 31 31 private ConfigurationService() { 32 Formatters = new Dictionary<Type, List<IFormatter>>();33 Decomposers = new List<IDecomposer>();32 PrimitiveSerializers = new Dictionary<Type, List<IPrimitiveSerializer>>(); 33 CompositeSerializers = new List<ICompositeSerializer>(); 34 34 customConfigurations = new Dictionary<IFormat, Configuration>(); 35 35 Formats = new List<IFormat>(); … … 94 94 public void Reset() { 95 95 customConfigurations.Clear(); 96 Formatters.Clear();97 Decomposers.Clear();96 PrimitiveSerializers.Clear(); 97 CompositeSerializers.Clear(); 98 98 Assembly defaultAssembly = Assembly.GetExecutingAssembly(); 99 99 DiscoverFrom(defaultAssembly); … … 101 101 if (a != defaultAssembly) 102 102 DiscoverFrom(a); 103 Sort Decomposers();103 SortCompositeSerializers(); 104 104 } 105 105 106 class PriortiySorter : IComparer<I Decomposer> {107 public int Compare(I Decomposer x, IDecomposer y) {106 class PriortiySorter : IComparer<ICompositeSerializer> { 107 public int Compare(ICompositeSerializer x, ICompositeSerializer y) { 108 108 return y.Priority - x.Priority; 109 109 } 110 110 } 111 111 112 protected void Sort Decomposers() {113 Decomposers.Sort(new PriortiySorter());112 protected void SortCompositeSerializers() { 113 CompositeSerializers.Sort(new PriortiySorter()); 114 114 } 115 115 116 116 protected void DiscoverFrom(Assembly a) { 117 117 foreach (Type t in a.GetTypes()) { 118 if (t.GetInterface(typeof(I Formatter).FullName) != null) {118 if (t.GetInterface(typeof(IPrimitiveSerializer).FullName) != null) { 119 119 try { 120 IFormatter formatter = (IFormatter)Activator.CreateInstance(t, true); 121 if (!Formatters.ContainsKey(formatter.SerialDataType)) { 122 Formatters.Add(formatter.SerialDataType, new List<IFormatter>()); 120 IPrimitiveSerializer primitiveSerializer = 121 (IPrimitiveSerializer)Activator.CreateInstance(t, true); 122 if (!PrimitiveSerializers.ContainsKey(primitiveSerializer.SerialDataType)) { 123 PrimitiveSerializers.Add(primitiveSerializer.SerialDataType, new List<IPrimitiveSerializer>()); 123 124 } 124 Formatters[formatter.SerialDataType].Add(formatter);125 Logger.Debug(String.Format("discovered formatter {0} ({1} -> {2})",125 PrimitiveSerializers[primitiveSerializer.SerialDataType].Add(primitiveSerializer); 126 Logger.Debug(String.Format("discovered primitive serializer {0} ({1} -> {2})", 126 127 t.VersionInvariantName(), 127 formatter.SourceType.VersionInvariantName(),128 formatter.SerialDataType.VersionInvariantName()));128 primitiveSerializer.SourceType.AssemblyQualifiedName, 129 primitiveSerializer.SerialDataType.AssemblyQualifiedName)); 129 130 } catch (MissingMethodException e) { 130 Logger.Warn("Could not instantiate " + t. VersionInvariantName(), e);131 Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e); 131 132 } catch (ArgumentException e) { 132 Logger.Warn("Could not instantiate " + t. VersionInvariantName(), e);133 Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e); 133 134 } 134 135 } 135 if (t.GetInterface(typeof(I Decomposer).FullName) != null) {136 if (t.GetInterface(typeof(ICompositeSerializer).FullName) != null) { 136 137 try { 137 Decomposers.Add((IDecomposer)Activator.CreateInstance(t, true));138 Logger.Debug("discovered decomposer " + t.VersionInvariantName());138 CompositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(t, true)); 139 Logger.Debug("discovered composite serializer " + t.AssemblyQualifiedName); 139 140 } catch (MissingMethodException e) { 140 Logger.Warn("Could not instantiate " + t. VersionInvariantName(), e);141 Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e); 141 142 } catch (ArgumentException e) { 142 Logger.Warn("Could not instantiate " + t. VersionInvariantName(), e);143 Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e); 143 144 } 144 145 } … … 150 151 format.Name, 151 152 format.SerialDataType, 152 t. VersionInvariantName()));153 t.AssemblyQualifiedName)); 153 154 } catch (MissingMethodException e) { 154 Logger.Warn("Could not instantiate " + t. VersionInvariantName(), e);155 Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e); 155 156 } catch (ArgumentException e) { 156 Logger.Warn("Could not instantiate " + t. VersionInvariantName(), e);157 Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e); 157 158 } 158 159 } … … 161 162 162 163 public Configuration GetDefaultConfig(IFormat format) { 163 Dictionary<Type, I Formatter> formatterConfig = new Dictionary<Type, IFormatter>();164 if ( Formatters.ContainsKey(format.SerialDataType)) {165 foreach (I Formatter f in Formatters[format.SerialDataType]) {166 if (! formatterConfig.ContainsKey(f.SourceType))167 formatterConfig.Add(f.SourceType, f);164 Dictionary<Type, IPrimitiveSerializer> primitiveConfig = new Dictionary<Type, IPrimitiveSerializer>(); 165 if (PrimitiveSerializers.ContainsKey(format.SerialDataType)) { 166 foreach (IPrimitiveSerializer f in PrimitiveSerializers[format.SerialDataType]) { 167 if (!primitiveConfig.ContainsKey(f.SourceType)) 168 primitiveConfig.Add(f.SourceType, f); 168 169 } 169 170 } else { 170 171 Logger.Warn(String.Format( 171 "No formatters found for format {0} with serial data type {1}",172 format.GetType(). VersionInvariantName(),173 format.SerialDataType. VersionInvariantName()));172 "No primitive serializers found for format {0} with serial data type {1}", 173 format.GetType().AssemblyQualifiedName, 174 format.SerialDataType.AssemblyQualifiedName)); 174 175 } 175 return new Configuration(format, formatterConfig.Values, Decomposers.Where((d) => d.Priority > 0)); 176 return new Configuration( 177 format, 178 primitiveConfig.Values, 179 CompositeSerializers.Where((d) => d.Priority > 0)); 176 180 } 177 181
Note: See TracChangeset
for help on using the changeset viewer.