Changeset 1323 for branches/New Persistence Exploration/Persistence
- Timestamp:
- 03/11/09 14:20:51 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 2 added
- 1 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/CompoundSerializers.cs
r1319 r1323 30 30 object instance = Activator.CreateInstance(t); 31 31 foreach (object o in objects) { 32 t.GetMethod("Add").Invoke(instance, new object[] { o });32 t.GetMethod("Add").Invoke(instance, new[] { o }); 33 33 } 34 34 return instance; -
branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj
r1322 r1323 57 57 <Compile Include="StorableAttributeTests.cs" /> 58 58 <Compile Include="StorableAttribute.cs" /> 59 <Compile Include=" NewSerializationTest.cs" />59 <Compile Include="Test\NewSerializationTest.cs" /> 60 60 <Compile Include="SerializationTest.cs" /> 61 61 <Compile Include="XmlFormatter.cs" /> -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1318 r1323 2 2 using System.Collections; 3 3 using System; 4 using System.Reflection;5 4 6 5 namespace Persistence { … … 8 7 public class Serializer : IEnumerable<ISerializationToken> { 9 8 10 private object obj;11 private string rootName;12 private Dictionary<object, int> obj2id;13 private Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;14 private List<ICompoundSerializer> compoundSerializers;9 private readonly object obj; 10 private readonly string rootName; 11 private readonly Dictionary<object, int> obj2id; 12 private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers; 13 private readonly List<ICompoundSerializer> compoundSerializers; 15 14 16 15 public Serializer(object obj) : … … 34 33 } 35 34 this.compoundSerializers = new List<ICompoundSerializer>(compoundSerializers); 36 this.obj2id = new Dictionary<object, int>(); 37 obj2id.Add(new object(), 0); 38 } 35 obj2id = new Dictionary<object, int> {{new object(), 0}}; 36 } 39 37 40 38 IEnumerator IEnumerable.GetEnumerator() { 41 return this.GetEnumerator();39 return GetEnumerator(); 42 40 } 43 41 44 42 public IEnumerator<ISerializationToken> GetEnumerator() { 45 43 DataMemberAccessor rootAccessor = new DataMemberAccessor( 46 this.rootName, obj.GetType(), null, () => this.obj, null);44 rootName, obj.GetType(), null, () => obj, null); 47 45 IEnumerator<ISerializationToken> iterator = Serialize(rootAccessor); 48 46 while (iterator.MoveNext()) … … 54 52 if (value == null) { 55 53 yield return new NullReferenceToken(accessor.Name); 56 } else if ( this.primitiveSerializers.ContainsKey(value.GetType())) {57 yield return new PrimitiveToken(accessor, this.primitiveSerializers[value.GetType()].Serialize(value));58 } else if ( this.obj2id.ContainsKey(value)) {59 yield return new ReferenceToken(accessor.Name, this.obj2id[value]);54 } else if (primitiveSerializers.ContainsKey(value.GetType())) { 55 yield return new PrimitiveToken(accessor, primitiveSerializers[value.GetType()].Serialize(value)); 56 } else if (obj2id.ContainsKey(value)) { 57 yield return new ReferenceToken(accessor.Name, obj2id[value]); 60 58 } else { 61 59 int id = obj2id.Count; 62 this.obj2id.Add(value, id);60 obj2id.Add(value, id); 63 61 yield return new BeginToken(accessor, id); 64 ICompoundSerializer customSerializer = this.FindCompoundSerializer(value.GetType());62 ICompoundSerializer customSerializer = FindCompoundSerializer(value.GetType()); 65 63 if (customSerializer != null) { 66 foreach (object o bjin customSerializer.Serialize(value)) {67 IEnumerator<ISerializationToken> iterator = this.Serialize(new DataMemberAccessor(obj));64 foreach (object o in customSerializer.Serialize(value)) { 65 IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o)); 68 66 while (iterator.MoveNext()) 69 67 yield return iterator.Current; … … 74 72 StorableAttribute.GetAutostorableAccessors(value)) { 75 73 nSubComponents += 1; 76 IEnumerator<ISerializationToken> iterator = this.Serialize(mapping.Value);74 IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value); 77 75 while (iterator.MoveNext()) { 78 76 yield return iterator.Current; -
branches/New Persistence Exploration/Persistence/Persistence/StorableAttribute.cs
r1322 r1323 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Text;4 3 using System.Reflection; 5 using System.Collections;6 4 7 5 namespace Persistence { … … 13 11 public class StorableAttribute : Attribute { 14 12 15 private string name = null; 16 private object defaultValue = null; 17 18 public string Name { 19 get { return this.name; } 20 set { this.name = value; } 21 } 22 23 public object DefaultValue { 24 get { return this.defaultValue; } 25 set { this.defaultValue = value; } 26 } 13 public string Name { get; set; } 14 public object DefaultValue { get; set; } 27 15 28 16 private const BindingFlags instanceMembers = … … 31 19 BindingFlags.NonPublic; 32 20 33 public static Dictionary<string, DataMemberAccessor> GetAutostorableAccessors(object obj) { 34 Dictionary<string, DataMemberAccessor> autoStorableAccessors = 35 new Dictionary<string, DataMemberAccessor>(); 21 22 public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>> GetStorableMembers(object obj) { 36 23 foreach (MemberInfo memberInfo in obj.GetType().GetMembers(instanceMembers)) { 37 24 foreach (object attribute in memberInfo.GetCustomAttributes(false)) { 38 StorableAttribute autoStorableAttribute =25 StorableAttribute storableAttribute = 39 26 attribute as StorableAttribute; 40 if (autoStorableAttribute != null) { 41 autoStorableAccessors.Add(memberInfo.Name, 42 new DataMemberAccessor(memberInfo, autoStorableAttribute, obj)); 27 if (storableAttribute != null) { 28 yield return new KeyValuePair<StorableAttribute, MemberInfo>(storableAttribute, memberInfo); 43 29 } 44 30 } 45 } 46 return autoStorableAccessors; 31 } 32 } 33 34 public static Dictionary<string, DataMemberAccessor> GetAutostorableAccessors(object obj) { 35 Dictionary<string, DataMemberAccessor> storableAccessors = 36 new Dictionary<string, DataMemberAccessor>(); 37 foreach (KeyValuePair<StorableAttribute, MemberInfo> pair in GetStorableMembers(obj)) { 38 storableAccessors.Add(pair.Value.Name, 39 new DataMemberAccessor(pair.Value, pair.Key, obj)); 40 } 41 return storableAccessors; 47 42 } 48 43 } … … 50 45 class CloningFactory { 51 46 52 private static CloningFactory instance = new CloningFactory();47 private static readonly CloningFactory instance = new CloningFactory(); 53 48 54 49 public static T DefaultClone<T>(T obj) { … … 56 51 } 57 52 58 private List<ICompoundSerializer> compoundSerializers;53 private readonly List<ICompoundSerializer> compoundSerializers; 59 54 60 55 public CloningFactory(IEnumerable<ICompoundSerializer> compoundSerializers) { … … 67 62 delegate object CloneMethod(object obj, Dictionary<object, object> twins); 68 63 69 Dictionary<Type, CloneMethod> cloneMethods = new Dictionary<Type, CloneMethod>();64 readonly Dictionary<Type, CloneMethod> cloneMethods = new Dictionary<Type, CloneMethod>(); 70 65 71 66 public T Clone<T>(T obj) { … … 75 70 76 71 public T Clone<T>(T obj, Dictionary<object, object> twins) { 77 if (obj == null)78 return default(T);79 80 72 ValueType t = obj as ValueType; 81 73 if (t != null) 82 return (T)obj; 74 return obj; 75 76 // ReSharper disable CompareNonConstrainedGenericWithNull 77 if (obj == null) 78 // ReSharper restore CompareNonConstrainedGenericWithNull 79 return default(T); 83 80 84 81 if (twins.ContainsKey(obj)) … … 99 96 100 97 private CloneMethod CreateCloneMethod(Type type) { 101 if (type.GetConstructor(new Type[] { type }) != null)98 if (type.GetConstructor(new[] { type }) != null) 102 99 return CopyConstructorCloneMethod.Create(type); 103 100 foreach (ICompoundSerializer serializer in compoundSerializers) … … 111 108 return new CopyConstructorCloneMethod(type).method; 112 109 } 113 private ConstructorInfo constructorInfo;110 private readonly ConstructorInfo constructorInfo; 114 111 public CopyConstructorCloneMethod(Type type) { 115 this.constructorInfo = type.GetConstructor(new Type[] { type }); 116 } 117 112 constructorInfo = type.GetConstructor(new[] { type }); 113 } 118 114 object method(object obj, Dictionary<object, object> twins) { 119 object newInstance = constructorInfo.Invoke(new object[] { obj });115 object newInstance = constructorInfo.Invoke(new[] { obj }); 120 116 twins.Add(obj, newInstance); 121 117 return newInstance; … … 128 124 return new CompoundSerializerCloneMethod(serializer, type, factory).method; 129 125 } 130 private ICompoundSerializer serializer;131 private Type type;132 private CloningFactory factory;126 private readonly ICompoundSerializer serializer; 127 private readonly Type type; 128 private readonly CloningFactory factory; 133 129 public CompoundSerializerCloneMethod(ICompoundSerializer serializer, 134 130 Type type, CloningFactory factory) { … … 138 134 } 139 135 object method(object obj, Dictionary<object, object> twins) { 140 return serializer.DeSerialize( 136 return serializer.DeSerialize( 141 137 Functional.Map( 142 138 serializer.Serialize(obj), 143 (o)=> factory.Clone(o, twins)),139 o => factory.Clone(o, twins)), 144 140 type); 145 141 } … … 148 144 class StorableAccessorCloneMethod { 149 145 150 delegate object Getter (object target);151 delegate void Setter (object target, object value);146 delegate object GetterMethod(object target); 147 delegate void SetterMethod(object target, object value); 152 148 153 149 struct FieldAccessor { 154 public GetterGetter;155 public SetterSetter;156 public FieldAccessor(Getter getter, Settersetter) {157 this.Getter = getter;158 this.Setter = setter;150 public readonly GetterMethod Getter; 151 public readonly SetterMethod Setter; 152 public FieldAccessor(GetterMethod getter, SetterMethod setter) { 153 Getter = getter; 154 Setter = setter; 159 155 } 160 156 } … … 169 165 } 170 166 171 private List<FieldAccessor> fieldAccessors;172 private CloningFactory factory;167 private readonly List<FieldAccessor> fieldAccessors; 168 private readonly CloningFactory factory; 173 169 174 170 public StorableAccessorCloneMethod(Type type, CloningFactory factory) { 175 171 this.factory = factory; 176 this.fieldAccessors = new List<FieldAccessor>();172 fieldAccessors = new List<FieldAccessor>(); 177 173 foreach (MemberInfo memberInfo in type.GetMembers(INSTANCE_MEMBERS)) { 178 174 foreach (object attribute in memberInfo.GetCustomAttributes(false)) { … … 188 184 PropertyInfo pi = (PropertyInfo)memberInfo; 189 185 fieldAccessors.Add(new FieldAccessor( 190 (target)=> pi.GetValue(target, null),186 target => pi.GetValue(target, null), 191 187 (target, value) => pi.SetValue(target, value, null))); 192 188 } … … 198 194 object newInstance = Activator.CreateInstance(obj.GetType()); 199 195 twins.Add(obj, newInstance); 200 foreach (FieldAccessor fa in this.fieldAccessors) {196 foreach (FieldAccessor fa in fieldAccessors) { 201 197 fa.Setter(newInstance, factory.Clone(fa.Getter(obj), twins)); 202 198 } … … 219 215 public delegate void Setter(object value); 220 216 221 private string name; 222 private Type type; 223 private object defaultValue; 224 private Getter getter; 225 private Setter setter; 217 private readonly Getter getter; 218 private readonly Setter setter; 226 219 227 220 public DataMemberAccessor( … … 231 224 if (memberInfo.MemberType == MemberTypes.Field) { 232 225 FieldInfo fieldInfo = (FieldInfo)memberInfo; 233 this.getter = () => fieldInfo.GetValue(obj);234 this.setter = (value)=> fieldInfo.SetValue(obj, value);235 this.type = fieldInfo.FieldType;226 getter = () => fieldInfo.GetValue(obj); 227 setter = value => fieldInfo.SetValue(obj, value); 228 Type = fieldInfo.FieldType; 236 229 } else if (memberInfo.MemberType == MemberTypes.Property) { 237 230 PropertyInfo propertyInfo = (PropertyInfo)memberInfo; … … 240 233 "Storable properties must implement both a Get and a Set accessor. "); 241 234 } 242 this.getter = () => propertyInfo.GetValue(obj, null);243 this.setter = (value)=> propertyInfo.SetValue(obj, value, null);244 this.type = propertyInfo.PropertyType;235 getter = () => propertyInfo.GetValue(obj, null); 236 setter = value => propertyInfo.SetValue(obj, value, null); 237 Type = propertyInfo.PropertyType; 245 238 } else { 246 239 throw new NotSupportedException( 247 240 "The Storable attribute can only be applied to fields and properties."); 248 241 } 249 this.name = autoStorableAttribute.Name != null ? autoStorableAttribute.Name :memberInfo.Name;250 this.defaultValue = autoStorableAttribute.DefaultValue;242 Name = autoStorableAttribute.Name ?? memberInfo.Name; 243 DefaultValue = autoStorableAttribute.DefaultValue; 251 244 } 252 245 … … 254 247 string name, Type type, object defaultValue, 255 248 Getter getter, Setter setter) { 256 this.name = name;257 this.type = type;258 this.defaultValue = defaultValue;249 Name = name; 250 Type = type; 251 DefaultValue = defaultValue; 259 252 this.getter = getter; 260 253 this.setter = setter; … … 262 255 263 256 public DataMemberAccessor(object o) { 264 this.name = ""; 265 this.type = o.GetType(); 266 this.defaultValue = null; 267 this.getter = () => o; 268 this.setter = null; 269 } 270 271 public string Name { 272 get { return this.name; } 273 } 274 275 public Type Type { 276 get { return this.type; } 277 } 278 279 public object DefaultValue { 280 get { return this.defaultValue; } 281 } 282 257 Name = ""; 258 Type = o.GetType(); 259 DefaultValue = null; 260 getter = () => o; 261 setter = null; 262 } 263 264 public string Name { get; private set; } 265 public Type Type { get; private set; } 266 public object DefaultValue { get; private set; } 283 267 public object Get() { 284 return this.getter(); 285 } 286 268 return getter(); 269 } 287 270 public void Set(object value) { 288 this.setter(value); 289 } 290 271 setter(value); 272 } 291 273 public override string ToString() { 292 274 return String.Format("DataMember({0}, {1}, {2}, {3}, {4}", 293 this.name,294 this.type == null ? "<null>" : this.type.FullName,295 this.defaultValue == null ? "<null>" : this.defaultValue,296 this.getter.Method,297 this.setter.Method);275 Name, 276 Type == null ? "<null>" : Type.FullName, 277 DefaultValue ?? "<null>", 278 getter.Method, 279 setter.Method); 298 280 } 299 281 } -
branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs
r1320 r1323 6 6 delegate string Formatter(ISerializationToken token); 7 7 8 private Dictionary<Type, Formatter> formatters;8 private readonly Dictionary<Type, Formatter> formatters; 9 9 private int depth; 10 10 11 11 public XmlFormatter() { 12 this.formatters = new Dictionary<Type, Formatter>(); 13 this.formatters.Add(typeof(BeginToken), new Formatter(FormatBegin)); 14 this.formatters.Add(typeof(EndToken), new Formatter(FormatEnd)); 15 this.formatters.Add(typeof(PrimitiveToken), new Formatter(FormatData)); 16 this.formatters.Add(typeof(ReferenceToken), new Formatter(FormatReference)); 17 this.formatters.Add(typeof(NullReferenceToken), new Formatter(FormatNullReference)); 18 this.depth = 0; 12 formatters = new Dictionary<Type, Formatter>{ 13 {typeof (BeginToken), FormatBegin}, 14 {typeof (EndToken), FormatEnd}, 15 {typeof (PrimitiveToken), FormatData}, 16 {typeof (ReferenceToken), FormatReference}, 17 {typeof (NullReferenceToken), FormatNullReference} 18 }; 19 depth = 0; 19 20 } 20 21 … … 24 25 25 26 private string Prefix { 26 get { return new string(' ', this.depth * 2); }27 get { return new string(' ', depth * 2); } 27 28 } 28 29 … … 31 32 string result = 32 33 String.Format("{0}<COMPOSITE name=\"{1}\" type=\"{2}\" id=\"{3}\">\n", 33 this.Prefix, beginToken.Accessor.Name, beginToken.Accessor.Get().GetType(), beginToken.Id);34 this.depth += 1;34 Prefix, beginToken.Accessor.Name, beginToken.Accessor.Get().GetType(), beginToken.Id); 35 depth += 1; 35 36 return result; 36 37 } 37 38 38 39 private string FormatEnd(ISerializationToken token) { 39 EndToken endToken = (EndToken)token; 40 this.depth -= 1; 40 depth -= 1; 41 41 return Prefix + "</COMPOSITE>\n"; 42 42 } … … 45 45 PrimitiveToken dataToken = (PrimitiveToken)token; 46 46 return String.Format("{0}<PRIMITIVE name=\"{1}\" type=\"{2}\">{3}</PRIMITIVE>\n", 47 this.Prefix, dataToken.accessor.Name, dataToken.accessor.Get().GetType(), dataToken.Data);47 Prefix, dataToken.accessor.Name, dataToken.accessor.Get().GetType(), dataToken.Data); 48 48 } 49 49 … … 51 51 ReferenceToken refToken = (ReferenceToken)token; 52 52 return String.Format("{0}<REFERENCE name=\"{1}\" ref=\"{2}\"/>\n", 53 this.Prefix, refToken.Name, refToken.Id);53 Prefix, refToken.Name, refToken.Id); 54 54 } 55 55 … … 57 57 NullReferenceToken nullRefToken = (NullReferenceToken)token; 58 58 return String.Format("{0}<NULL name=\"{1}\"/>\n", 59 this.Prefix, nullRefToken.Name);59 Prefix, nullRefToken.Name); 60 60 } 61 61 }
Note: See TracChangeset
for help on using the changeset viewer.