Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1358 for branches


Ignore:
Timestamp:
03/19/09 11:00:29 (16 years ago)
Author:
epitzer
Message:

Better formatting configuration interface. (#506)

Location:
branches/New Persistence Exploration/Persistence
Files:
4 edited

Legend:

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

    r1357 r1358  
    7777    private readonly Dictionary<Type, Handler> handlers;
    7878    private readonly Stack<IAccessibleObject> compositeStack;
    79     private readonly PersistenceConfiguration persistenceConfiguration;
     79    private readonly Configuration configuration;
    8080    private readonly Dictionary<int, Type> typeIds;   
    8181    private List<Thunk> finalFixes;
     
    8383    public DeSerializer(
    8484      IEnumerable<KeyValuePair<string, int>> typeCache,
    85       PersistenceConfiguration persistenceConfiguration) {
    86       this.persistenceConfiguration = persistenceConfiguration;
     85      Configuration configuration) {
     86      this.configuration = configuration;
    8787      id2obj = new Dictionary<int, object>();
    8888      compositeStack = new Stack<IAccessibleObject>();
     
    116116      object instance;     
    117117      Type type = typeIds[(int)start.TypeId];
    118       if (persistenceConfiguration.GetDecomposer(type) != null) {
     118      if (configuration.GetDecomposer(type) != null) {
    119119        instance = new ParentReference();
    120120        compositeStack.Push(new CustomObject(instance));       
     
    132132      EndToken end = (EndToken)token;
    133133      Type type = typeIds[(int)end.TypeId];
    134       IDecomposer decomposer = persistenceConfiguration.GetDecomposer(type);
     134      IDecomposer decomposer = configuration.GetDecomposer(type);
    135135      if (decomposer != null) {
    136136        CustomObject customObject = (CustomObject)compositeStack.Pop();
     
    150150      PrimitiveToken primitive = (PrimitiveToken)token;
    151151      Type type = typeIds[(int)primitive.TypeId];
    152       object value = persistenceConfiguration
    153         .GetFormatter(XmlFormat.Instance, type)
     152      object value = configuration
     153        .GetFormatter(type)
    154154        .DeSerialize(primitive.SerialData);
    155155      if ( ! value.GetType().IsValueType )
  • branches/New Persistence Exploration/Persistence/Persistence/PersistenceConfiguration.cs

    r1357 r1358  
    66namespace HeuristicLab.Persistence {
    77
    8   public class PersistenceConfiguration {
     8  public class Configuration {
    99
    10     private readonly List<IFormatter> formatters;
     10    private readonly Dictionary<Type, IFormatter> formatters;
    1111    private readonly List<IDecomposer> decomposers;
    12     private readonly Dictionary<IFormat, Dictionary<Type, IFormatter>> formatterConfig;
    1312    private readonly Dictionary<Type, IDecomposer> decomposerCache;
     13    public readonly IFormat Format;
    1414
    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 {
    1758      get {
    1859        if (instance == null) {
    19           instance = new PersistenceConfiguration();
    20           instance.DefaultConfig();
     60          instance = new ConfigurationService();
    2161        }
    2262        return instance;
     
    2464    }
    2565
    26     public PersistenceConfiguration() {
    27       formatters = new List<IFormatter>();
     66    public ConfigurationService() {
     67      formatters = new Dictionary<IFormat, List<IFormatter>>();
    2868      decomposers = new List<IDecomposer>();
    29       formatterConfig = new Dictionary<IFormat, Dictionary<Type, IFormatter>>();
    30       decomposerCache = new Dictionary<Type, IDecomposer>();
     69      Reset();
    3170    }
    3271
    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) {
    3683          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);
    3889          } catch (MissingMethodException) {
    3990            Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
    40           }
     91          }         
    4192        }
    42         if (t.GetInterface(typeof(IDecomposer).FullName) != null) {
     93        if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
    4394          try {
    4495            decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
     
    50101    }
    51102
    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);
    65108      }
    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);
    90110    }
    91111
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1357 r1358  
    1313    private readonly Dictionary<object, int> obj2id;
    1414    private readonly Dictionary<Type, int> typeCache;
    15     private readonly PersistenceConfiguration persistenceConfiguration;
     15    private readonly Configuration configuration;
    1616
    1717    public Dictionary<string, int> TypeCache {
     
    2424    }
    2525
    26     public Serializer(object obj, PersistenceConfiguration persistenceConfiguration) :       
    27       this(obj, persistenceConfiguration, "ROOT") { }
     26    public Serializer(object obj, Configuration configuration) :       
     27      this(obj, configuration, "ROOT") { }
    2828
    29     public Serializer(object obj, PersistenceConfiguration persistenceConfiguration, string rootName) {
     29    public Serializer(object obj, Configuration configuration, string rootName) {
    3030      this.obj = obj;
    3131      this.rootName = rootName;
    32       this.persistenceConfiguration = persistenceConfiguration;     
     32      this.configuration = configuration;     
    3333      obj2id = new Dictionary<object, int> {{new object(), 0}};
    3434      typeCache = new Dictionary<Type, int>();
     
    6262        obj2id.Add(value, (int)id);
    6363      }
    64       IFormatter formatter = persistenceConfiguration.GetFormatter(XmlFormat.Instance, value.GetType());
     64      IFormatter formatter = configuration.GetFormatter(value.GetType());
    6565      if (formatter != null)
    6666        return PrimitiveEnumeration(accessor.Name, typeId, formatter.Serialize(value), id);
    67       IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType());
     67      IDecomposer decomposer = configuration.GetDecomposer(value.GetType());
    6868      if (decomposer != null)
    6969        return CompositeEnumeration(accessor.Name, decomposer.Serialize(value), id, typeId);           
  • branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs

    r1357 r1358  
    145145  public class NewSerializationTest {
    146146
    147     public static void Test1() {
     147    public static void Test1() {     
    148148      Root r = new Root();
    149149      r.selfReferences = new List<Root> {r, r};
     
    152152      r.dict.Add("two", 2);
    153153      r.dict.Add("three", 3);
    154       Serializer s = new Serializer(r, PersistenceConfiguration.Instance);       
     154      Serializer s = new Serializer(r, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
    155155      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
    156156      StreamWriter writer = new StreamWriter("test.xml");
     
    171171      DeSerializer deSerializer = new DeSerializer(
    172172        XmlParser.ParseTypeCache(new StreamReader("test-types.xml")),
    173         PersistenceConfiguration.Instance);       
     173        ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
    174174      object o = deSerializer.DeSerialize(parser);
    175175      Root t = CloningFactory.DefaultClone(r);
     
    179179    public static void Test2() {
    180180      Manager m = new Manager();     
    181       Serializer s = new Serializer(m, PersistenceConfiguration.Instance);
     181      Serializer s = new Serializer(m,
     182        ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));
    182183      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
    183184      StreamWriter writer = new StreamWriter("test2.xml");
     
    191192      DeSerializer deSerializer = new DeSerializer(
    192193        XmlParser.ParseTypeCache(new StreamReader("test-types.xml")),
    193         PersistenceConfiguration.Instance);             
     194        ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance));       
    194195      object o = deSerializer.DeSerialize(parser);
    195196      Manager n = CloningFactory.DefaultClone(m);
Note: See TracChangeset for help on using the changeset viewer.