Skip to content

Commit

Permalink
Slight performance improvement for IEdmNavigationSource.EntityType() (
Browse files Browse the repository at this point in the history
#2808)

* Simplify EntityType() method

* Optimize Type property in CsdlSemanticsEntitySet and Singleton
  • Loading branch information
habbes authored Nov 30, 2023
1 parent 46d9ffa commit 1f67bb9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 30 deletions.
12 changes: 11 additions & 1 deletion src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsEntitySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ public CsdlSemanticsEntitySet(CsdlSemanticsEntityContainer container, CsdlEntity
{
}

private IEdmType type;

public override IEdmType Type
{
get { return new EdmCollectionType(new EdmEntityTypeReference(this.typeCache.GetValue(this, ComputeElementTypeFunc, null), false)); }
get
{
if (type == null)
{
type = new EdmCollectionType(new EdmEntityTypeReference(this.typeCache.GetValue(this, ComputeElementTypeFunc, null), false));
}

return type;
}
}

public override EdmContainerElementKind ContainerElementKind
Expand Down
12 changes: 11 additions & 1 deletion src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ public CsdlSemanticsSingleton(CsdlSemanticsEntityContainer container, CsdlSingle
{
}

private IEdmType type;

public override IEdmType Type
{
get { return this.typeCache.GetValue(this, ComputeElementTypeFunc, null); }
get
{
if (type == null)
{
type = this.typeCache.GetValue(this, ComputeElementTypeFunc, null);
}

return type;
}
}

public override EdmContainerElementKind ContainerElementKind
Expand Down
29 changes: 1 addition & 28 deletions src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2822,34 +2822,7 @@ public static string FullNavigationSourceName(this IEdmNavigationSource navigati
/// <returns>The entity type of the navigation source.</returns>
public static IEdmEntityType EntityType(this IEdmNavigationSource navigationSource)
{
var entitySetBase = navigationSource as IEdmEntitySetBase;
if (entitySetBase != null)
{
IEdmCollectionType collectionType = entitySetBase.Type as IEdmCollectionType;

if (collectionType != null)
{
return collectionType.ElementType.Definition as IEdmEntityType;
}

var unknownEntitySet = entitySetBase as IEdmUnknownEntitySet;
if (unknownEntitySet != null)
{
// Handle missing navigation target for nullable
// singleton navigation property.
return unknownEntitySet.Type as IEdmEntityType;
}

return null;
}

var singleton = navigationSource as IEdmSingleton;
if (singleton != null)
{
return singleton.Type as IEdmEntityType;
}

return null;
return navigationSource?.Type.AsElementType() as IEdmEntityType;
}

#endregion
Expand Down

0 comments on commit 1f67bb9

Please sign in to comment.