private ClassMapping GetMapping(Type type) { var mapping = Configuration.Mapping .GetOrAdd(type, t => new ClassMapping(TypeAccessor.GetAccessor(type))); if (mapping.Mapped) { return(mapping); } bool autoMap = mapping.AutoMap || Configuration.AutoMap; if (!autoMap) { return(mapping); } // thread-safe initialization lock (mapping.SyncRoot) { if (mapping.Mapped) { return(mapping); } var typeAccessor = mapping.TypeAccessor; // scan for data sources var dataSources = Configuration .DataSources() .OrderBy(p => p.Priority) .ToList(); var properties = typeAccessor.GetProperties() .Where(p => p.HasGetter && p.HasSetter); foreach (var property in properties) { // get or create member var memberMapping = mapping.Members.FirstOrDefault(m => m.MemberAccessor.Name == property.Name); if (memberMapping == null) { memberMapping = new MemberMapping { MemberAccessor = property }; mapping.Members.Add(memberMapping); } // skip already mapped fields if (memberMapping.Ignored || memberMapping.DataSource != null) { continue; } // search all for first match var context = new MappingContext(mapping, memberMapping); memberMapping.DataSource = dataSources.FirstOrDefault(d => d.TryMap(context)); } mapping.Mapped = true; return(mapping); } }
/// <summary> /// Creates a new <see langword="object"/> that is a copy of the current instance. /// </summary> /// <returns> /// A new object that is a copy of this instance. /// </returns> public ClassMapping Clone() { var classMapping = new ClassMapping { AutoMap = AutoMap, Ignored = Ignored, Mapped = Mapped, TypeAccessor = TypeAccessor, }; foreach (var m in Members) { var memberMapping = new MemberMapping { Ignored = m.Ignored, MemberAccessor = m.MemberAccessor, DataSource = m.DataSource }; classMapping.Members.Add(memberMapping); } return classMapping; }
/// <summary> /// Initializes a new instance of the <see cref="MappingContext"/> class. /// </summary> /// <param name="classMapping">The class mapping.</param> /// <param name="memberMapping">The member mapping.</param> public MappingContext(ClassMapping classMapping, MemberMapping memberMapping) { ClassMapping = classMapping; MemberMapping = memberMapping; }