Skip to content

Commit 397e43a

Browse files
author
Bart Koelman
committed
Added PropertyInfo to RelationshipAttribute
Inlined RelationshipAttribute.InternalRelationshipName Explicit interface implementation and property rename Inlined IsHasOne/IsHasMany on RelationshipAttribute
1 parent b55c295 commit 397e43a

15 files changed

+42
-42
lines changed

src/JsonApiDotNetCore/Builders/ResourceGraphBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ protected virtual List<RelationshipAttribute> GetRelationships(Type entityType)
128128
var attribute = (RelationshipAttribute)prop.GetCustomAttribute(typeof(RelationshipAttribute));
129129
if (attribute == null) continue;
130130

131+
attribute.PropertyInfo = prop;
131132
attribute.PublicRelationshipName ??= FormatPropertyName(prop);
132-
attribute.InternalRelationshipName = prop.Name;
133133
attribute.RightType = GetRelationshipType(attribute, prop);
134134
attribute.LeftType = entityType;
135135
attributes.Add(attribute);
@@ -179,7 +179,7 @@ protected virtual List<RelationshipAttribute> GetRelationships(Type entityType)
179179
}
180180

181181
protected virtual Type GetRelationshipType(RelationshipAttribute relation, PropertyInfo prop) =>
182-
relation.IsHasMany ? prop.PropertyType.GetGenericArguments()[0] : prop.PropertyType;
182+
relation is HasOneAttribute ? prop.PropertyType : prop.PropertyType.GetGenericArguments()[0];
183183

