Skip to content

Commit fd5e04a

Browse files
committed
feat(context-graph-builder): intro resource attr
1 parent 629169f commit fd5e04a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/JsonApiDotNetCore/Builders/ContextGraphBuilder.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void AddDbContext<T>() where T : DbContext
100100
var entityType = dbSetType.GetGenericArguments()[0];
101101
entities.Add(new ContextEntity
102102
{
103-
EntityName = property.Name.Dasherize(),
103+
EntityName = GetResourceName(property),
104104
EntityType = entityType,
105105
Attributes = GetAttributes(entityType),
106106
Relationships = GetRelationships(entityType)
@@ -110,5 +110,14 @@ public void AddDbContext<T>() where T : DbContext
110110

111111
Entities = entities;
112112
}
113+
114+
private string GetResourceName(PropertyInfo property)
115+
{
116+
var resourceAttribute = property.GetCustomAttribute(typeof(ResourceAttribute));
117+
if(resourceAttribute == null)
118+
return property.Name.Dasherize();
119+
120+
return ((ResourceAttribute)resourceAttribute).ResourceName;
121+
}
113122
}
114123
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace JsonApiDotNetCore.Models
4+
{
5+
public class ResourceAttribute : Attribute
6+
{
7+
public ResourceAttribute(string resourceName)
8+
{
9+
ResourceName = resourceName;
10+
}
11+
12+
public string ResourceName { get; set; }
13+
}
14+
}

0 commit comments

Comments
 (0)