Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/11/09 14:20:51 (16 years ago)
Author:
epitzer
Message:

resharping...

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  
    3030      object instance = Activator.CreateInstance(t);
    3131      foreach (object o in objects) {
    32         t.GetMethod("Add").Invoke(instance, new object[] { o });
     32        t.GetMethod("Add").Invoke(instance, new[] { o });
    3333      }
    3434      return instance;
  • branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj

    r1322 r1323  
    5757    <Compile Include="StorableAttributeTests.cs" />
    5858    <Compile Include="StorableAttribute.cs" />
    59     <Compile Include="NewSerializationTest.cs" />
     59    <Compile Include="Test\NewSerializationTest.cs" />
    6060    <Compile Include="SerializationTest.cs" />
    6161    <Compile Include="XmlFormatter.cs" />
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1318 r1323  
    22using System.Collections;
    33using System;
    4 using System.Reflection;
    54
    65namespace Persistence {
     
    87  public class Serializer : IEnumerable<ISerializationToken> {
    98
    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;
    1514
    1615    public Serializer(object obj) :
     
    3433      }
    3534      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        }
    3937
    4038    IEnumerator IEnumerable.GetEnumerator() {
    41       return this.GetEnumerator();
     39      return GetEnumerator();
    4240    }
    4341
    4442    public IEnumerator<ISerializationToken> GetEnumerator() {
    4543      DataMemberAccessor rootAccessor = new DataMemberAccessor(
    46         this.rootName, obj.GetType(), null, () => this.obj, null);
     44        rootName, obj.GetType(), null, () => obj, null);
    4745      IEnumerator<ISerializationToken> iterator = Serialize(rootAccessor);
    4846      while (iterator.MoveNext())
     
    5452      if (value == null) {
    5553        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]);
    6058      } else {
    6159        int id = obj2id.Count;
    62         this.obj2id.Add(value, id);
     60        obj2id.Add(value, id);
    6361        yield return new BeginToken(accessor, id);
    64         ICompoundSerializer customSerializer = this.FindCompoundSerializer(value.GetType());
     62        ICompoundSerializer customSerializer = FindCompoundSerializer(value.GetType());
    6563        if (customSerializer != null) {
    66           foreach (object obj in 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));
    6866            while (iterator.MoveNext())
    6967              yield return iterator.Current;
     
    7472            StorableAttribute.GetAutostorableAccessors(value)) {
    7573            nSubComponents += 1;
    76             IEnumerator<ISerializationToken> iterator = this.Serialize(mapping.Value);                       
     74            IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value);                       
    7775            while (iterator.MoveNext()) {                           
    7876              yield return iterator.Current;
  • branches/New Persistence Exploration/Persistence/Persistence/StorableAttribute.cs

    r1322 r1323  
    11using System;
    22using System.Collections.Generic;
    3 using System.Text;
    43using System.Reflection;
    5 using System.Collections;
    64
    75namespace Persistence {
     
    1311  public class StorableAttribute : Attribute {
    1412
    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; }
    2715
    2816    private const BindingFlags instanceMembers =
     
    3119      BindingFlags.NonPublic;
    3220
    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) {
    3623      foreach (MemberInfo memberInfo in obj.GetType().GetMembers(instanceMembers)) {
    3724        foreach (object attribute in memberInfo.GetCustomAttributes(false)) {         
    38           StorableAttribute autoStorableAttribute =
     25          StorableAttribute storableAttribute =
    3926            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);           
    4329          }
    4430        }
    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;
    4742    }   
    4843  }
     
    5045  class CloningFactory {
    5146
    52     private static CloningFactory instance = new CloningFactory();
     47    private static readonly CloningFactory instance = new CloningFactory();
    5348
    5449    public static T DefaultClone<T>(T obj) {
     
    5651    }
    5752
    58     private List<ICompoundSerializer> compoundSerializers;
     53    private readonly List<ICompoundSerializer> compoundSerializers;
    5954
    6055    public CloningFactory(IEnumerable<ICompoundSerializer> compoundSerializers) {
     
    6762    delegate object CloneMethod(object obj, Dictionary<object, object> twins);
    6863
    69     Dictionary<Type, CloneMethod> cloneMethods = new Dictionary<Type, CloneMethod>();
     64    readonly Dictionary<Type, CloneMethod> cloneMethods = new Dictionary<Type, CloneMethod>();
    7065
    7166    public T Clone<T>(T obj) {
     
    7570
    7671    public T Clone<T>(T obj, Dictionary<object, object> twins) {
    77       if (obj == null)
    78         return default(T);
    79 
    8072      ValueType t = obj as ValueType;
    8173      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);
    8380
    8481      if (twins.ContainsKey(obj))
     
    9996
    10097    private CloneMethod CreateCloneMethod(Type type) {
    101       if (type.GetConstructor(new Type[] { type }) != null)
     98      if (type.GetConstructor(new[] { type }) != null)
    10299        return CopyConstructorCloneMethod.Create(type);
    103100      foreach (ICompoundSerializer serializer in compoundSerializers)
     
    111108        return new CopyConstructorCloneMethod(type).method;
    112109      }
    113       private ConstructorInfo constructorInfo;
     110      private readonly ConstructorInfo constructorInfo;
    114111      public CopyConstructorCloneMethod(Type type) {
    115         this.constructorInfo = type.GetConstructor(new Type[] { type });
    116       }
    117 
     112        constructorInfo = type.GetConstructor(new[] { type });
     113      }
    118114      object method(object obj, Dictionary<object, object> twins) {
    119         object newInstance = constructorInfo.Invoke(new object[] { obj });
     115        object newInstance = constructorInfo.Invoke(new[] { obj });
    120116        twins.Add(obj, newInstance);
    121117        return newInstance;
     
    128124        return new CompoundSerializerCloneMethod(serializer, type, factory).method;
    129125      }
    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;
    133129      public CompoundSerializerCloneMethod(ICompoundSerializer serializer,
    134130          Type type, CloningFactory factory) {
     
    138134      }
    139135      object method(object obj, Dictionary<object, object> twins) {
    140         return serializer.DeSerialize(
     136        return serializer.DeSerialize(         
    141137          Functional.Map(
    142138            serializer.Serialize(obj),
    143             (o) => factory.Clone(o, twins)),
     139            o => factory.Clone(o, twins)),
    144140          type);
    145141      }
     
    148144    class StorableAccessorCloneMethod {
    149145
    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);
    152148
    153149      struct FieldAccessor {     
    154         public Getter Getter;
    155         public Setter Setter;
    156         public FieldAccessor(Getter getter, Setter setter) {
    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;
    159155        }
    160156      }
     
    169165      }
    170166
    171       private List<FieldAccessor> fieldAccessors;
    172       private CloningFactory factory;
     167      private readonly List<FieldAccessor> fieldAccessors;
     168      private readonly CloningFactory factory;
    173169         
    174170      public StorableAccessorCloneMethod(Type type, CloningFactory factory) {
    175171        this.factory = factory;
    176         this.fieldAccessors = new List<FieldAccessor>();
     172        fieldAccessors = new List<FieldAccessor>();
    177173        foreach (MemberInfo memberInfo in type.GetMembers(INSTANCE_MEMBERS)) {
    178174          foreach (object attribute in memberInfo.GetCustomAttributes(false)) {
     
    188184                PropertyInfo pi = (PropertyInfo)memberInfo;
    189185                fieldAccessors.Add(new FieldAccessor(
    190                   (target) => pi.GetValue(target, null),
     186                  target => pi.GetValue(target, null),
    191187                  (target, value) => pi.SetValue(target, value, null)));
    192188              }
     
    198194        object newInstance = Activator.CreateInstance(obj.GetType());
    199195        twins.Add(obj, newInstance);
    200         foreach (FieldAccessor fa in this.fieldAccessors) {
     196        foreach (FieldAccessor fa in fieldAccessors) {
    201197          fa.Setter(newInstance, factory.Clone(fa.Getter(obj), twins));
    202198        }
     
    219215    public delegate void Setter(object value);
    220216
    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;
    226219
    227220    public DataMemberAccessor(
     
    231224      if (memberInfo.MemberType == MemberTypes.Field) {
    232225        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;
    236229      } else if (memberInfo.MemberType == MemberTypes.Property) {
    237230        PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
     
    240233            "Storable properties must implement both a Get and a Set accessor. ");
    241234        }
    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;
    245238      } else {
    246239        throw new NotSupportedException(
    247240                "The Storable attribute can only be applied to fields and properties.");
    248241      }     
    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;                 
    251244    }
    252245
     
    254247        string name, Type type, object defaultValue,
    255248        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;
    259252      this.getter = getter;
    260253      this.setter = setter;
     
    262255
    263256    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; }
    283267    public object Get() {
    284       return this.getter();
    285     }
    286 
     268      return getter();
     269    }
    287270    public void Set(object value) {
    288       this.setter(value);
    289     }
    290 
     271      setter(value);
     272    }
    291273    public override string ToString() {
    292274      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);
    298280    }
    299281  }
  • branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs

    r1320 r1323  
    66    delegate string Formatter(ISerializationToken token);
    77
    8     private Dictionary<Type, Formatter> formatters;
     8    private readonly Dictionary<Type, Formatter> formatters;
    99    private int depth;
    1010
    1111    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;
    1920    }
    2021
     
    2425
    2526    private string Prefix {
    26       get { return new string(' ', this.depth * 2); }
     27      get { return new string(' ', depth * 2); }
    2728    }
    2829
     
    3132      string result =
    3233        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;
    3536      return result;
    3637    }
    3738
    3839    private string FormatEnd(ISerializationToken token) {
    39       EndToken endToken = (EndToken)token;
    40       this.depth -= 1;
     40      depth -= 1;
    4141      return Prefix + "</COMPOSITE>\n";
    4242    }
     
    4545      PrimitiveToken dataToken = (PrimitiveToken)token;
    4646      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);
    4848    }
    4949
     
    5151      ReferenceToken refToken = (ReferenceToken)token;
    5252      return String.Format("{0}<REFERENCE name=\"{1}\" ref=\"{2}\"/>\n",
    53         this.Prefix, refToken.Name, refToken.Id);
     53        Prefix, refToken.Name, refToken.Id);
    5454    }
    5555
     
    5757      NullReferenceToken nullRefToken = (NullReferenceToken)token;
    5858      return String.Format("{0}<NULL name=\"{1}\"/>\n",
    59         this.Prefix, nullRefToken.Name);
     59        Prefix, nullRefToken.Name);
    6060    }
    6161  }
Note: See TracChangeset for help on using the changeset viewer.