Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1960 for trunk


Ignore:
Timestamp:
05/29/09 16:44:27 (16 years ago)
Author:
epitzer
Message:

Refactoring of storables. (#603)

Location:
trunk/sources/HeuristicLab.Persistence
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableAttribute.cs

    r1938 r1960  
    11using System;
     2using System.Linq;
    23using System.Collections.Generic;
    34using System.Reflection;
     
    4243      public StorableAttribute Attribute { get; private set; }
    4344      public MemberInfo MemberInfo { get; private set; }
     45      public string DisentangledName { get; private set; }
     46      public string FullyQualifiedMemberName {
     47        get {
     48          return new StringBuilder()
     49            .Append(MemberInfo.ReflectedType.FullName)
     50            .Append('.')
     51            .Append(MemberInfo.Name)
     52            .ToString();
     53        }
     54      }
    4455      public StorableMemberInfo(StorableAttribute attribute, MemberInfo memberInfo) {
    4556        this.Attribute = attribute;
     
    5061          .Append('[').Append(Attribute).Append(", ")
    5162          .Append(MemberInfo).Append('}').ToString();
     63      }
     64      public void SetDisentangledName(string name) {
     65        DisentangledName = Attribute.Name ?? name;
     66      }
     67      public Type GetPropertyDeclaringBaseType() {
     68        return ((PropertyInfo)MemberInfo).GetGetMethod(true).GetBaseDefinition().DeclaringType;
    5269      }
    5370    }
     
    91108        }
    92109      }
    93       return storableMembers;
     110      return DisentangleNameMapping(storableMembers);
    94111    }
    95112
    96     public static Dictionary<string, DataMemberAccessor> GetStorableAccessors(object obj) {
    97       var storableAccessors = new Dictionary<string, DataMemberAccessor>();
    98       var nameMapping = createNameMapping(obj.GetType());
    99       var finalNameMapping = analyzeNameMapping(nameMapping);
    100       foreach (var mapping in finalNameMapping) {
    101         storableAccessors.Add(mapping.Value.Attribute.Name ?? mapping.Key,
    102           new DataMemberAccessor(
    103             mapping.Value.MemberInfo,
    104             mapping.Value.Attribute.Name ?? mapping.Key,
    105             mapping.Value.Attribute.DefaultValue,
    106             obj));
    107       }
    108       return storableAccessors;
     113    public static IEnumerable<DataMemberAccessor> GetStorableAccessors(object obj) {     
     114      foreach (var memberInfo in GetStorableMembers(obj.GetType()))
     115        yield return new DataMemberAccessor(
     116          memberInfo.MemberInfo,
     117          memberInfo.DisentangledName,
     118          memberInfo.Attribute.DefaultValue,
     119          obj);     
    109120    }
    110121
    111     private static Dictionary<string, StorableMemberInfo> analyzeNameMapping(
    112         Dictionary<string, List<StorableMemberInfo>> nameMapping) {
    113       var finalNameMapping = new Dictionary<string, StorableMemberInfo>();
    114       foreach (var attributes in nameMapping) {
    115         if (attributes.Value.Count == 1) {
    116           finalNameMapping[attributes.Key] = attributes.Value[0];
    117         } else if (attributes.Value[0].MemberInfo.MemberType == MemberTypes.Field) {
    118           foreach (var attribute in attributes.Value) {
    119             StringBuilder sb = new StringBuilder();
    120             sb.Append(attribute.MemberInfo.ReflectedType.FullName).Append('.')
    121               .Append(attribute.MemberInfo.Name);
    122             finalNameMapping[sb.ToString()] = attribute;
     122    private static IEnumerable<StorableMemberInfo> DisentangleNameMapping(
     123        IEnumerable<StorableMemberInfo> storableMemberInfos) {
     124      var nameGrouping = new Dictionary<string, List<StorableMemberInfo>>();
     125      foreach (StorableMemberInfo storable in storableMemberInfos) {
     126        if (!nameGrouping.ContainsKey(storable.MemberInfo.Name))
     127          nameGrouping[storable.MemberInfo.Name] = new List<StorableMemberInfo>();
     128        nameGrouping[storable.MemberInfo.Name].Add(storable);
     129      }
     130      var memberInfos = new List<StorableMemberInfo>();
     131      foreach (var storableMemberInfoGroup in nameGrouping.Values) {       
     132        if (storableMemberInfoGroup.Count == 1) {
     133          storableMemberInfoGroup[0].SetDisentangledName(storableMemberInfoGroup[0].MemberInfo.Name);
     134          memberInfos.Add(storableMemberInfoGroup[0]);
     135        } else if (storableMemberInfoGroup[0].MemberInfo.MemberType == MemberTypes.Field) {
     136          foreach (var storableMemberInfo in storableMemberInfoGroup) {           
     137            storableMemberInfo.SetDisentangledName(storableMemberInfo.FullyQualifiedMemberName);
     138            memberInfos.Add(storableMemberInfo);
    123139          }
    124         } else {
    125           var uniqueAccessors = new Dictionary<Type, StorableMemberInfo>();
    126           foreach (var attribute in attributes.Value) {
    127             uniqueAccessors[((PropertyInfo)attribute.MemberInfo).GetGetMethod(true).GetBaseDefinition().DeclaringType] =
    128               attribute;
    129           }
    130           if (uniqueAccessors.Count == 1) {
    131             var it = uniqueAccessors.Values.GetEnumerator();
    132             it.MoveNext();
    133             finalNameMapping[attributes.Key] = it.Current;
    134           } else {
    135             foreach (var attribute in uniqueAccessors.Values) {
    136               StringBuilder sb = new StringBuilder();
    137               sb.Append(attribute.MemberInfo.DeclaringType.FullName).Append('.')
    138                 .Append(attribute.MemberInfo.Name);
    139               finalNameMapping[sb.ToString()] = attribute;
    140             }
    141           }
     140        } else {         
     141          memberInfos.AddRange(MergePropertyAccessors(storableMemberInfoGroup));
    142142        }
    143143      }
    144       return finalNameMapping;
     144      return memberInfos;
    145145    }
    146 
    147     private static Dictionary<string, List<StorableMemberInfo>> createNameMapping(Type type) {
    148       var nameMapping = new Dictionary<string, List<StorableMemberInfo>>();
    149       foreach (StorableMemberInfo storable in GetStorableMembers(type)) {
    150         if (!nameMapping.ContainsKey(storable.MemberInfo.Name))
    151           nameMapping[storable.MemberInfo.Name] = new List<StorableMemberInfo>();
    152         nameMapping[storable.MemberInfo.Name].Add(storable);
     146   
     147    private static IEnumerable<StorableMemberInfo> MergePropertyAccessors(List<StorableMemberInfo> members) {
     148      var uniqueAccessors = new Dictionary<Type, StorableMemberInfo>();
     149      foreach (var member in members)
     150        uniqueAccessors[member.GetPropertyDeclaringBaseType()] = member;                 
     151      if (uniqueAccessors.Count == 1) {
     152        var storableMemberInfo = uniqueAccessors.Values.First();
     153        storableMemberInfo.SetDisentangledName(storableMemberInfo.MemberInfo.Name);
     154        yield return storableMemberInfo;
     155      } else {
     156        foreach (var attribute in uniqueAccessors.Values) {
     157          attribute.SetDisentangledName(attribute.FullyQualifiedMemberName);
     158          yield return attribute;
     159        }
    153160      }
    154       return nameMapping;
    155     }
     161    }   
    156162  }
    157163}
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r1938 r1960  
    3333
    3434    public IEnumerable<Tag> Decompose(object obj) {
    35       foreach (var mapping in StorableAttribute.GetStorableAccessors(obj)) {
    36         yield return new Tag(mapping.Key, mapping.Value.Get());
     35      foreach (var accessor in StorableAttribute.GetStorableAccessors(obj)) {
     36        yield return new Tag(accessor.Name, accessor.Get());
    3737      }
    3838    }
     
    4848        memberDict.Add(iter.Current.Name, iter.Current);
    4949      }
    50       foreach (var mapping in StorableAttribute.GetStorableAccessors(instance)) {
    51         string name = mapping.Key;
    52         if (memberDict.ContainsKey(name)) {
    53           mapping.Value.Set(memberDict[name].Value);
    54         } else if (mapping.Value.DefaultValue != null) {
    55           mapping.Value.Set(mapping.Value.DefaultValue);
     50      foreach (var accessor in StorableAttribute.GetStorableAccessors(instance)) {       
     51        if (memberDict.ContainsKey(accessor.Name)) {
     52          accessor.Set(memberDict[accessor.Name].Value);         
     53        } else if (accessor.DefaultValue != null) {
     54          accessor.Set(accessor.DefaultValue);         
    5655        }
    5756      }
  • trunk/sources/HeuristicLab.Persistence/UnitTests/StorableAttributeTests.cs

    r1938 r1960  
    5656    public void SimpleStorableAttributeTest() {
    5757      DemoClass t = new DemoClass();
    58       Dictionary<string, DataMemberAccessor> accessors = StorableAttribute.GetStorableAccessors(t);
     58      IEnumerable<DataMemberAccessor> accessorList = StorableAttribute.GetStorableAccessors(t);
     59      Dictionary<string, DataMemberAccessor> accessors = new Dictionary<string, DataMemberAccessor>();
     60      foreach (var a in accessorList)
     61        accessors.Add(a.Name, a);
    5962      Assert.IsTrue(accessors.ContainsKey("TestProperty"));
    6063      Assert.IsTrue(accessors.ContainsKey("x"));
     
    7881      var accessDict = new Dictionary<string, DataMemberAccessor>();
    7982      foreach (var accessor in accessors) // assert uniqueness
    80         accessDict.Add(accessor.Key, accessor.Value);
     83        accessDict.Add(accessor.Name, accessor);
    8184      Assert.IsTrue(accessDict.ContainsKey(typeof(New).FullName + ".Name"));
    8285      Assert.IsTrue(accessDict.ContainsKey(typeof(Override).FullName + ".Name"));
Note: See TracChangeset for help on using the changeset viewer.