public IPropertyMapping GetPropertyMapping(string name, Type owningType = null) { if (owningType == null || owningType == Type) { IPropertyMapping mapping; if (_propertyMappings.TryGetValue(name, out mapping)) { return(mapping); } if (HasSubTypeMappings) { return(null); } } else if (HasSubTypeMappings) { IObjectMapping subTypeMapping; if (SubTypeMappingsTypeDictionary.TryGetValue(owningType, out subTypeMapping)) { return(subTypeMapping.GetPropertyMapping(name)); } } throw new MappingException($"Property mapping with name '{name}' was not found for '{Type.FullName}'"); }
public IPropertyMapping GetPropertyMappingByAttribute(string name, Type owningType = null) { if (owningType == null || owningType == Type) { if (_attributePropertyMappings.TryGetValue(name, out IPropertyMapping mapping)) { return(mapping); } } else if (HasSubTypeMappings) { if (SubTypeMappingsTypeDictionary.TryGetValue(owningType, out IObjectMapping subTypeMapping)) { return(subTypeMapping.GetPropertyMappingByAttribute(name)); } } return(null); }
public virtual void AddSubTypeMapping(IObjectMapping mapping) { if (WithoutSubTypeMapping || SubTypeMappingsObjectClassDictionary.Values.Contains(mapping)) { return; } var currentMappings = SortByInheritanceDescending(SubTypeMappingsObjectClassDictionary.Values.Union(new[] { mapping })); SubTypeMappingsObjectClassDictionary.Clear(); SubTypeMappingsTypeDictionary.Clear(); foreach (var currentMapping in currentMappings) { var objectClasses = currentMapping.ObjectClasses.ToList(); //find direct ancestor object classes or default to this class' object classes if a direct ancestor hasn't been mapped yet. var parentObjectClasses = currentMappings .Where(x => currentMapping.Type.IsSubclassOf(x.Type)) .Select(x => x.ObjectClasses) .FirstOrDefault() ?? ObjectClasses; objectClasses = objectClasses.Except(parentObjectClasses, StringComparer.OrdinalIgnoreCase).ToList(); if (objectClasses.Count == 0) { throw new InvalidOperationException("Unable to identify distinct object class based on mapped inheritance"); } SubTypeMappingsObjectClassDictionary.Add(objectClasses[0], currentMapping); SubTypeMappingsTypeDictionary.Add(currentMapping.Type, currentMapping); } _readOnlySubTypeMappings = null; _propertyNames = InitializePropertyNames(); }