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

Support extension method as Navigation property #1528

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override void Apply(StructuralTypeConfiguration edmTypeConfiguration, ODa
{
if (!property.AddedExplicitly)
{
edmTypeConfiguration.RemoveProperty(property.PropertyInfo);
edmTypeConfiguration.RemoveProperty(property.PropertyInfo.PropertyInfo);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static void ApplyNavigation(NavigationPropertyConfiguration navProperty,
{
Type dependentType = Nullable.GetUnderlyingType(dependent.PropertyInfo.PropertyType) ?? dependent.PropertyInfo.PropertyType;
PrimitivePropertyConfiguration principal = principalEntity.Keys.FirstOrDefault(
k => k.PropertyInfo.PropertyType == dependentType && navProperty.PrincipalProperties.All(p => p != k.PropertyInfo));
k => k.PropertyInfo.PropertyType == dependentType && navProperty.PrincipalProperties.All(p => p != k.PropertyInfo.MemberInfo));

if (principal != null)
{
Expand Down Expand Up @@ -136,7 +136,7 @@ private static void ApplyPrimitive(PrimitivePropertyConfiguration dependent, Ent

Type dependentType = Nullable.GetUnderlyingType(dependent.PropertyInfo.PropertyType) ?? dependent.PropertyInfo.PropertyType;
PrimitivePropertyConfiguration principal = principalEntity.Keys.FirstOrDefault(
k => k.PropertyInfo.PropertyType == dependentType && navProperty.PrincipalProperties.All(p => p != k.PropertyInfo));
k => k.PropertyInfo.PropertyType == dependentType && navProperty.PrincipalProperties.All(p => p != k.PropertyInfo.MemberInfo));
if (principal != null)
{
navProperty.HasConstraint(dependent.PropertyInfo, principal.PropertyInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public override void Apply(PropertyConfiguration edmProperty,
}
else
{
structuralTypeConfiguration.RemoveProperty(edmProperty.PropertyInfo);
structuralTypeConfiguration.RemoveProperty(edmProperty.PropertyInfo.PropertyInfo);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override void Apply(StructuralPropertyConfiguration edmProperty,
EntityTypeConfiguration entity = structuralTypeConfiguration as EntityTypeConfiguration;
if (entity != null)
{
entity.HasKey(edmProperty.PropertyInfo);
entity.HasKey(edmProperty.PropertyInfo.PropertyInfo);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Apply(PropertyConfiguration edmProperty,

if (!edmProperty.AddedExplicitly)
{
structuralTypeConfiguration.RemoveProperty(edmProperty.PropertyInfo);
structuralTypeConfiguration.RemoveProperty(edmProperty.PropertyInfo.PropertyInfo);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Apply(EntityTypeConfiguration entity, ODataConventionModelB
PropertyConfiguration key = GetKeyProperty(entity);
if (key != null)
{
entity.HasKey(key.PropertyInfo);
entity.HasKey(key.PropertyInfo.PropertyInfo);
}
}

Expand Down
19 changes: 13 additions & 6 deletions src/Microsoft.AspNet.OData.Shared/Builder/EdmModelHelperMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private static string ConvertBindingPath(EdmTypeMap edmMap, NavigationPropertyBi
{
Type typeCast = TypeHelper.AsType(bindingInfo);
PropertyInfo propertyInfo = bindingInfo as PropertyInfo;
MethodInfo methodInfo = bindingInfo as MethodInfo;

if (typeCast != null)
{
Expand All @@ -184,7 +185,13 @@ private static string ConvertBindingPath(EdmTypeMap edmMap, NavigationPropertyBi
}
else if (propertyInfo != null)
{
bindings.Add(edmMap.EdmProperties[propertyInfo].Name);
PropertyDescriptor propDescr = new PropertyDescriptor(propertyInfo);
bindings.Add(edmMap.EdmProperties[propDescr].Name);
}
else if (methodInfo != null)
{
PropertyDescriptor propDescr = new PropertyDescriptor(methodInfo);
bindings.Add(edmMap.EdmProperties[propDescr].Name);
}
}

Expand Down Expand Up @@ -424,7 +431,7 @@ private static Dictionary<Type, IEdmType> AddTypes(this EdmModel model, EdmTypeM
model.AddClrTypeAnnotations(edmTypes);

// add annotation for properties
Dictionary<PropertyInfo, IEdmProperty> edmProperties = edmTypeMap.EdmProperties;
Dictionary<PropertyDescriptor, IEdmProperty> edmProperties = edmTypeMap.EdmProperties;
model.AddClrPropertyInfoAnnotations(edmProperties);
model.AddPropertyRestrictionsAnnotations(edmTypeMap.EdmPropertiesRestrictions);
model.AddPropertiesQuerySettings(edmTypeMap.EdmPropertiesQuerySettings);
Expand Down Expand Up @@ -488,13 +495,13 @@ private static void AddClrTypeAnnotations(this EdmModel model, Dictionary<Type,
}
}

private static void AddClrPropertyInfoAnnotations(this EdmModel model, Dictionary<PropertyInfo, IEdmProperty> edmProperties)
private static void AddClrPropertyInfoAnnotations(this EdmModel model, Dictionary<PropertyDescriptor, IEdmProperty> edmProperties)
{
foreach (KeyValuePair<PropertyInfo, IEdmProperty> edmPropertyMap in edmProperties)
foreach (KeyValuePair<PropertyDescriptor, IEdmProperty> edmPropertyMap in edmProperties)
{
IEdmProperty edmProperty = edmPropertyMap.Value;
PropertyInfo clrProperty = edmPropertyMap.Key;
if (edmProperty.Name != clrProperty.Name)
PropertyDescriptor clrProperty = edmPropertyMap.Key;
if (clrProperty.MethodInfo != null || edmProperty.Name != clrProperty.Name)
{
model.SetAnnotationValue(edmProperty, new ClrPropertyInfoAnnotation(clrProperty));
}
Expand Down
8 changes: 5 additions & 3 deletions src/Microsoft.AspNet.OData.Shared/Builder/EdmTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class EdmTypeBuilder
{
private readonly List<IEdmTypeConfiguration> _configurations;
private readonly Dictionary<Type, IEdmType> _types = new Dictionary<Type, IEdmType>();
private readonly Dictionary<PropertyInfo, IEdmProperty> _properties = new Dictionary<PropertyInfo, IEdmProperty>();
private readonly Dictionary<PropertyDescriptor, IEdmProperty> _properties = new Dictionary<PropertyDescriptor, IEdmProperty>();
private readonly Dictionary<IEdmProperty, QueryableRestrictions> _propertiesRestrictions = new Dictionary<IEdmProperty, QueryableRestrictions>();
private readonly Dictionary<IEdmProperty, ModelBoundQuerySettings> _propertiesQuerySettings = new Dictionary<IEdmProperty, ModelBoundQuerySettings>();
private readonly Dictionary<IEdmStructuredType, ModelBoundQuerySettings> _structuredTypeQuerySettings = new Dictionary<IEdmStructuredType, ModelBoundQuerySettings>();
Expand Down Expand Up @@ -433,7 +433,8 @@ private IList<IEdmStructuralProperty> GetDeclaringPropertyInfo(IEnumerable<Prope
foreach (PropertyInfo propInfo in propertyInfos)
{
IEdmProperty edmProperty;
if (_properties.TryGetValue(propInfo, out edmProperty))
PropertyDescriptor propDescr = new PropertyDescriptor(propInfo);
if (_properties.TryGetValue(propDescr, out edmProperty))
{
edmProperties.Add(edmProperty);
}
Expand All @@ -444,7 +445,8 @@ private IList<IEdmStructuralProperty> GetDeclaringPropertyInfo(IEnumerable<Prope
while (baseType != null)
{
PropertyInfo basePropInfo = baseType.GetProperty(propInfo.Name);
if (_properties.TryGetValue(basePropInfo, out edmProperty))
PropertyDescriptor basePropDescr = new PropertyDescriptor(basePropInfo);
if (_properties.TryGetValue(basePropDescr, out edmProperty))
{
edmProperties.Add(edmProperty);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.AspNet.OData.Shared/Builder/EdmTypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class EdmTypeMap
{
public EdmTypeMap(
Dictionary<Type, IEdmType> edmTypes,
Dictionary<PropertyInfo, IEdmProperty> edmProperties,
Dictionary<PropertyDescriptor, IEdmProperty> edmProperties,
Dictionary<IEdmProperty, QueryableRestrictions> edmPropertiesRestrictions,
Dictionary<IEdmProperty, ModelBoundQuerySettings> edmPropertiesQuerySettings,
Dictionary<IEdmStructuredType, ModelBoundQuerySettings> edmStructuredTypeQuerySettings,
Expand All @@ -31,7 +31,7 @@ public EdmTypeMap(

public Dictionary<Type, IEdmType> EdmTypes { get; private set; }

public Dictionary<PropertyInfo, IEdmProperty> EdmProperties { get; private set; }
public Dictionary<PropertyDescriptor, IEdmProperty> EdmProperties { get; private set; }

public Dictionary<IEdmProperty, QueryableRestrictions> EdmPropertiesRestrictions { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public EntityTypeConfiguration<TEntityType> DerivesFrom<TBaseType>() where TBase
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Explicit Expression generic type is more clear")]
public EntityTypeConfiguration<TEntityType> HasKey<TKey>(Expression<Func<TEntityType, TKey>> keyDefinitionExpression)
{
ICollection<PropertyInfo> properties = PropertySelectorVisitor.GetSelectedProperties(keyDefinitionExpression);
ICollection<MemberInfo> properties = PropertySelectorVisitor.GetSelectedProperties(keyDefinitionExpression, false);
foreach (PropertyInfo property in properties)
{
_configuration.HasKey(property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,34 @@ public class NavigationPropertyConfiguration : PropertyConfiguration
/// <param name="multiplicity">The <see cref="EdmMultiplicity"/>.</param>
/// <param name="declaringType">The declaring structural type.</param>
public NavigationPropertyConfiguration(PropertyInfo property, EdmMultiplicity multiplicity, StructuralTypeConfiguration declaringType)
: base(property, declaringType)
:this(CreatePropertyDescriptor(property), multiplicity, declaringType)
{
if (property == null)

}

/// <summary>
/// Initializes a new instance of the <see cref="NavigationPropertyConfiguration"/> class.
/// </summary>
/// <param name="propertyDecriptor">The backing CLR property.</param>
/// <param name="multiplicity">The <see cref="EdmMultiplicity"/>.</param>
/// <param name="declaringType">The declaring structural type.</param>
public NavigationPropertyConfiguration(PropertyDescriptor propertyDecriptor, EdmMultiplicity multiplicity, StructuralTypeConfiguration declaringType)
: base(propertyDecriptor, declaringType)
{
if (propertyDecriptor == null)
{
throw Error.ArgumentNull("property");
throw Error.ArgumentNull("propertyDescripor");
}

Multiplicity = multiplicity;

_relatedType = property.PropertyType;
_relatedType = propertyDecriptor.PropertyType;
if (multiplicity == EdmMultiplicity.Many)
{
Type elementType;
if (!TypeHelper.IsCollection(_relatedType, out elementType))
{
throw Error.Argument("property", SRResources.ManyToManyNavigationPropertyMustReturnCollection, property.Name, TypeHelper.GetReflectedType(property).Name);
throw Error.Argument("property", SRResources.ManyToManyNavigationPropertyMustReturnCollection, propertyDecriptor.Name, TypeHelper.GetReflectedType(propertyDecriptor).Name);
}

_relatedType = elementType;
Expand All @@ -53,6 +65,15 @@ public NavigationPropertyConfiguration(PropertyInfo property, EdmMultiplicity mu
OnDeleteAction = EdmOnDeleteAction.None;
}

private static PropertyDescriptor CreatePropertyDescriptor(PropertyInfo property)
{
if(property == null)
{
throw Error.ArgumentNull("property");
}
return new PropertyDescriptor(property);
}

/// <summary>
/// Gets the <see cref="EdmMultiplicity"/> of this navigation property.
/// </summary>
Expand Down Expand Up @@ -192,6 +213,18 @@ public NavigationPropertyConfiguration HasConstraint(PropertyInfo dependentPrope
principalPropertyInfo));
}

/// <summary>
/// Configures the referential constraint with the specified <parameref name="dependentPropertyInfo"/>
/// and <parameref name="principalPropertyInfo" />.
/// </summary>
/// <param name="dependentPropertyInfo">The dependent property info for the referential constraint.</param>
/// <param name="principalPropertyInfo">The principal property info for the referential constraint.</param>
public NavigationPropertyConfiguration HasConstraint(PropertyDescriptor dependentPropertyInfo,
PropertyDescriptor principalPropertyInfo)
{
return HasConstraint(dependentPropertyInfo.PropertyInfo, principalPropertyInfo.PropertyInfo);
}

/// <summary>
/// Configures the referential constraint with the dependent and principal property pair.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.AspNet.OData.Common;

namespace Microsoft.AspNet.OData.Builder
Expand Down Expand Up @@ -525,8 +526,8 @@ private void VerifyBindingPath(NavigationPropertyConfiguration navigationConfigu
Contract.Assert(navigationConfiguration != null);
Contract.Assert(bindingPath != null);

PropertyInfo navigation = bindingPath.Last() as PropertyInfo;
if (navigation == null || navigation != navigationConfiguration.PropertyInfo)
MemberInfo navigation = bindingPath.Last() as MemberInfo;
if (navigation == null || navigation != navigationConfiguration.PropertyInfo.MemberInfo)
{
throw Error.Argument("navigationConfiguration", SRResources.NavigationPropertyBindingPathIsNotValid,
bindingPath.ConvertBindingPath(), navigationConfiguration.Name);
Expand All @@ -552,13 +553,25 @@ private static Type VerifyBindingSegment(Type current, MemberInfo info)
return derivedType.BaseType;
}

Type declaringType = null;
Type propertyType = null;
PropertyInfo propertyInfo = info as PropertyInfo;
if (propertyInfo == null)
MethodInfo methodInfo = info as MethodInfo;
if(propertyInfo!=null)
{
declaringType = info.DeclaringType;
propertyType = propertyInfo.PropertyType;
}
else if(methodInfo!=null && methodInfo.GetCustomAttribute<ExtensionAttribute>()!=null)
{
declaringType = methodInfo.GetParameters().First().ParameterType;
propertyType = methodInfo.ReturnType;
}
else
{
throw Error.NotSupported(SRResources.NavigationPropertyBindingPathNotSupported, info.Name, info.MemberType);
}

Type declaringType = propertyInfo.DeclaringType;
if (declaringType == null ||
!(declaringType.IsAssignableFrom(current) || current.IsAssignableFrom(declaringType)))
{
Expand All @@ -567,12 +580,12 @@ private static Type VerifyBindingSegment(Type current, MemberInfo info)
}

Type elementType;
if (TypeHelper.IsCollection(propertyInfo.PropertyType, out elementType))
if (TypeHelper.IsCollection(propertyType, out elementType))
{
return elementType;
}

return propertyInfo.PropertyType;
return propertyType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class PrimitivePropertyConfiguration : StructuralPropertyConfiguration
/// <param name="property">The name of the property.</param>
/// <param name="declaringType">The declaring EDM type of the property.</param>
public PrimitivePropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
: base(property, declaringType)
:base(property, declaringType)
{

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class PropertyConfiguration
/// </summary>
/// <param name="property">The name of the property.</param>
/// <param name="declaringType">The declaring EDM type of the property.</param>
protected PropertyConfiguration(PropertyInfo property, StructuralTypeConfiguration declaringType)
protected PropertyConfiguration(PropertyDescriptor property, StructuralTypeConfiguration declaringType)
{
if (property == null)
{
Expand Down Expand Up @@ -67,7 +67,7 @@ public string Name
/// <summary>
/// Gets the mapping CLR <see cref="PropertyInfo"/>.
/// </summary>
public PropertyInfo PropertyInfo { get; private set; }
public PropertyDescriptor PropertyInfo { get; private set; }

/// <summary>
/// Gets the CLR <see cref="Type"/> of the property.
Expand Down
Loading