Skip to content

Commit

Permalink
- added handling of multiple associations between two tables and tool…
Browse files Browse the repository at this point in the history
…tips for associations
  • Loading branch information
fknx committed Oct 31, 2017
1 parent 46f0243 commit ee22527
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions DynamicLinqPadPostgreSqlDriver/Extensions/TableDataExtensions.cs
Expand Up @@ -12,16 +12,21 @@ public static string CreateOneToManyAssociation(this TableData table, TableData
// create an IEnumerable<> with the type of the (main) table's type builder
var typedEnumerableType = typeof(IEnumerable<>).MakeGenericType(table.TypeBuilder);

// use the table's name as property name (plus constraint name)
var propertyName = $"{table.Name} ({constraintName})";
foreignTable.PropertyAndFieldNames.Add(propertyName);
// use the table's name as property name
var propertyName = $"{table.ExplorerItem.Text}";

var i = 1;
while (!foreignTable.PropertyAndFieldNames.Add(propertyName))
{
propertyName = $"{table.ExplorerItem.Text}{i++}";
}

// create a property in the foreign key's target table
var property = foreignTable.TypeBuilder.DefineProperty(propertyName, typedEnumerableType);

// create a getter for the property
var propertyGetter = foreignTable.TypeBuilder.DefineGetter(property);

// obtain ResolveOneToMany method
var resolveOneToManyMethod = typeof(Entity).GetMethod("ResolveOneToMany", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(table.TypeBuilder);

Expand All @@ -38,9 +43,14 @@ public static string CreateOneToManyAssociation(this TableData table, TableData

// add the 'AssociationAttribute' to the property
property.AddAssociationAttribute(foreignColumnNames, columnNames, table.Name);

// create the explorer item
var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.CollectionLink, ExplorerIcon.OneToMany) { HyperlinkTarget = table.ExplorerItem };
var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.CollectionLink, ExplorerIcon.OneToMany)
{
HyperlinkTarget = table.ExplorerItem,
ToolTipText = $"{table.ExplorerItem.Text} ({constraintName})"
};

foreignTable.ExplorerItem.Children.Add(explorerItem);

// create 'backward' association
Expand All @@ -51,10 +61,15 @@ public static string CreateOneToManyAssociation(this TableData table, TableData

public static string CreateManyToOneAssociation(this TableData table, TableData foreignTable, string columnNames, string foreignColumnNames, string constraintName, bool backwardReference = false)
{
// use the foreign table's name as property name (plus constraint name)
var propertyName = $"{foreignTable.Name} ({constraintName})";
table.PropertyAndFieldNames.Add(propertyName);

// use the foreign table's name as property name (ToDo singularize?)
var propertyName = $"{foreignTable.ExplorerItem.Text}";

var i = 1;
while (!table.PropertyAndFieldNames.Add(propertyName))
{
propertyName = $"{foreignTable.ExplorerItem.Text}{i++}";
}

// create a property of the foreign table's type in the table entity
var property = table.TypeBuilder.DefineProperty(propertyName, foreignTable.TypeBuilder);

Expand All @@ -79,7 +94,12 @@ public static string CreateManyToOneAssociation(this TableData table, TableData
property.AddAssociationAttribute(foreignColumnNames, columnNames, foreignTable.Name, backwardReference);

// create the explorer item
var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.ReferenceLink, ExplorerIcon.ManyToOne) { HyperlinkTarget = foreignTable.ExplorerItem };
var explorerItem = new ExplorerItem(propertyName, ExplorerItemKind.ReferenceLink, ExplorerIcon.ManyToOne)
{
HyperlinkTarget = foreignTable.ExplorerItem,
ToolTipText = $"{foreignTable.ExplorerItem.Text} ({constraintName})"
};

table.ExplorerItem.Children.Add(explorerItem);

return property.Name;
Expand Down

0 comments on commit ee22527

Please sign in to comment.