[1349] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Reflection;
|
---|
[1357] | 4 | using HeuristicLab.Persistence.Interfaces;
|
---|
[1349] | 5 |
|
---|
[1357] | 6 | namespace HeuristicLab.Persistence {
|
---|
[1349] | 7 |
|
---|
[1358] | 8 | public class Configuration {
|
---|
[1349] | 9 |
|
---|
[1358] | 10 | private readonly Dictionary<Type, IFormatter> formatters;
|
---|
[1349] | 11 | private readonly List<IDecomposer> decomposers;
|
---|
| 12 | private readonly Dictionary<Type, IDecomposer> decomposerCache;
|
---|
[1358] | 13 | public readonly IFormat Format;
|
---|
[1349] | 14 |
|
---|
[1358] | 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 {
|
---|
[1349] | 58 | get {
|
---|
| 59 | if (instance == null) {
|
---|
[1358] | 60 | instance = new ConfigurationService();
|
---|
[1349] | 61 | }
|
---|
| 62 | return instance;
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[1358] | 66 | public ConfigurationService() {
|
---|
| 67 | formatters = new Dictionary<IFormat, List<IFormatter>>();
|
---|
[1349] | 68 | decomposers = new List<IDecomposer>();
|
---|
[1358] | 69 | Reset();
|
---|
[1349] | 70 | }
|
---|
| 71 |
|
---|
[1358] | 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) {
|
---|
[1349] | 83 | try {
|
---|
[1358] | 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);
|
---|
[1349] | 89 | } catch (MissingMethodException) {
|
---|
| 90 | Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
|
---|
[1358] | 91 | }
|
---|
[1349] | 92 | }
|
---|
[1358] | 93 | if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
|
---|
[1349] | 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 |
|
---|
[1358] | 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);
|
---|
[1349] | 108 | }
|
---|
[1358] | 109 | return new Configuration(formatterConfig, decomposers);
|
---|
[1349] | 110 | }
|
---|
| 111 |
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | } |
---|