Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method to get all navigation property bindings #2157

Merged
merged 7 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,39 @@ public static IEnumerable<IEdmOperationImport> OperationImports(this IEdmEntityC
return container.AllElements().OfType<IEdmOperationImport>();
}

/// <summary>
/// Returns NavigationPropertyBindings with their corresponding container elements (EntitySet or Singelton)
/// </summary>
/// <param name="container">Reference to the calling object.</param>
/// <returns>collection of pairs of container element and NavigationPropertyBindings.</returns>
public static IEnumerable<(IEdmEntityContainerElement, IEdmNavigationPropertyBinding)> GetNavigationPropertyBindings(this IEdmEntityContainer container)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the caller be interested in the specific kind of IEdmEntityContainerElement returned for each element? We are already casting inside this method, it would be a shame for the caller to have to do so as well if we can avoid it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is true that a user would need to cast the first element of the pair if they are interested in more than what IEdmEntityContainerElement provides (which is the name, container, kind and a few extension methods).
But I doubt that this is common since this API is specifically about all bindings, independent of the type of "bindable" container element.

gathogojr marked this conversation as resolved.
Show resolved Hide resolved
{
EdmUtil.CheckArgumentNull(container, "container");

var elements = container.AllElements();
chrisspre marked this conversation as resolved.
Show resolved Hide resolved
foreach(var element in elements)
{
switch (element)
{
case IEdmEntitySet entitySet:
foreach(var binding in entitySet.NavigationPropertyBindings)
{
yield return (entitySet, binding);
gathogojr marked this conversation as resolved.
Show resolved Hide resolved
}
break;
case IEdmSingleton singleton:
foreach (var binding in singleton.NavigationPropertyBindings)
Copy link
Contributor

@corranrogue9 corranrogue9 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a more fundamental change, but I wonder if it would make sense for IEdmEntitySet and IEdmSingleton to derive from a common interface that has NavigationPropertyBindings on it. Then this whole method is sort of obsolete (or at least very derivative):

return container
  .AllElements()
  .OfType<IEdmWithNavPropertyBindings>()
  .SelectMany(element => element.NavigationPropertyBindings.Select(binding => (element, binding)));

{
yield return (singleton, binding);
gathogojr marked this conversation as resolved.
Show resolved Hide resolved
}
break;
default:
break;
}
}
}


#endregion

#region IEdmTypeReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,28 @@ public void EnsureActionImportActionPropertyIsUnresolvedAction()
Assert.Equal(EdmContainerElementKind.ActionImport, imports[0].ContainerElementKind);
Assert.Null(imports[0].EntitySet);
}

[Fact]
public void NavigationPropertyBindingsReturned()
{
// arrange
var entitySet1 = new CsdlEntitySet("EntitySet1", "unknown", new[] {
new CsdlNavigationPropertyBinding("foo", "bar", testLocation)
}, testLocation);
var singleton1 = new CsdlSingleton("Singleton", "unknown", new[] {
new CsdlNavigationPropertyBinding("foo", "bar", testLocation)
}, testLocation);
var csdlEntityContainer = CsdlBuilder.EntityContainer("Container", entitySets: new [] { entitySet1 }, singletons: new[] { singleton1 } );
var schema = CsdlBuilder.Schema("FQ.NS", csdlEntityContainers: new CsdlEntityContainer[] { csdlEntityContainer });
var csdlModel = new CsdlModel();
csdlModel.AddSchema(schema);

// act
var container = (IEdmEntityContainer)csdlEntityContainer;
gathogojr marked this conversation as resolved.
Show resolved Hide resolved
var bindings = container.GetNavigationPropertyBindings().ToList();

// assert
Assert.Equal(2, bindings.Count);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also assert the actual bindings are what's expected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@habbes Added requested asserts

}
}
}