private OneToOneRelation <MetadataToken, uint> InitializeFieldLayouts() { var tablesStream = DotNetDirectory.Metadata.GetStream <TablesStream>(); var layoutTable = tablesStream.GetTable <FieldLayoutRow>(TableIndex.FieldLayout); var fieldLayouts = new OneToOneRelation <MetadataToken, uint>(); for (int i = 0; i < layoutTable.Count; i++) { var ownerToken = new MetadataToken(TableIndex.Field, layoutTable[i].Field); uint layoutRid = (uint)(i + 1); fieldLayouts.Add(ownerToken, layoutRid); } return(fieldLayouts); }
private OneToOneRelation <MetadataToken, uint> InitializeFieldRvas() { var tablesStream = DotNetDirectory.Metadata.GetStream <TablesStream>(); var rvaTable = tablesStream.GetTable <FieldRvaRow>(TableIndex.FieldRva); var rvas = new OneToOneRelation <MetadataToken, uint>(); for (int i = 0; i < rvaTable.Count; i++) { var ownerToken = new MetadataToken(TableIndex.Field, rvaTable[i].Field); uint rvaRid = (uint)(i + 1); rvas.Add(ownerToken, rvaRid); } return(rvas); }
private OneToOneRelation <MetadataToken, uint> InitializeFieldMarshals() { var tablesStream = DotNetDirectory.Metadata.GetStream <TablesStream>(); var marshalTable = tablesStream.GetTable <FieldMarshalRow>(TableIndex.FieldMarshal); var encoder = tablesStream.GetIndexEncoder(CodedIndex.HasFieldMarshal); var marshals = new OneToOneRelation <MetadataToken, uint>(); for (int i = 0; i < marshalTable.Count; i++) { var ownerToken = encoder.DecodeIndex(marshalTable[i].Parent); uint rvaRid = (uint)(i + 1); marshals.Add(ownerToken, rvaRid); } return(marshals); }
private OneToOneRelation <MetadataToken, uint> InitializeImplementationMaps() { var tablesStream = DotNetDirectory.Metadata.GetStream <TablesStream>(); var mapTable = tablesStream.GetTable <ImplementationMapRow>(TableIndex.ImplMap); var encoder = tablesStream.GetIndexEncoder(CodedIndex.TypeOrMethodDef); var maps = new OneToOneRelation <MetadataToken, uint>(); for (int i = 0; i < mapTable.Count; i++) { var ownerToken = encoder.DecodeIndex(mapTable[i].MemberForwarded); uint mapRid = (uint)(i + 1); maps.Add(ownerToken, mapRid); } return(maps); }
private OneToOneRelation <MetadataToken, uint> GetConstants() { var tablesStream = DotNetDirectory.Metadata.GetStream <TablesStream>(); var constantTable = tablesStream.GetTable <ConstantRow>(TableIndex.Constant); var encoder = tablesStream.GetIndexEncoder(CodedIndex.HasConstant); var constants = new OneToOneRelation <MetadataToken, uint>(); for (int i = 0; i < constantTable.Count; i++) { var ownerToken = encoder.DecodeIndex(constantTable[i].Parent); uint constantRid = (uint)(i + 1); constants.Add(ownerToken, constantRid); } return(constants); }
private void GenerateOneToOneRelationFromSources(CodeTypeDeclaration classDeclaration, CodeNamespace nameSpace, OneToOneRelation relationship) { CodeTypeDeclaration sourceClass = GenerateClass(relationship.Source, nameSpace); CodeMemberField memberField = GetMemberField(sourceClass.Name, sourceClass.Name, Accessor.Private, relationship.SourceAccess); classDeclaration.Members.Add(memberField); CodeMemberProperty memberProperty; if (String.IsNullOrEmpty(relationship.TargetDescription)) memberProperty = GetMemberProperty(memberField, sourceClass.Name, true, true, relationship.Source.DoesImplementINotifyPropertyChanged(), null); else memberProperty = GetMemberProperty(memberField, sourceClass.Name, true, true, relationship.Source.DoesImplementINotifyPropertyChanged(), relationship.TargetDescription); classDeclaration.Members.Add(memberProperty); memberProperty.CustomAttributes.Add(relationship.GetOneToOneAttributeForTarget()); }
private bool ApplyOneToOneRelation <TLeft, TRight>(TLeft leftHand, TRight rightHand, OneToOneRelation relation) { var leftValue = relation.GetRight(leftHand); var rightValue = relation.GetRight(rightHand); return(AreMemberWiseEqual(leftValue, rightValue)); }
private static void GetOneToOneEntity <TEntity>(DataTable[] dataTables, ConvertToPrimitive convertToPrimitive, TEntity parentObj, bool oneToOneRelationRequired) { var type = typeof(TEntity); var propertiesOneToOne = type.GetProperties().Where(p => !p.GetCustomAttributes(typeof(NotMapped), true).Any() && p.GetCustomAttributes(typeof(OneToOneRelation), true).Any()); var propertiesNames = type.GetProperties().Where(p => !p.GetCustomAttributes(typeof(NotMapped), true).Any() && p.GetCustomAttributes(typeof(ColumnName), true).Any()); var propertiesNamesObj = type.GetProperties().Where(p => !p.GetCustomAttributes(typeof(NotMapped), true).Any() && !p.GetCustomAttributes(typeof(OneToOneRelation), true).Any() && !p.GetCustomAttributes(typeof(OneToManyRelation), true).Any() && !p.GetCustomAttributes(typeof(ColumnName), true).Any()); if (propertiesOneToOne != null && propertiesOneToOne.Any()) { foreach (var prop in propertiesOneToOne) { var oneToOneAttrs = prop.GetCustomAttributes(typeof(OneToOneRelation), true); if (oneToOneAttrs.Count() > 1) { throw new System.InvalidOperationException(string.Format("Property {0} contains a duplicate attribute", prop.Name)); } OneToOneRelation attr = oneToOneAttrs.Single() as OneToOneRelation; DataTable datatableOneToOne; if (oneToOneRelationRequired || attr.Required) { var datat = dataTables.Where(dt => string.Equals(dt.TableName, attr.TableName, StringComparison.OrdinalIgnoreCase)); if (!datat.Any()) { throw new ArgumentNullException(string.Format("Required Datatable was not found for the property {0} with an OneToOneRelation attribute (DataTable:{1})", prop.Name, attr.TableName)); } datatableOneToOne = datat.First(); } else { datatableOneToOne = dataTables.Where(dt => string.Equals(dt.TableName, attr.TableName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); } if (datatableOneToOne != null && datatableOneToOne.Rows.Count > 0) { PropertyInfo propertyParentKey = null; Dictionary <string, PropertyInfo> propertyParentKeys = new Dictionary <string, PropertyInfo>(); if (!string.IsNullOrWhiteSpace(attr.ParentKey)) { propertyParentKey = SearchPropertyParent(propertiesNames, propertiesNamesObj, attr.ParentKey); } else if (attr.ParentKeys != null && attr.ParentKeys.Any()) { foreach (var key in attr.ParentKeys) { var resul = SearchPropertyParent(propertiesNames, propertiesNamesObj, key); propertyParentKeys.Add(key, resul); } } else { throw new ArgumentException("One to one relational attributes are required"); } Func <DataRow, bool> funcOneToOne = null; if (!string.IsNullOrWhiteSpace(attr.SiblingKey) && !string.IsNullOrWhiteSpace(attr.ParentKey)) { if (propertyParentKey == null) { throw new ArgumentException(string.Format("One to one relational attributes are required, ParentKey ({0}) property was not found", attr.ParentKey)); } funcOneToOne = (r) => { if (r.Table.Columns.Contains(attr.SiblingKey)) { return(string.Equals(r[attr.SiblingKey].ToString(), propertyParentKey.GetValue(parentObj, null).ToString(), StringComparison.OrdinalIgnoreCase)); } else { throw new ArgumentException(string.Format("One to one relational attributes are required, SiblingKey ({0}) column was not found", attr.SiblingKey)); } }; } else if (attr.SiblingKeys != null && attr.SiblingKeys.Any() && propertyParentKeys != null && propertyParentKeys.Any() && !propertyParentKeys.Any(p => p.Value == null) && attr.SiblingKeys.Count() == propertyParentKeys.Count()) { funcOneToOne = (r) => { bool resul = false; for (int i = 0; i < attr.SiblingKeys.Length; i++) { if (propertyParentKeys.TryGetValue(attr.ParentKeys[i], out propertyParentKey)) { if (r.Table.Columns.Contains(attr.SiblingKeys[i])) { resul = string.Equals(r[attr.SiblingKeys[i]].ToString(), propertyParentKey.GetValue(parentObj, null).ToString(), StringComparison.OrdinalIgnoreCase); } else { throw new ArgumentException(string.Format("One to one relational attributes are required, SiblingKey ({0}) column was not found", attr.SiblingKeys[i])); } } else { throw new ArgumentException("One to one relational attributes must match"); } if (!resul) { break; } } return(resul); }; } else { throw new ArgumentException("One to one relational attributes are required"); } var dataRows = datatableOneToOne.Select().Where(funcOneToOne); if (dataRows.Count() > 1) { throw new System.InvalidOperationException("Only one row must match in an One to One Relation"); } var dataRow = dataRows.SingleOrDefault(); if ((oneToOneRelationRequired || attr.Required) && dataRow == null) { throw new NullReferenceException("One to one relational row is required"); } else if (dataRow == null) { continue; } MethodInfo method = typeof(DataTableMap).GetMethod("GetEntity", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo generic = method.MakeGenericMethod(new[] { prop.PropertyType }); var obj = generic.Invoke(null, new object[] { dataRow, dataTables, convertToPrimitive, parentObj, oneToOneRelationRequired }); prop.SetValue(parentObj, obj, null); } else if (oneToOneRelationRequired || attr.Required) { throw new ArgumentException("One to one relational table is required"); } } } }