1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Persistence.Interfaces;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Persistence.Core {
|
---|
8 |
|
---|
9 | /// <summary>
|
---|
10 | /// Defines the set of primitive and composite serializers that are to be used
|
---|
11 | /// for a certain seraial format. The configuration can be obtained from the
|
---|
12 | /// <c>ConfigurationService</c>.
|
---|
13 | /// </summary>
|
---|
14 | [StorableClass]
|
---|
15 | public class Configuration {
|
---|
16 |
|
---|
17 | [Storable]
|
---|
18 | private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
|
---|
19 |
|
---|
20 | [Storable]
|
---|
21 | private readonly List<ICompositeSerializer> compositeSerializers;
|
---|
22 | private readonly Dictionary<Type, ICompositeSerializer> compositeSerializerCache;
|
---|
23 |
|
---|
24 | /// <summary>
|
---|
25 | /// Gets the format.
|
---|
26 | /// </summary>
|
---|
27 | /// <value>The format.</value>
|
---|
28 | [Storable]
|
---|
29 | public IFormat Format { get; private set; }
|
---|
30 |
|
---|
31 | [StorableConstructor]
|
---|
32 | private Configuration(bool isDeserializing) {
|
---|
33 | compositeSerializerCache = new Dictionary<Type, ICompositeSerializer>();
|
---|
34 | if (isDeserializing)
|
---|
35 | return;
|
---|
36 | primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
|
---|
37 | compositeSerializers = new List<ICompositeSerializer>();
|
---|
38 | }
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// Initializes a new instance of the <see cref="Configuration"/> class.
|
---|
42 | /// </summary>
|
---|
43 | /// <param name="format">The format.</param>
|
---|
44 | /// <param name="primitiveSerializers">The primitive serializers.</param>
|
---|
45 | /// <param name="compositeSerializers">The composite serializers.</param>
|
---|
46 | public Configuration(IFormat format,
|
---|
47 | IEnumerable<IPrimitiveSerializer> primitiveSerializers,
|
---|
48 | IEnumerable<ICompositeSerializer> compositeSerializers)
|
---|
49 | : this(false) {
|
---|
50 | this.Format = format;
|
---|
51 | this.compositeSerializers.AddRange(compositeSerializers);
|
---|
52 | foreach (var primitiveSerializer in primitiveSerializers) {
|
---|
53 | if (primitiveSerializer.SerialDataType != format.SerialDataType)
|
---|
54 | throw new ArgumentException(string.Format(
|
---|
55 | "primitive serializer's ({0}) serialized data type ({1}) " + Environment.NewLine +
|
---|
56 | "is not compatible with selected format's ({2}) seriali data type ({3})",
|
---|
57 | primitiveSerializers.GetType().FullName, primitiveSerializer.SerialDataType.FullName,
|
---|
58 | format.Name, format.SerialDataType.FullName));
|
---|
59 | this.primitiveSerializers.Add(primitiveSerializer.SourceType, primitiveSerializer);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Gets the primitive serializers.
|
---|
65 | /// </summary>
|
---|
66 | /// <value>The primitive serializers.</value>
|
---|
67 | public IEnumerable<IPrimitiveSerializer> PrimitiveSerializers {
|
---|
68 | get { return primitiveSerializers.Values; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Gets the composite serializers.
|
---|
73 | /// </summary>
|
---|
74 | /// <value>An enumerable of composite serializers.</value>
|
---|
75 | public IEnumerable<ICompositeSerializer> CompositeSerializers {
|
---|
76 | get { return compositeSerializers; }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Gets the primitive serializer.
|
---|
81 | /// </summary>
|
---|
82 | /// <param name="type">The type.</param>
|
---|
83 | /// <returns>The appropriate primitive serializer for the type.</returns>
|
---|
84 | public IPrimitiveSerializer GetPrimitiveSerializer(Type type) {
|
---|
85 | IPrimitiveSerializer primitiveSerializer;
|
---|
86 | primitiveSerializers.TryGetValue(type, out primitiveSerializer);
|
---|
87 | return primitiveSerializer;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /// <summary>
|
---|
91 | /// Gets the composite serializer for a given type.
|
---|
92 | /// </summary>
|
---|
93 | /// <param name="type">The type.</param>
|
---|
94 | /// <returns>The first matching composite serializer for the type.</returns>
|
---|
95 | public ICompositeSerializer GetCompositeSerializer(Type type) {
|
---|
96 | if (compositeSerializerCache.ContainsKey(type))
|
---|
97 | return compositeSerializerCache[type];
|
---|
98 | foreach (ICompositeSerializer d in compositeSerializers) {
|
---|
99 | if (d.CanSerialize(type)) {
|
---|
100 | compositeSerializerCache.Add(type, d);
|
---|
101 | return d;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | compositeSerializerCache.Add(type, null);
|
---|
105 | return null;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /// <summary>
|
---|
109 | /// Copies this configuration and re-instantiates all serializers.
|
---|
110 | /// </summary>
|
---|
111 | /// <returns>A new <see cref="Configuration"/></returns>
|
---|
112 | public Configuration Copy() {
|
---|
113 | var config = new Configuration(false);
|
---|
114 | config.Format = Format;
|
---|
115 | foreach (var ps in primitiveSerializers)
|
---|
116 | config.primitiveSerializers.Add(
|
---|
117 | ps.Key,
|
---|
118 | (IPrimitiveSerializer)Activator.CreateInstance(ps.Value.GetType()));
|
---|
119 | foreach (var cs in compositeSerializers)
|
---|
120 | config.compositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(cs.GetType()));
|
---|
121 | return config;
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | } |
---|