184184
private List<EagerLoadAttribute> GetEagerLoads(Type entityType, int recursionDepth = 0)
185185
{

src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void LoadInverseRelationships(object trackedRelationshipValue, Relations
149149

150150
private bool IsHasOneRelationship(string internalRelationshipName, Type type)
151151
{
152-
var relationshipAttr = _resourceGraph.GetRelationships(type).FirstOrDefault(r => r.InternalRelationshipName == internalRelationshipName);
152+
var relationshipAttr = _resourceGraph.GetRelationships(type).FirstOrDefault(r => r.PropertyInfo.Name == internalRelationshipName);
153153
if (relationshipAttr != null)
154154
{
155155
if (relationshipAttr is HasOneAttribute)
@@ -425,7 +425,7 @@ protected void LoadCurrentRelationships(TResource oldEntity, RelationshipAttribu
425425
}
426426
else if (relationshipAttribute is HasManyAttribute hasManyAttribute)
427427
{
428-
_context.Entry(oldEntity).Collection(hasManyAttribute.InternalRelationshipName).Load();
428+
_context.Entry(oldEntity).Collection(hasManyAttribute.PropertyInfo.Name).Load();
429429
}
430430
}
431431

src/JsonApiDotNetCore/Extensions/QueryableExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private static IQueryable<TSource> CallGenericWhereContainsMethod<TSource>(IQuer
199199
MemberExpression member;
200200
if (filter.IsAttributeOfRelationship)
201201
{
202-
var relation = Expression.PropertyOrField(entity, filter.Relationship.InternalRelationshipName);
202+
var relation = Expression.PropertyOrField(entity, filter.Relationship.PropertyInfo.Name);
203203
member = Expression.Property(relation, filter.Attribute.PropertyInfo.Name);
204204
}
205205
else
@@ -263,16 +263,16 @@ private static IQueryable<TSource> CallGenericWhereMethod<TSource>(IQueryable<TS
263263
// Is relationship attribute
264264
if (filter.IsAttributeOfRelationship)
265265
{
266-
var relationProperty = concreteType.GetProperty(filter.Relationship.InternalRelationshipName);
266+
var relationProperty = concreteType.GetProperty(filter.Relationship.PropertyInfo.Name);
267267
if (relationProperty == null)
268-
throw new ArgumentException($"'{filter.Relationship.InternalRelationshipName}' is not a valid relationship of '{concreteType}'");
268+
throw new ArgumentException($"'{filter.Relationship.PropertyInfo.Name}' is not a valid relationship of '{concreteType}'");
269269

270270
var relatedType = filter.Relationship.RightType;
271271
property = relatedType.GetProperty(filter.Attribute.PropertyInfo.Name);
272272
if (property == null)
273-
throw new ArgumentException($"'{filter.Attribute.PropertyInfo.Name}' is not a valid attribute of '{filter.Relationship.InternalRelationshipName}'");
273+
throw new ArgumentException($"'{filter.Attribute.PropertyInfo.Name}' is not a valid attribute of '{filter.Relationship.PropertyInfo.Name}'");
274274

275-
var leftRelationship = Expression.PropertyOrField(parameter, filter.Relationship.InternalRelationshipName);
275+
var leftRelationship = Expression.PropertyOrField(parameter, filter.Relationship.PropertyInfo.Name);
276276
// {model.Relationship}
277277
left = Expression.PropertyOrField(leftRelationship, property.Name);
278278
}

src/JsonApiDotNetCore/Hooks/Execution/RelationshipsDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public Dictionary<RelationshipAttribute, HashSet<TResource>> GetByRelationship(T
9494
public HashSet<TResource> GetAffected(Expression<Func<TResource, object>> navigationAction)
9595
{
9696
var property = TypeHelper.ParseNavigationExpression(navigationAction);
97-
return this.Where(p => p.Key.InternalRelationshipName == property.Name).Select(p => p.Value).SingleOrDefault();
97+
return this.Where(p => p.Key.PropertyInfo.Name == property.Name).Select(p => p.Value).SingleOrDefault();
9898
}
9999
}
100100
}

src/JsonApiDotNetCore/Internal/InverseRelationships.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void Resolve()
5252
foreach (var attr in ce.Relationships)
5353
{
5454
if (attr is HasManyThroughAttribute) continue;
55-
INavigation inverseNavigation = meta.FindNavigation(attr.InternalRelationshipName)?.FindInverse();
55+
INavigation inverseNavigation = meta.FindNavigation(attr.PropertyInfo.Name)?.FindInverse();
5656
attr.InverseNavigation = inverseNavigation?.Name;
5757
}
5858
}

src/JsonApiDotNetCore/Internal/Query/BaseQueryContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected BaseQueryContext(TQuery query)
2323
public string GetPropertyPath()
2424
{
2525
if (IsAttributeOfRelationship)
26-
return $"{Relationship.InternalRelationshipName}.{Attribute.PropertyInfo.Name}";
26+
return $"{Relationship.PropertyInfo.Name}.{Attribute.PropertyInfo.Name}";
2727

2828
return Attribute.PropertyInfo.Name;
2929
}

src/JsonApiDotNetCore/Internal/ResourceGraph.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public RelationshipAttribute GetInverse(RelationshipAttribute relationship)
6666
if (relationship.InverseNavigation == null) return null;
6767
return GetResourceContext(relationship.RightType)
6868
.Relationships
69-
.SingleOrDefault(r => r.InternalRelationshipName == relationship.InverseNavigation);
69+
.SingleOrDefault(r => r.PropertyInfo.Name == relationship.InverseNavigation);
7070
}
7171

7272
private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selector = null, FieldFilterType type = FieldFilterType.None) where T : IIdentifiable
@@ -88,7 +88,7 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec
8888
{ // model => model.Field1
8989
try
9090
{
91-
targeted.Add(available.Single(f => f.ExposedInternalMemberName == memberExpression.Member.Name));
91+
targeted.Add(available.Single(f => f.PropertyName == memberExpression.Member.Name));
9292
return targeted;
9393
}
9494
catch (InvalidOperationException)
@@ -109,7 +109,7 @@ private IEnumerable<IResourceField> Getter<T>(Expression<Func<T, dynamic>> selec
109109
foreach (var member in newExpression.Members)
110110
{
111111
memberName = member.Name;
112-
targeted.Add(available.Single(f => f.ExposedInternalMemberName == memberName));
112+
targeted.Add(available.Single(f => f.PropertyName == memberName));
113113
}
114114
return targeted;
115115
}

src/JsonApiDotNetCore/Models/Annotation/AttrAttribute.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public AttrAttribute(string publicName = null, bool isImmutable = false, bool is
3535
IsSortable = isSortable;
3636
}
3737

38-
public string ExposedInternalMemberName => PropertyInfo.Name;
38+
string IResourceField.PropertyName => PropertyInfo.Name;
3939

4040
/// <summary>
4141
/// How this attribute is exposed through the API
@@ -62,9 +62,9 @@ public AttrAttribute(string publicName = null, bool isImmutable = false, bool is
6262
public bool IsSortable { get; }
6363

6464
/// <summary>
65-
/// The member property info
65+
/// The resource property that this attribute is declared on.
6666
/// </summary>
67-
public PropertyInfo PropertyInfo { get; set; }
67+
public PropertyInfo PropertyInfo { get; internal set; }
6868

6969
/// <summary>
7070
/// Get the value of the attribute for the given object.

src/JsonApiDotNetCore/Models/Annotation/HasManyAttribute.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ public HasManyAttribute(string publicName = null, Link relationshipLinks = Link.
3838
public override object GetValue(object entity)
3939
{
4040
return entity?.GetType()
41-
.GetProperty(InternalRelationshipName)?
41+
.GetProperty(PropertyInfo.Name)?
4242
.GetValue(entity);
4343
}
4444

45-
4645
/// <summary>
4746
/// Sets the value of the property identified by this attribute
4847
/// </summary>
@@ -52,7 +51,7 @@ public override void SetValue(object entity, object newValue)
5251
{
5352
var propertyInfo = entity
5453
.GetType()
55-
.GetProperty(InternalRelationshipName);
54+
.GetProperty(PropertyInfo.Name);
5655

5756
propertyInfo.SetValue(entity, newValue);
5857
}

src/JsonApiDotNetCore/Models/Annotation/HasManyThroughAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public override object GetValue(object entity)
9494
return rightEntities.Cast(RightType);
9595
}
9696

97-
9897
/// <summary>
9998
/// Sets the value of the property identified by this attribute
10099
/// </summary>
@@ -104,7 +103,7 @@ public override void SetValue(object entity, object newValue)
104103
{
105104
var propertyInfo = entity
106105
.GetType()
107-
.GetProperty(InternalRelationshipName);
106+
.GetProperty(PropertyInfo.Name);
108107
propertyInfo.SetValue(entity, newValue);
109108

110109
if (newValue == null)

0 commit comments

Comments
 (0)