Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/PersistenceConfiguration.cs @ 1358

Last change on this file since 1358 was 1358, checked in by epitzer, 15 years ago

Better formatting configuration interface. (#506)

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using HeuristicLab.Persistence.Interfaces;
5
6namespace HeuristicLab.Persistence {
7
8  public class Configuration {
9
10    private readonly Dictionary<Type, IFormatter> formatters;
11    private readonly List<IDecomposer> decomposers;
12    private readonly Dictionary<Type, IDecomposer> decomposerCache;
13    public readonly IFormat Format;
14
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 {
58      get {
59        if (instance == null) {
60          instance = new ConfigurationService();
61        }
62        return instance;
63      }
64    }
65
66    public ConfigurationService() {
67      formatters = new Dictionary<IFormat, List<IFormatter>>();
68      decomposers = new List<IDecomposer>();
69      Reset();
70    }
71
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) {
83          try {
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);
89          } catch (MissingMethodException) {
90            Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
91          }         
92        }
93        if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
94          try {
95            decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
96          } catch (MissingMethodException) {
97            Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
98          }
99        }
100      }
101    }
102
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);
108      }
109      return new Configuration(formatterConfig, decomposers);
110    }
111
112  }
113 
114}
Note: See TracBrowser for help on using the repository browser.