public void Fill(ReferenceImpl reference, bool isEnd1) { IsEnd1 = isEnd1; Reference = reference; PropertiesAffected = false; Populate(); }
public void It_Returns_The_Reference_Objects() { ProviderInfo info = new ProviderInfo(); var entity1 = new EntityImpl("T1"); var entity2 = new EntityImpl("T2"); var entity3 = new EntityImpl("T3"); var rel1 = new ReferenceImpl { EntitySet = info.MappingSet.EntitySet }; var rel2 = new ReferenceImpl { EntitySet = info.MappingSet.EntitySet }; info.MappingSet.EntitySet.AddEntity(entity1); info.MappingSet.EntitySet.AddEntity(entity2); info.MappingSet.EntitySet.AddEntity(entity3); rel1.AddThisTo(entity1, entity2); rel2.AddThisTo(entity3, entity2); IEnumerable <IScriptBaseObject> objects = info.GetAllObjectsOfType(typeof(Reference)); Assert.That(objects, Has.Length(2)); Assert.That(objects.Contains(rel1)); Assert.That(objects.Contains(rel2)); }
public void A_ToEntity__It_Should_Throw_An_Exception() { Reference reference = new ReferenceImpl(); reference.Entity1 = new EntityImpl("Entity1"); new EntitySetSerialisationScheme().SerialiseReference(reference); }
public static Reference GetReference() { Reference reference = new ReferenceImpl(); var mappingSet = new MappingSetImpl(); var entitySet = new EntitySetImpl(); entitySet.AddReference(reference); mappingSet.EntitySet = entitySet; return(reference); }
public void A_ToCardinality__It_Should_Default_To_One() { string expectedXML = string.Format(CardinalityFormatXml, 1, 1, 2, 2); Reference reference = new ReferenceImpl(new Guid("11111111-1111-1111-1111-111111111111")) { Entity2 = new EntityImpl("Entity1"), Entity1 = new EntityImpl("Entity2"), Cardinality2 = new Cardinality(2), End1Enabled = true, End2Enabled = true, End1Name = "ParentEntity1", End2Name = "Entity2s" }; string outputXML = new EntitySetSerialisationScheme().SerialiseReference(reference); outputXML = XmlSqueezer.RemoveWhitespaceBetweenElements(outputXML); Assert.That(outputXML, Is.EqualTo(expectedXML)); }
public void Correct_References_Are_Created() { EntityProcessor proc = MockRepository.GenerateMock <EntityProcessor>(); Entity entity1 = new EntityImpl(); Entity entity2 = new EntityImpl(); Reference reference = new ReferenceImpl(entity1, entity2); proc.Stub(p => p.CreateEntity(Table1)).Return(entity1); proc.Stub(p => p.CreateEntity(Table2)).Return(entity2); proc.Stub(p => p.CreateReference(Relationship12, Set.EntitySet)).Return(reference); MappingProcessor mappingProc = new MappingProcessor(proc); mappingProc.CreateOneToOneMappingsFor(new[] { Table1, Table2 }, Set); proc.AssertWasCalled(p => p.CreateEntity(Table1)); proc.AssertWasCalled(p => p.CreateEntity(Table2)); proc.AssertWasCalled(p => p.CreateReference(Relationship12, Set.EntitySet)); proc.AssertWasNotCalled(p => p.CreateReference(Relationship13, Set.EntitySet)); }
private void ProcessMouseOverEndOfLine(object sender, MouseEventArgs e, Rectangle endRectangle, bool isEnd1) { int offset = 0; Point pt = new Point(endRectangle.X, endRectangle.Y + endRectangle.Height);// PointToClient(Cursor.Position);// e.Location; pt.Offset(offset, offset); if (endRectangle != CurrentEndRectangle || !RefEditorForm.Visible) { CurrentEndRectangle = endRectangle; LinkLine line = (LinkLine)sender; ReferenceImpl reference = (ReferenceImpl)line.DataObject; RefEditorForm.Fill(reference, isEnd1); //RefEditorForm.Fill((ReferenceImpl)lineEndWithFocus.Line.DataObject, lineEndWithFocus.EndType == ShapeCanvas.LineEndWithFocus.EndTypes.Start); if (pt.X + RefEditorForm.Width < this.Width) { RefEditorForm.Location = pt; } else { pt.Offset(-1 * RefEditorForm.Width + endRectangle.Width + offset * 2, 0); RefEditorForm.Location = pt; } if (pt.Y + RefEditorForm.Height < this.Height) { RefEditorForm.Location = pt; } else { pt.Offset(0, -1 * RefEditorForm.Height - endRectangle.Height + offset * 2); RefEditorForm.Location = pt; } RefEditorForm.Visible = true; RefEditorForm.Refresh(); } }
public void It_Should_Throw_An_Exception() { Reference reference = new ReferenceImpl(); new EntitySetSerialisationScheme().SerialiseReference(reference); }
private void UseDiscoveredInformationToCreateReferences(MappingSet mappingSet) { while (associationInformation.Count > 0) { var info = associationInformation[0]; associationInformation.RemoveAt(0); // Get existing objects var fromEntity = mappingSet.EntitySet.GetEntity(info.ThisEntityName); var fromTable = fromEntity.MappedTables().First(); var toEntity = mappingSet.EntitySet.GetEntity(info.OtherEntityName); var toTable = toEntity.MappedTables().First(); // Sort out this side of the reference. Reference reference = new ReferenceImpl(); reference.Entity1 = fromEntity; reference.End1Name = info.PropertyName; reference.End1Enabled = true; reference.Cardinality1 = info.Cardinality; reference.SetEnd1AssociationType(info.AssociationType); reference.SetEnd1IndexColumnName(info.IndexColumn); reference.SetEnd1SqlWhereClause(info.WhereClause); reference.SetReferenceEnd1FetchMode(info.FetchMode); reference.SetReferenceEnd1CollectionFetchMode(info.CollectionFetchMode); reference.SetReferenceEnd1Insert(info.Insert); reference.SetReferenceEnd1Update(info.Update); reference.SetReferenceEnd1Inverse(info.Inverse); reference.SetReferenceEnd1FetchMode(info.FetchMode); reference.SetReferenceEnd1Lazy(info.CollectionLazy); reference.SetReferenceEnd1Cascade(info.Cascade); reference.SetReferenceEnd1CollectionCascade(info.CollectionCascade); if (!string.IsNullOrWhiteSpace(info.OrderByColumnName)) { var orderByProp = fromEntity.Properties.SingleOrDefault(p => p.MappedColumn() != null && p.MappedColumn().Name.Equals(info.OrderByColumnName, StringComparison.InvariantCultureIgnoreCase)); if (orderByProp != null) { reference.SetReferenceEnd1OrderByProperty(orderByProp.Name); } } reference.SetReferenceEnd1OrderByIsAsc(info.OrderByIsAsc); // Find the other side of the reference. ProcessOtherEndOfReference(info, toEntity, reference); //associationInformation.RemoveAt(0); fromEntity.AddReference(reference); if (fromEntity.InternalIdentifier != toEntity.InternalIdentifier) { toEntity.AddReference(reference); } if (info.AssociationTableName != null && !string.IsNullOrEmpty(info.AssociationTableName.TableName)) { // Map Reference to Table string schema = /*string.IsNullOrEmpty(info.AssociationTableName.SchemaName) ? "" :*/ info.AssociationTableName.SchemaName.UnBackTick(); var mappedTable = mappingSet.Database.GetTable(info.AssociationTableName.TableName.UnBackTick(), schema); if (mappedTable == null) { throw new NHibernateMappingException(string.Format("Could not find association table {0} to map to reference {1}.", info.AssociationTableName, reference.Name)); } mappingSet.ChangeMappingFor(reference).To(mappedTable); } else { IEnumerable <IColumn> foreignKeyColumns; Func <DirectedRelationship, bool> predicate; if (info.ForeignKeyColumnNames.Count == 0 && info.Cardinality.Start == 1 && info.Cardinality.End == 1 && fromTable != toTable) { predicate = r => r.ToKey.Columns.OrderBy(c => c.Name).SequenceEqual(r.ToTable.ColumnsInPrimaryKey.OrderBy(c => c.Name)) && r.FromKey.Columns.OrderBy(c => c.Name).SequenceEqual(r.FromTable.ColumnsInPrimaryKey.OrderBy(c => c.Name)); } else { if (info.ForeignKeyBelongsToThisTable) { foreignKeyColumns = info.ForeignKeyColumnNames.Select(f => fromTable.GetColumn(f.UnBackTick())); if (fromTable == toTable) { // Self referencing keys might have the primary key at either end. predicate = r => (r.FromKey.Columns.SequenceEqual(foreignKeyColumns) || r.ToKey.Columns.SequenceEqual(foreignKeyColumns)); } else { predicate = r => r.FromKey.Columns.SequenceEqual(foreignKeyColumns); } } else { foreignKeyColumns = info.ForeignKeyColumnNames.Select(f => toTable.GetColumn(f.UnBackTick())); predicate = r => r.ToKey.Columns.SequenceEqual(foreignKeyColumns); } } var possibleRelationships = fromTable.DirectedRelationships.Where(r => r.ToTable == toTable); var relationshipToMap = possibleRelationships.FirstOrDefault(predicate); if (relationshipToMap != null) { mappingSet.ChangeMappingFor(reference).To(relationshipToMap.Relationship); } else { throw new NHibernateMappingException(string.Format("Could not find relationship to map to for reference between Entities \"{0}\" and \"{1}\"", info.ThisEntityName, info.OtherEntityName)); } } } }
private static Reference CreateReferenceFromRelationship(Relationship relationship, EntitySet entitySet) { Reference reference = new ReferenceImpl(); reference.Name = relationship.Name; reference.Entity1 = relationship.PrimaryTable.MappedEntities().FirstOrDefault(); reference.Entity2 = relationship.ForeignTable.MappedEntities().FirstOrDefault(); if (reference.Entity1 == null || reference.Entity2 == null) { return(null); } // Don't add a reference if an inheritance structure exists between the two entities if ((reference.Entity1.Parent != null && reference.Entity1.Parent == reference.Entity2) || (reference.Entity2.Parent != null && reference.Entity2.Parent == reference.Entity1)) { return(null); } reference.Cardinality1 = relationship.PrimaryCardinality; reference.Cardinality2 = relationship.ForeignCardinality; IList <string> existingNames1 = reference.Entity1.GetReferenceNames(reference.Entity2); IList <string> existingNames2 = reference.Entity2.GetReferenceNames(reference.Entity1); if (reference.Entity1 == reference.Entity2) { existingNames1.Add(reference.Entity1.Name); reference.End1Name = reference.Cardinality1 == Cardinality.Many ? reference.Entity2.Name.Pluralize().GetNextName(existingNames2) : reference.Entity2.Name.GetNextName(existingNames2); if (relationship.PrimaryKey.Columns.Count == 1) { if (relationship.ForeignKey == null) { throw new Exception(string.Format("Relationship [{0}] on table [{1}] has no foreign key.", relationship.Name, relationship.PrimaryTable.Name)); } else if (relationship.ForeignKey.Columns.Count != 1) { throw new Exception(string.Format("The foreign key [{0}] of relationship [{1}] on table [{2}] should have 1 column (to match the number of columns in the primary key). It has {3} columns.", relationship.ForeignKey.Name, relationship.Name, relationship.PrimaryTable.Name, relationship.ForeignKey.Columns.Count)); } reference.End2Name = relationship.ForeignKey.Columns[0].Name.GetCSharpFriendlyIdentifier().GetNextName(existingNames1); } else { reference.End2Name = reference.Cardinality2 == Cardinality.Many ? reference.Entity1.Name.Pluralize().GetNextName(existingNames1) : reference.Entity1.Name.GetNextName(existingNames1); } } else { reference.End1Name = reference.Cardinality1 == Cardinality.Many ? reference.Entity2.Name.Pluralize().GetNextName(existingNames2) : reference.Entity2.Name.GetNextName(existingNames2); reference.End2Name = reference.Cardinality2 == Cardinality.Many ? reference.Entity1.Name.Pluralize().GetNextName(existingNames1) : reference.Entity1.Name.GetNextName(existingNames1); } reference.End1Enabled = true; reference.End2Enabled = true; reference.EntitySet = entitySet; entitySet.AddReference(reference); reference.Entity1.AddReference(reference); if (reference.Entity1 != reference.Entity2) { reference.Entity2.AddReference(reference); } reference.SetMappedRelationship(relationship); return(reference); }