public override List<MapRelationBase> GetRelations(MappingSchema schema, ExtensionList typeExt, Type master, Type slave, out bool isSet) { var masterAccessor = TypeAccessor.GetAccessor(master); var slaveAccessor = slave != null ? TypeAccessor.GetAccessor(slave) : null; var relations = new List<MapRelationBase>(); foreach (MemberAccessor ma in masterAccessor) { var attr = ma.GetAttribute<RelationAttribute>(); if (attr == null || (slave != null && attr.Destination != slave && ma.Type != slave)) continue; if (slave == null) slaveAccessor = TypeAccessor.GetAccessor(attr.Destination ?? ma.Type); var toMany = TypeHelper.IsSameOrParent(typeof(IEnumerable), ma.Type); if (toMany && attr.Destination == null) throw new InvalidOperationException("Destination type should be set for enumerable relations: " + ma.Type.FullName + "." + ma.Name); var masterIndex = attr.MasterIndex; var slaveIndex = attr.SlaveIndex; if (slaveIndex == null) { var accessor = toMany ? masterAccessor : slaveAccessor; var tex = TypeExtension.GetTypeExtension(accessor.Type, typeExt); var keys = GetPrimaryKeyFields(schema, accessor, tex); if (keys.Count > 0) slaveIndex = new MapIndex(keys.ToArray()); } if (slaveIndex == null) throw new InvalidOperationException("Slave index is not set for relation: " + ma.Type.FullName + "." + ma.Name); if (masterIndex == null) masterIndex = slaveIndex; var relation = new MapRelationBase(attr.Destination ?? ma.Type, slaveIndex, masterIndex, ma.Name); relations.Add(relation); } isSet = true; return relations; }
public override List <MapRelationBase> GetRelations(MappingSchema schema, ExtensionList typeExt, Type master, Type slave, out bool isSet) { var masterAccessor = TypeAccessor.GetAccessor(master); var slaveAccessor = slave != null?TypeAccessor.GetAccessor(slave) : null; var relations = new List <MapRelationBase>(); foreach (MemberAccessor ma in masterAccessor) { var attr = ma.GetAttribute <RelationAttribute>(); if (attr == null || (slave != null && attr.Destination != slave && ma.Type != slave)) { continue; } if (slave == null) { slaveAccessor = TypeAccessor.GetAccessor(attr.Destination ?? ma.Type); } var toMany = TypeHelper.IsSameOrParent(typeof(IEnumerable), ma.Type); if (toMany && attr.Destination == null) { throw new InvalidOperationException("Destination type should be set for enumerable relations: " + ma.Type.FullName + "." + ma.Name); } var masterIndex = attr.MasterIndex; var slaveIndex = attr.SlaveIndex; if (slaveIndex == null) { var accessor = toMany ? masterAccessor : slaveAccessor; var tex = TypeExtension.GetTypeExtension(accessor.Type, typeExt); var keys = GetPrimaryKeyFields(schema, accessor, tex); if (keys.Count > 0) { slaveIndex = new MapIndex(keys.ToArray()); } } if (slaveIndex == null) { throw new InvalidOperationException("Slave index is not set for relation: " + ma.Type.FullName + "." + ma.Name); } if (masterIndex == null) { masterIndex = slaveIndex; } var relation = new MapRelationBase(attr.Destination ?? ma.Type, slaveIndex, masterIndex, ma.Name); relations.Add(relation); } isSet = true; return(relations); }
public override List<MapRelationBase> GetRelations(MappingSchema schema, ExtensionList typeExt, Type master, Type slave, out bool isSet) { var relations = new List<MapRelationBase>(); var ext = typeExt != null ? typeExt[master] : TypeExtension.Null; isSet = ext != TypeExtension.Null; if (!isSet) return relations; var ta = TypeAccessor.GetAccessor(master); foreach (var mex in ext.Members.Values) { var relationInfos = mex.Attributes[TypeExtension.NodeName.Relation]; if (relationInfos == AttributeExtensionCollection.Null) continue; var destinationTypeName = relationInfos[0][TypeExtension.AttrName.DestinationType, string.Empty].ToString(); var destinationType = slave; var ma = ta[mex.Name]; var toMany = TypeHelper.IsSameOrParent(typeof(IEnumerable), ma.Type); if (destinationTypeName == string.Empty) { if (toMany) throw new InvalidOperationException("Destination type should be set for enumerable relations: " + ma.Type.FullName + "." + ma.Name); destinationType = ma.Type; } else { if (!destinationTypeName.Contains(",")) destinationTypeName += ", " + ta.OriginalType.Assembly.FullName; try { destinationType = Type.GetType(destinationTypeName, true); } catch (TypeLoadException ex) { throw new InvalidOperationException( "Unable to load type by name: " + destinationTypeName + "\n may be assembly is not specefied, please see Type.GetType(string typeName) documentation", ex); } } if (slave != null && !TypeHelper.IsSameOrParent(slave, destinationType)) continue; var masterIndexFields = new List<string>(); var slaveIndexFields = new List<string>(); foreach (var ae in relationInfos[0].Attributes[TypeExtension.NodeName.MasterIndex]) masterIndexFields.Add(ae[TypeExtension.AttrName.Name].ToString()); foreach (var ae in relationInfos[0].Attributes[TypeExtension.NodeName.SlaveIndex]) slaveIndexFields.Add(ae[TypeExtension.AttrName.Name].ToString()); if (slaveIndexFields.Count == 0) { var accessor = toMany ? ta : TypeAccessor.GetAccessor(destinationType); var tex = TypeExtension.GetTypeExtension(accessor.Type, typeExt); slaveIndexFields = GetPrimaryKeyFields(schema, accessor, tex); } if (slaveIndexFields.Count == 0) throw new InvalidOperationException("Slave index is not set for relation: " + ma.Type.FullName + "." + ma.Name); var slaveIndex = new MapIndex(slaveIndexFields.ToArray()); var masterIndex = masterIndexFields.Count > 0 ? new MapIndex(masterIndexFields.ToArray()) : slaveIndex; var mapRelation = new MapRelationBase(destinationType, slaveIndex, masterIndex, mex.Name); relations.Add(mapRelation); } isSet = relations.Count > 0; return relations; }
public override List <MapRelationBase> GetRelations(MappingSchema schema, ExtensionList typeExt, Type master, Type slave, out bool isSet) { List <MapRelationBase> relations = new List <MapRelationBase>(); TypeExtension ext = typeExt != null ? typeExt[master] : TypeExtension.Null; isSet = ext != TypeExtension.Null; if (!isSet) { return(relations); } TypeAccessor ta = TypeAccessor.GetAccessor(master); foreach (MemberExtension mex in ext.Members) { AttributeExtensionCollection relationInfos = mex.Attributes[TypeExtension.NodeName.Relation]; if (relationInfos == AttributeExtensionCollection.Null) { continue; } string destinationTypeName = relationInfos[0][TypeExtension.AttrName.DestinationType, string.Empty].ToString(); Type destinationType = slave; MemberAccessor ma = ta[mex.Name]; bool toMany = TypeHelper.IsSameOrParent(typeof(IEnumerable), ma.Type); if (destinationTypeName == string.Empty) { if (toMany) { throw new InvalidOperationException("Destination type should be set for enumerable relations: " + ma.Type.FullName + "." + ma.Name); } destinationType = ma.Type; } else { if (!destinationTypeName.Contains(",")) { destinationTypeName += ", " + ta.OriginalType.Assembly.FullName; } try { destinationType = Type.GetType(destinationTypeName, true); } catch (TypeLoadException ex) { throw new InvalidOperationException( "Unable to load type by name: " + destinationTypeName + "\n may be assembly is not specefied, please see Type.GetType(string typeName) documentation", ex); } } if (slave != null && !TypeHelper.IsSameOrParent(slave, destinationType)) { continue; } List <string> masterIndexFields = new List <string>(); List <string> slaveIndexFields = new List <string>(); foreach (AttributeExtension ae in relationInfos[0].Attributes[TypeExtension.NodeName.MasterIndex]) { masterIndexFields.Add(ae[TypeExtension.AttrName.Name].ToString()); } foreach (AttributeExtension ae in relationInfos[0].Attributes[TypeExtension.NodeName.SlaveIndex]) { slaveIndexFields.Add(ae[TypeExtension.AttrName.Name].ToString()); } if (slaveIndexFields.Count == 0) { TypeAccessor accessor = toMany ? ta : TypeAccessor.GetAccessor(destinationType); TypeExtension tex = TypeExtension.GetTypeExtension(accessor.Type, typeExt); slaveIndexFields = GetPrimaryKeyFields(schema, accessor, tex); } if (slaveIndexFields.Count == 0) { throw new InvalidOperationException("Slave index is not set for relation: " + ma.Type.FullName + "." + ma.Name); } MapIndex slaveIndex = new MapIndex(slaveIndexFields.ToArray()); MapIndex masterIndex = masterIndexFields.Count > 0 ? new MapIndex(masterIndexFields.ToArray()) : slaveIndex; MapRelationBase mapRelation = new MapRelationBase(destinationType, slaveIndex, masterIndex, mex.Name); relations.Add(mapRelation); } isSet = relations.Count > 0; return(relations); }